feat: add led support

This commit is contained in:
Guillermo Marcel 2025-04-10 14:15:26 -03:00
parent 0d3279c943
commit 8c51bfb8e7
3 changed files with 105 additions and 32 deletions

View File

@ -1,5 +1,6 @@
#include <IRremote.hpp> #include <IRremote.hpp>
#include "melody.h" #include "melody.h"
#include "ledindicator.h"
#define IR_RECEIVE_PIN 7 #define IR_RECEIVE_PIN 7
@ -15,46 +16,72 @@ const int DOOR_SENSOR_PIN = 13;
* Led Receptor: 7 & Gnd & 5V * Led Receptor: 7 & Gnd & 5V
*/ */
int currentDoorState; // current state of door sensor int currentDoorState; // current state of door sensor
int lastDoorState; // previous state of door sensor int lastDoorState; // previous state of door sensor
bool isArmed = true;
bool muted = true;
void setup() void setup()
{ {
Serial.begin(115200); // // Establish serial communication Serial.begin(115200); // // Establish serial communication
setupLeds();
IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK); // Start the receiver IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK); // Start the receiver
pinMode(DOOR_SENSOR_PIN, INPUT_PULLUP); pinMode(DOOR_SENSOR_PIN, INPUT_PULLUP);
currentDoorState = digitalRead(DOOR_SENSOR_PIN); // read state currentDoorState = digitalRead(DOOR_SENSOR_PIN); // read state
isArmed = currentDoorState == LOW;
Serial.println("Alarm is " + String(isArmed ? "Armed" : "Disarmed"));
if(useTimerFreeTone){ if(useTimerFreeTone){
Serial.println("configure to use timer free tone"); Serial.println("configure to use timer free tone");
} }
showAlarm(false);
} }
int lastCmd; int lastCmd;
unsigned long lastTime; unsigned long lastTime;
void showAlarm(bool fired)
{
if(fired)
{
showRed();
return;
}
if(isArmed)
{
showBlue();
}else{
showGreen();
}
}
void loop() { void loop() {
handleDoor(); handleDoor();
handleIR();
}
void handleIR(){
if (IrReceiver.decode()) { if (IrReceiver.decode()) {
// Serial.println(IrReceiver.decodedIRData.decodedRawData, HEX); // Print "old" raw data if(IrReceiver.decodedIRData.flags & IRDATA_FLAGS_IS_REPEAT)
// IrReceiver.printIRResultShort(&Serial); // Print complete received data in one line {
// IrReceiver.printIRSendUsage(&Serial); // Print the statement required to send this data IrReceiver.resume();
// if(lastCmd == IrReceiver.decodedIRData.command && lastTime + 2000 > millis() ) return;
// { }
// IrReceiver.resume();
// return;
// }
switch(IrReceiver.decodedIRData.command) switch(IrReceiver.decodedIRData.command)
{ {
case 67: case 67:
Serial.println("Play"); Serial.println("Play");
playTone(); isArmed=!isArmed;
showAlarm(false);
Serial.println("Alarm is " + String(isArmed ? "Armed" : "Disarmed"));
// playTone();
break; break;
default: default:
Serial.print("unk: "); Serial.print("unk: ");
@ -68,20 +95,30 @@ void loop() {
IrReceiver.resume(); IrReceiver.resume();
} }
} }
void handleDoor(){ void handleDoor(){
lastDoorState = currentDoorState; // save the last state lastDoorState = currentDoorState; // save the last state
currentDoorState = digitalRead(DOOR_SENSOR_PIN); // read new 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"); 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(); playTone();
} }
else else
if (lastDoorState == HIGH && currentDoorState == LOW) { // state change: HIGH -> LOW if (lastDoorState == HIGH && currentDoorState == LOW) { // state change: HIGH -> LOW
Serial.println("The door-closing event is detected"); Serial.println("The door-closing event is detected");
isArmed=true;
showAlarm(false);
// TODO: turn off alarm, light or send notification ... // TODO: turn off alarm, light or send notification ...
} }
} }

View File

@ -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);
}

View File

@ -12,7 +12,10 @@ int noteDurations[] = {
}; };
int melody2[] = { 262, 196, 196, 220, 196, 0, 247, 262 }; 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; const bool useTimerFreeTone =true;
@ -21,30 +24,30 @@ const bool useTimerFreeTone =true;
void playTone(){ void playTone(){
if(useTimerFreeTone) if(useTimerFreeTone){
{
for (int thisNote = 0; thisNote < 8; thisNote++) { // Loop through the notes in the array. 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. delay(50); // Short delay between notes.
} }
return; return;
} }
// iterate over the notes of the melody: // // iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 8; thisNote++) { // for (int thisNote = 0; thisNote < 8; thisNote++) {
// to calculate the note duration, take one second divided by the note type. // // to calculate the note duration, take one second divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc. // //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000 / noteDurations[thisNote]; // int noteDuration = 1000 / noteDurations[thisNote];
tone(TONE_PIN, melody[thisNote], noteDuration); // tone(TONE_PIN, melody[thisNote], noteDuration);
// to distinguish the notes, set a minimum time between them. // // to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well: // // the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30; // int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes); // delay(pauseBetweenNotes);
// stop the tone playing: // // stop the tone playing:
noTone(TONE_PIN); // noTone(TONE_PIN);
} // }
} }