arduino-sketches/door-sensor/door-sensor.ino

148 lines
3.0 KiB
Arduino
Raw Normal View History

2025-04-10 10:34:47 -03:00
#include <IRremote.hpp>
2025-04-10 10:50:54 -03:00
#include "melody.h"
2025-04-10 14:15:26 -03:00
#include "ledindicator.h"
2025-04-10 10:50:54 -03:00
2025-04-10 10:34:47 -03:00
#define IR_RECEIVE_PIN 7
2025-04-10 10:50:54 -03:00
2025-04-10 10:34:47 -03:00
const int DOOR_SENSOR_PIN = 13;
2025-04-10 10:50:54 -03:00
/* Connections:
* Door Sensor: Gnd & 13
* Buzzer: Gnd & 8
* Led Receptor: 7 & Gnd & 5V
*/
2025-04-10 10:34:47 -03:00
int currentDoorState; // current state of door sensor
int lastDoorState; // previous state of door sensor
2025-04-10 14:15:26 -03:00
bool isArmed = true;
2025-04-10 21:09:36 -03:00
bool muted = false;
bool ledOn = true;
2025-04-10 10:34:47 -03:00
void setup()
{
Serial.begin(115200); // // Establish serial communication
2025-04-10 14:15:26 -03:00
setupLeds();
2025-04-10 10:34:47 -03:00
IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK); // Start the receiver
pinMode(DOOR_SENSOR_PIN, INPUT_PULLUP);
currentDoorState = digitalRead(DOOR_SENSOR_PIN); // read state
2025-04-10 10:50:54 -03:00
2025-04-10 14:15:26 -03:00
isArmed = currentDoorState == LOW;
Serial.println("Alarm is " + String(isArmed ? "Armed" : "Disarmed"));
2025-04-10 10:50:54 -03:00
if(useTimerFreeTone){
Serial.println("configure to use timer free tone");
}
2025-04-10 14:15:26 -03:00
showAlarm(false);
2025-04-10 10:34:47 -03:00
}
int lastCmd;
unsigned long lastTime;
2025-04-10 14:15:26 -03:00
void showAlarm(bool fired)
{
2025-04-10 21:09:36 -03:00
if(!ledOn)
{
offLed();
return;
}
2025-04-10 14:15:26 -03:00
if(fired)
{
2025-04-10 21:09:36 -03:00
if(isArmed)
{
showRed();
}else{
showYellow();
}
2025-04-10 14:15:26 -03:00
return;
}
if(isArmed)
{
showBlue();
}else{
showGreen();
}
}
2025-04-10 10:34:47 -03:00
void loop() {
handleDoor();
2025-04-10 14:15:26 -03:00
handleIR();
}
void handleIR(){
2025-04-10 10:34:47 -03:00
if (IrReceiver.decode()) {
2025-04-10 14:15:26 -03:00
if(IrReceiver.decodedIRData.flags & IRDATA_FLAGS_IS_REPEAT)
{
IrReceiver.resume();
return;
}
2025-04-10 10:34:47 -03:00
switch(IrReceiver.decodedIRData.command)
{
case 67:
Serial.println("Play");
2025-04-10 14:15:26 -03:00
isArmed=!isArmed;
showAlarm(false);
Serial.println("Alarm is " + String(isArmed ? "Armed" : "Disarmed"));
// playTone();
2025-04-10 10:34:47 -03:00
break;
2025-04-10 21:09:36 -03:00
case 70:
Serial.println("Led on/off");
ledOn=!ledOn;
showAlarm(false);
break;
2025-04-10 10:34:47 -03:00
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
2025-04-10 14:15:26 -03:00
// state change: LOW -> HIGH
if (lastDoorState == LOW && currentDoorState == HIGH) {
2025-04-10 10:34:47 -03:00
Serial.println("The door-opening event is detected");
2025-04-10 14:15:26 -03:00
if(!isArmed)
{
Serial.println("Alarm is dissarmed, not fireing");
2025-04-10 21:09:36 -03:00
delay(500);
showAlarm(true);
2025-04-10 14:15:26 -03:00
return;
}
Serial.println("Alarm is armed, firing");
showAlarm(true);
if(!muted)
playTone();
2025-04-10 10:34:47 -03:00
}
else
if (lastDoorState == HIGH && currentDoorState == LOW) { // state change: HIGH -> LOW
2025-04-10 21:09:36 -03:00
delay(500);
2025-04-10 10:34:47 -03:00
Serial.println("The door-closing event is detected");
2025-04-10 14:15:26 -03:00
isArmed=true;
showAlarm(false);
2025-04-10 10:34:47 -03:00
// TODO: turn off alarm, light or send notification ...
}
}
2025-04-10 10:50:54 -03:00
2025-04-10 10:34:47 -03:00