129 lines
2.7 KiB
C++
129 lines
2.7 KiB
C++
#include <IRremote.hpp>
|
|
#include "melody.h"
|
|
#include "ledindicator.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
|
|
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()) {
|
|
if(IrReceiver.decodedIRData.flags & IRDATA_FLAGS_IS_REPEAT)
|
|
{
|
|
IrReceiver.resume();
|
|
return;
|
|
}
|
|
switch(IrReceiver.decodedIRData.command)
|
|
{
|
|
case 67:
|
|
Serial.println("Play");
|
|
isArmed=!isArmed;
|
|
showAlarm(false);
|
|
Serial.println("Alarm is " + String(isArmed ? "Armed" : "Disarmed"));
|
|
// 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
|
|
|
|
// state change: LOW -> HIGH
|
|
if (lastDoorState == LOW && currentDoorState == HIGH) {
|
|
Serial.println("The door-opening event is detected");
|
|
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 ...
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|