CC

Sketch:

//Arduino Sketch for Garage door
#include <EEPROM.h>
#include <RCSwitch.h>
//Variable
String BTCodeOC = "4407"; //Open/Close, example
String BTCodeD = "4408"; //Delete RC code, example
String BTCodeW = "4409"; //Write RC code, example
// Door safety infrared photocell(DSIR)> LOW is activated
byte st; //status Door 0>open / 1>close / 2>Stop - activated DSIR
byte m; //move Door
byte sa; //status action 1>yes / 0>no
unsigned long pt = 0; //start interval
unsigned long ti = 12000; //period for open or close door
unsigned long dRC ; //wait period RC
byte np[4]; //count and level
//byte np[0]; //curent level
//byte np[1]; //count DSIR
//byte np[2]; //curent level
//byte np[3]; //pre level DSIR
//*************************************************************************
//BlueTooth
String inputString = "";         // a String to hold incoming data
bool stringComplete = false;  // whether the string is complete
void serialEvent() {
  while (Serial.available()) {
    char inChar = (char)Serial.read();// get the new byte:
    inputString += inChar;// add it to the inputString:
    if (inChar == '\n') {// if the incoming character is a newline
      stringComplete = true; break;
    }
  }
}
//*************************************************************************
// Reset
void(* resetFunc) (void) = 0;
// RCSwitch
RCSwitch mySwitch = RCSwitch();
unsigned long receiveCode; //return code RC
unsigned long codeArray[11]; // array recorded code
byte codeCount; // number recorded code

// Setup, intializing
//*************************************************************************
void setup() {
  pinMode(10, OUTPUT); //Pin output to Open door
  digitalWrite(10, LOW);
  pinMode(9, OUTPUT); //Pin output to Close door
  digitalWrite(9, LOW);
  pinMode(LED_BUILTIN, OUTPUT);
  //pin#2 RC input
  pinMode(3, INPUT_PULLUP); //in DSIR, LOW>active
  np[0] = 1; //level input pin
  np[1] = 0; //count DSIR
  np[2] = 0; //return digitalAct()
  np[3] = 1; //previous level pin#3
  codeCount = EEPROM.read(0); // read number recorded code
  //Serial setup
  Serial.begin(9600);
  inputString.reserve(200);
  mySwitch.enableReceive(0);  // Receiver on interrupt 0 => that is pin #2
  if (digitalRead(3) == HIGH) { //check DSIR > Close door
    oc(9, 1);
  }
  rRC();
}
// Repeatedly action
//*************************************************************************
void loop() {
  chkDSIR();// read the value from the DSIR
  if (interval(ti) && m != 0) { //check for finish interval > Reset
    rst();
    Serial.println("Reset");
  }
  if (interval(600000)) { // check DSIR > Close after 10min
    if (digitalRead(3) == HIGH && (st == 0 || st == 2)) {
      oc(9, 1);
      Serial.println("Finish timeout 10min - Close door");
    }
  }
  // input BT code
  //***********************************************************************
  // compare string when a newline arrives:
  if (stringComplete) {
    Serial.println(inputString);
    if (inputString == BTCodeOC) {
      sa = 1; logic();
    } else if (inputString == BTCodeD) {
      Serial.println("Delete code");
      EEPROM.write(0, 0); delay(1000);
      resetFunc();// RESET
    } else if (inputString == BTCodeW) {
      Serial.println("Write new code");
      wRC();
    }
    inputString = "";// clear the string:
    stringComplete = false;
  }
  //input RC code
  //***********************************************************************
  receiveCode = chkRC();
  if (millis() - dRC > 1200) {
    // compare code
    byte cn = 0;
    while (receiveCode != 0 && cn < codeCount) {
      if (codeArray[cn] == receiveCode) {
        Serial.println("Recognition RC code");
        sa = 1; logic(); dRC = millis();
        break;
      }
      cn++;
    }
  }
  mySwitch.resetAvailable();
}

