#include #include "melody.h" #define IR_RECEIVE_PIN 7 const int DOOR_SENSOR_PIN = 13; /* Connections: * Door Sensor: Gnd & 13 * Buzzer: Gnd & 8 * Led Receptor: 7 & Gnd & 5V */ int currentDoorState; // current state of door sensor int lastDoorState; // previous state of door sensor void setup() { Serial.begin(115200); // // Establish serial communication IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK); // Start the receiver pinMode(DOOR_SENSOR_PIN, INPUT_PULLUP); currentDoorState = digitalRead(DOOR_SENSOR_PIN); // read state if(useTimerFreeTone){ Serial.println("configure to use timer free tone"); } } int lastCmd; unsigned long lastTime; void loop() { handleDoor(); 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; // } switch(IrReceiver.decodedIRData.command) { case 67: Serial.println("Play"); playTone(); break; default: Serial.print("unk: "); Serial.println(IrReceiver.decodedIRData.command); } //IrReceiver.printIRResultShort(&Serial); // Print complete received data in one line // lastTime = millis(); // lastCmd=IrReceiver.decodedIRData.command; //Serial.println(IrReceiver.decodedIRData.command); 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 Serial.println("The door-opening event is detected"); playTone(); } else if (lastDoorState == HIGH && currentDoorState == LOW) { // state change: HIGH -> LOW Serial.println("The door-closing event is detected"); // TODO: turn off alarm, light or send notification ... } }