Hello! I'm trying to put together a 3 sensor alarm controlled by google's Firebase. The logic is as follows:
-
With a mobile app I select an option that changes the value of a firebase variable. 6 possible combinations: Activated, Activated silently, Deactivated, Turn on the siren, turn it off and Reset.
-
This variable data is received by the ESP-8266-01, which connects to an Arduino MEGA and it saves this data in a "state" variable.
-
With that variable "state", configure the interrupts "live" (activating or deactivating them).
I have 3 sensors, which in case the state is activated or activated silently I need their interrupt to be activated (the only difference between them is that the siren sounds or not). And in case of deactivated I need that the interruption of the sensors is deactivated.
My problem occurs in item 3, I don't know if it's okay to use the function a**ttachInterrupt(digitalPinToInterrupt(ESB),DETECCION , HIGH); / detachInterrupt(digitalPinToInterrupt(ESB)); to disable / enable interrupts accordingly.
You can see my problem in the function: "Desactivar()" and "ActivarTotalmente()"
I apologize from the start, English is not my first language. Thank you very much for understanding
some names:
PC, SB, PG: Sensor
LPC, LSB, LPG: Sensor LEDs
RPC, RSB, RPG: Sensor relay
void loop() {
Lectura();
}
// -----------------------------------------------------------------------------------
// Serial port reading stage, the data received is stored in the variable 'data'
void Lectura(){
while(1){
while (Serial.available())
{
char character = Serial.read();
if (character != '\n')
{
data.concat(character);
}
else
{
Serial.println(data);
data = "";
}
}
if (data == "ACTIVATOTAL"){
estado == 'A'; // It indicates that the status is A = Activated
ActivarTotalmente();
}
if (data == "ACTIVASILENCIO"){
estado == 'S'; // It indicates that the status is S = Silenty Activated
ActivarSilenciada();
}
if (data == "DESACTIVADA"){
estado == 'D'; // It indicates that the status is D = Deactivated
Desactivar();
}
if (data == "DISUADIRON"){ // Turn on just the siren
digitalWrite(RSIR,HIGH);
}
if (data == "DISUADIROFF"){ // Turn off just the siren
digitalWrite(RSIR,LOW);
}
if (data == "RESET"){
Reset();
}
}
} // up to this point I'm sure it works
// -----------------------------------------------------------------------------------
// Activation States Activated, Silenced or Deactivated
void ActivarTotalmente(){
attachInterrupt(digitalPinToInterrupt(ESB),DETECCION , HIGH); // Enable interrupt de SB (one of the sensors gives '1' when it is triggered)
attachInterrupt(digitalPinToInterrupt(EPG),DETECCION , LOW); // Enable interrupcion de PG (others work with '0' when triggered)
attachInterrupt(digitalPinToInterrupt(EPC),DETECCION , LOW); // Enable interrupcion de PC
int a = 2;
Pestaneo(a); // towards a blink function, I had no problems with this so I did not include it
Lecture();
}
// -----------------------------------------------------------------------------------
void ActivarSilenciada(){
attachInterrupt(digitalPinToInterrupt(ESB),DETECCION , HIGH); // Enable interrupt SB (The 'E' is for 'Entrada' OR 'Input')
attachInterrupt(digitalPinToInterrupt(EPG),DETECCION , LOW); // Enable interrupt PG
attachInterrupt(digitalPinToInterrupt(EPC),DETECCION , LOW); // Enable interrupt PC
int a = 1;
Pestaneo(a);
Lectura();
}
// -----------------------------------------------------------------------------------
void Desactivar(){
detachInterrupt(digitalPinToInterrupt(ESB)); // Remove the interrupt on pin SB
detachInterrupt(digitalPinToInterrupt(EPG)); // Remove the interrupt on pin PG
detachInterrupt(digitalPinToInterrupt(EPC)); // Remove the interrupt on pin PC
int a = 3;
Pestaneo(a);
digitalWrite(RPC,LOW); // I turn off the relays after disabling the interruptions, in order to save energy
digitalWrite(RPG,LOW);
digitalWrite(RSB,LOW);
delay(500);
Lectura();
}
// -----------------------------------------------------------------------------------
void DETECCION(){ //In the detection stage, I check which sensors are triggered and based on that I return to firebase a change of variable (which for now is only a serialprint), plus activate or not the siren based on it
if (EPC == LOW){
digitalWrite(LPC, HIGH);
Serial.println("PCDIS"); // Warns that PC has been triggered
if (estado == 'A'){ // Activates siren if it is 'Activated', for 1 minute
digitalWrite(RSIR,HIGH);
delay(1000);
}
}
if (EPG == LOW){
digitalWrite(LPG, HIGH);
Serial.println("PGDIS"); // same logic as in the previous case
if (estado == 'A'){ // same logic as in the previous case
digitalWrite(RSIR,HIGH);
delay(1000);
}
}
if (ESB == HIGH){
digitalWrite(LSB, HIGH);
Serial.println("SBDIS"); // same logic as in the previous case
if (estado == 'A'){ // same logic as in the previous case
digitalWrite(RSIR,HIGH);
delay(1000);
}
}
}
// -----------------------------------------------------------------------------------
void Reset(){
digitalWrite(RSIR,LOW); // Turn off Siren
digitalWrite(LPC,LOW); // Turn off LED
digitalWrite(LPG,LOW);
digitalWrite(LSB,LOW);
int a=0;
Pestaneo(a);
Lectura();
}