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 "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 ...
}
}

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