//Module
//Function with RCSwitch
//*************************************************************************
unsigned long chkRC() { // check for signal from RC
  if (mySwitch.available()) {
    return mySwitch.getReceivedValue(); //return curent code
  }
  return 0;
}
//********************************************
void wRC() { // write code from RC
  if (codeCount > 12) {
    Serial.println("Memory is FULL"); return;
  }
  unsigned long receiveCode1, receiveCode2, receiveCode3;
  delay(1000); receiveCode1 = chkRC();
  mySwitch.resetAvailable();
  Serial.print("Received code 1 - ");
  Serial.println(receiveCode1); delay(1000);
  if (receiveCode1 != 0) {
    receiveCode2 = chkRC();
    mySwitch.resetAvailable();
    Serial.print("Received code 2 - ");
    Serial.println(receiveCode2); delay(1000);
    if (receiveCode2 != 0) {
      receiveCode3 = chkRC();
      Serial.print("Received code 3 - ");
      Serial.println(receiveCode3);
      if (receiveCode3 != 0) {
        if (receiveCode1 == receiveCode2) {
          if (receiveCode1 == receiveCode3) {
            unsigned long cB = receiveCode1, codeByte[4];
            Serial.print("Received code Verify - ");
            Serial.println(cB);
            codeByte[0] = cB - (cB / 100) * 100;
            cB = (cB / 100);
            codeByte[1] = cB - (cB / 100) * 100;
            cB = (cB / 100);
            codeByte[2] = cB - (cB / 100) * 100;
            cB = (cB / 100);
            codeByte[3] = cB - (cB / 100) * 100;
            Serial.println(receiveCode1);
            Serial.println("*******************");
            //Serial.println(codeByte[0]);
            //Serial.println(codeByte[1]);
            //Serial.println(codeByte[2]);
            //Serial.println(codeByte[3]);
            delay(1000);
            EEPROM.update(codeCount * 4 + 1, codeByte[0]);
            EEPROM.update(codeCount * 4 + 2, codeByte[1]);
            EEPROM.update(codeCount * 4 + 3, codeByte[2]);
            EEPROM.update(codeCount * 4 + 4, codeByte[3]);
            EEPROM.update(0, codeCount + 1);
            delay(1000);
            resetFunc();// RESET
          }
        }
      }
      mySwitch.resetAvailable();
    }
  }
}
//***************************************************************
void rRC() {
  Serial.print("Number of recorded code - ");
  Serial.println(codeCount); unsigned long codeByte[4]; // 4 byte from code
  if (codeCount > 0) {
    for (byte cn = 0; cn < codeCount; cn++) {
      codeByte[0] = EEPROM.read(cn * 4 + 1);
      codeByte[1] = EEPROM.read(cn * 4 + 2);
      codeByte[2] = EEPROM.read(cn * 4 + 3);
      codeByte[3] = EEPROM.read(cn * 4 + 4);
      codeArray[cn] = codeByte[0] + codeByte[1] * 100 + codeByte[2] * 10000 + codeByte[3] * 1000000;
      Serial.println(codeArray[cn]);
      /* Serial.println(codeByte[0]);
        Serial.println(codeByte[1]);
        Serial.println(codeByte[2]);
        Serial.println(codeByte[3]);
      */
    }
  } else {
    Serial.println("NO recorded code");
  }
}
//*************************************************************************
//Manipulating Door
// Function
//*************************************************************************
// Flash
void flash(byte n) { // indicate, delay with flash
  for (byte k = 0; k < n; k++) {
    digitalWrite(LED_BUILTIN, HIGH); delay(10);
    digitalWrite(LED_BUILTIN, LOW); delay(300);
  }
}
//Reset
void rst () { // reset out pins
  digitalWrite(10, LOW);
  digitalWrite(9, LOW);
  m = 0; sa = 0; ti = 0; flash(1);
}
//Set out pins
void oc(byte p, byte s) { // p- pin board, s=0 open/ s=1 close
  rst(); digitalWrite(p, HIGH);
  pt = millis(); m = p; st = s; ti = 12000;
  if (p == 9) {
    Serial.println("Close");
  }
  if (p == 10) {
    Serial.println("Open");
  }
}
//Check finish interval
bool interval(unsigned long ti) { // true>finish, falshe>continue
  return (millis() - pt >= ti);
}
//Management logic
void logic() {
  if (sa == 1) { //existence signal for action
    if (m != 0) { //check finish interval > reset/stop
      rst(); Serial.println("Stop");
    }
    else {
      if (digitalRead(3) == HIGH) {
        if (st == 1) { //st=1>Door is Close, else st=0>Door is Open
          oc(10, 0);   //Open door
        }
        else {
          oc(9, 1);   //Close door
        }
      }
      else { //activated DSIR> only Open half
        oc(10, 0); ti = 5000;
      }
    }
  }
}
//Check input digital pins
byte digitalAct(byte i) { //check for action in i-pin board
  np[0] = (digitalRead(i) == LOW) ? 0 : 1;
  switch (np[0] - np[i]) {
    case 0:
      return np[0]; //if level const.
    case -1:
      np[i] = np[0];
      flash(1);
      return 3; //if level 1>0
    case 1:
      np[i] = np[0];
      flash(1);
      return 4;  //if level 0>1
  }
  return 5;
}
//Check count DSIR > if count 3 times >Close Door
void chkDSIR() {
  np[2] = digitalAct(3);
  if (np[0] == 0 && ti != 5000) {
    rst(); st = 2;
    Serial.println("Stop - activated DSIR");
  }
  if (np[2] == 3 && m == 0) {
    np[1]++;
    if (np[1] == 1) {
      np[1]++; pt = millis();
    }
    if ((np[1] > 0 && np[1] < 5) && !interval(4500)) {
      Serial.println(np[1]);
      if (np[1] == 4) {
        oc(9, 1); np[1] = 0; flash(5); return;
      }
      else {
        return;
      }
    }
    else {
      np[1] = 0; return;
    }
  }
}
//****************************************************************