diff --git a/door-sensor/door-sensor.ino b/door-sensor/door-sensor.ino index bf8244a..f1f078b 100644 --- a/door-sensor/door-sensor.ino +++ b/door-sensor/door-sensor.ino @@ -1,5 +1,6 @@ #include #include "melody.h" +#include "ledindicator.h" #define IR_RECEIVE_PIN 7 @@ -15,46 +16,72 @@ const int DOOR_SENSOR_PIN = 13; * Led Receptor: 7 & Gnd & 5V */ - - - int currentDoorState; // current state of door sensor int lastDoorState; // previous state of door sensor +bool isArmed = true; +bool muted = true; void setup() { Serial.begin(115200); // // Establish serial communication + setupLeds(); IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK); // Start the receiver pinMode(DOOR_SENSOR_PIN, INPUT_PULLUP); currentDoorState = digitalRead(DOOR_SENSOR_PIN); // read state + isArmed = currentDoorState == LOW; + Serial.println("Alarm is " + String(isArmed ? "Armed" : "Disarmed")); if(useTimerFreeTone){ Serial.println("configure to use timer free tone"); } + + showAlarm(false); + + } int lastCmd; unsigned long lastTime; +void showAlarm(bool fired) +{ + if(fired) + { + showRed(); + return; + } + if(isArmed) + { + showBlue(); + }else{ + showGreen(); + } +} + void loop() { handleDoor(); + handleIR(); + +} + +void handleIR(){ if (IrReceiver.decode()) { - // Serial.println(IrReceiver.decodedIRData.decodedRawData, HEX); // Print "old" raw data - // IrReceiver.printIRResultShort(&Serial); // Print complete received data in one line - // IrReceiver.printIRSendUsage(&Serial); // Print the statement required to send this data - // if(lastCmd == IrReceiver.decodedIRData.command && lastTime + 2000 > millis() ) - // { - // IrReceiver.resume(); - // return; - // } + if(IrReceiver.decodedIRData.flags & IRDATA_FLAGS_IS_REPEAT) + { + IrReceiver.resume(); + return; + } switch(IrReceiver.decodedIRData.command) { case 67: Serial.println("Play"); - playTone(); + isArmed=!isArmed; + showAlarm(false); + Serial.println("Alarm is " + String(isArmed ? "Armed" : "Disarmed")); + // playTone(); break; default: Serial.print("unk: "); @@ -68,20 +95,30 @@ void loop() { IrReceiver.resume(); } - } void handleDoor(){ lastDoorState = currentDoorState; // save the last state currentDoorState = digitalRead(DOOR_SENSOR_PIN); // read new state - if (lastDoorState == LOW && currentDoorState == HIGH) { // state change: LOW -> HIGH + // state change: LOW -> HIGH + if (lastDoorState == LOW && currentDoorState == HIGH) { Serial.println("The door-opening event is detected"); - playTone(); + if(!isArmed) + { + Serial.println("Alarm is dissarmed, not fireing"); + return; + } + Serial.println("Alarm is armed, firing"); + showAlarm(true); + if(!muted) + playTone(); } else if (lastDoorState == HIGH && currentDoorState == LOW) { // state change: HIGH -> LOW Serial.println("The door-closing event is detected"); + isArmed=true; + showAlarm(false); // TODO: turn off alarm, light or send notification ... } } diff --git a/door-sensor/ledindicator.h b/door-sensor/ledindicator.h new file mode 100644 index 0000000..f666609 --- /dev/null +++ b/door-sensor/ledindicator.h @@ -0,0 +1,33 @@ +int ledRojo = 3; +int ledVerde = 5; +int ledAzul = 6; + +setupLeds(){ + pinMode(ledRojo,OUTPUT); +pinMode(ledVerde,OUTPUT); +pinMode(ledAzul,OUTPUT); +} + +void showRed(){ + digitalWrite(ledRojo,255); +digitalWrite(ledVerde,0); +digitalWrite(ledAzul,0); +} + +void showGreen(){ + digitalWrite(ledRojo,0); +digitalWrite(ledVerde,255); +digitalWrite(ledAzul,0); +} + +void showYellow(){ + digitalWrite(ledRojo,255); +digitalWrite(ledVerde,255); +digitalWrite(ledAzul,0); +} + +void showBlue(){ + digitalWrite(ledRojo,0); +digitalWrite(ledVerde,0); +digitalWrite(ledAzul,255); +} \ No newline at end of file diff --git a/door-sensor/melody.h b/door-sensor/melody.h index f4f2a93..2a27a3a 100644 --- a/door-sensor/melody.h +++ b/door-sensor/melody.h @@ -12,7 +12,10 @@ int noteDurations[] = { }; int melody2[] = { 262, 196, 196, 220, 196, 0, 247, 262 }; -int duration[] = { 250, 125, 125, 250, 250, 250, 250, 250 }; +int duration2[] = { 250, 125, 125, 250, 250, 250, 250, 250 }; + +int melody3[] = { 262, 196}; +int duration3[] = { 250, 125}; const bool useTimerFreeTone =true; @@ -21,30 +24,30 @@ const bool useTimerFreeTone =true; void playTone(){ - if(useTimerFreeTone) - { + if(useTimerFreeTone){ + for (int thisNote = 0; thisNote < 8; thisNote++) { // Loop through the notes in the array. - TimerFreeTone(TONE_PIN, melody2[thisNote], duration[thisNote]); // Play thisNote for duration. + TimerFreeTone(TONE_PIN, melody2[thisNote], duration2[thisNote]); // Play thisNote for duration. delay(50); // Short delay between notes. } return; } - // iterate over the notes of the melody: - for (int thisNote = 0; thisNote < 8; thisNote++) { + // // iterate over the notes of the melody: + // for (int thisNote = 0; thisNote < 8; thisNote++) { - // to calculate the note duration, take one second divided by the note type. - //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc. - int noteDuration = 1000 / noteDurations[thisNote]; - tone(TONE_PIN, melody[thisNote], noteDuration); + // // to calculate the note duration, take one second divided by the note type. + // //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc. + // int noteDuration = 1000 / noteDurations[thisNote]; + // tone(TONE_PIN, melody[thisNote], noteDuration); - // to distinguish the notes, set a minimum time between them. - // the note's duration + 30% seems to work well: - int pauseBetweenNotes = noteDuration * 1.30; - delay(pauseBetweenNotes); - // stop the tone playing: - noTone(TONE_PIN); - } + // // to distinguish the notes, set a minimum time between them. + // // the note's duration + 30% seems to work well: + // int pauseBetweenNotes = noteDuration * 1.30; + // delay(pauseBetweenNotes); + // // stop the tone playing: + // noTone(TONE_PIN); + // } } \ No newline at end of file