Hello everyone,
I'm attempting to make an alarm clock using the Arduino Uno, and In my program, I made it so that when the alarm is set, and when the clock reaches the alarm time, the alarm goes off.
My goal is to make it so that when the buzzer sounds play and the sequence for the RGB module is shown, that it checks if an RFID card has been swiped. When it checks that the correct card has been swiped, the alarm turns off.
The problem I'm facing is that I can swipe the RFID Card, and it says "Alarm off" confirming that it works, but after the fact the alarm still keeps playing. After some debugging, I figured out the EQpressed variable switches back to true when it goes through the loop again into the switch statement.
Here is my Code:
//LCD code
#include <Wire.h> // Library for I2C communication
#include <LiquidCrystal_I2C.h> // Library for LCD
String content= "";
// Wiring: SDA pin is connected to A4 and SCL pin to A5.
// Connect to LCD via I2C, default address 0x27 (A0-A2 not jumpered)
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2); // Change to (0x27,20,4) for 20x4 LCD.
#include <IRremote.h> //including infrared remote header file
int hrs = 0;
int mins = 0;
//This is to check if the time has been set (EQ has been pressed)
bool EQpressed = false;
//This integer is supposed to represent the RTC's time
int alarmHrs = 0;
int alarmMins = 0;
//Remote Code
int RECV_PIN = 2; // the pin where you connect the output pin of IR sensor
IRrecv irrecv(RECV_PIN);
decode_results results;
//Buzzer Code
#include <TimerFreeTone.h>
#define TONE_PIN 3 // Pin you have speaker/piezo connected to (be sure to include a 100 ohm resistor).
//RGB Module
#define LEDR 5
#define LEDG 5
#define LEDB 4
int r = 0;
int g = 0;
int b = 0;
//RFID
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
//RTC Module
#include <virtuabotixRTC.h> //Library used
//Wiring SCLK -> 6, I/O -> 7, CE -> 8
//Or CLK -> 6 , DAT -> 7, Reset -> 8
virtuabotixRTC myRTC(6,7,8); //If you change the wiring change the pins here also
void setup() {
// put your setup code here, to run once:
myRTC.updateTime();
hrs = 0;
mins = 0;
//RFID as Push Button Sensor
SPI.begin(); // Initiate SPI bus
mfrc522.PCD_Init(); // Initiate MFRC522
//RGB Module
pinMode(LEDR, OUTPUT);
pinMode(LEDG, OUTPUT);
pinMode(LEDB, OUTPUT);
//Buzzer Code
//pinMode(buzzer, OUTPUT); // Set buzzer - pin 3 as an output
//LCD Code
lcd.init();
lcd.backlight();
lcd.begin(16,2);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Time: "); lcd.print(myRTC.hours); lcd.print(":"); lcd.print(myRTC.minutes);
lcd.setCursor(0,1);
lcd.print("Input alarm time");
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Time: "); lcd.print(myRTC.hours); lcd.print(":"); lcd.print(myRTC.minutes);
lcd.setCursor(0,1);
lcd.print("CH+/CH- for mins");
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Time: "); lcd.print(myRTC.hours); lcd.print(":"); lcd.print(myRTC.minutes);
lcd.setCursor(0,1);
lcd.print("+/- for hrs");
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Time: "); lcd.print(myRTC.hours); lcd.print(":"); lcd.print(myRTC.minutes);
//IR Remote
irrecv.enableIRIn();
}
void loop() {
// put your main code here, to run repeatedly:
uint32_t readResults = results.value;// Results of decoding are stored in result.value
//Remote input code
if (irrecv.decode(&results)){// Returns 0 if no data ready, 1 if data ready.
int readResults = results.value;// Results of decoding are stored in result.value
Serial.println( results.value);
switch ( results.value) {
case 16754775 :
{
//VOL+
hrs +=1;
//Makes sure user doesn't input invalid hours
if(hrs > 23){
hrs = 0;
}
delay(100);
break;
}
case 16769055 :
{
//VOL-
hrs -=1;
//Makes sure user doesn't input invalid hours
if(hrs < 0){
hrs = 23;
}
delay(100);
break;
}
case 16769565 :
{
//CH+
mins +=1;
//Makes sure user doesn't input invalid minutes
if(mins > 59){
mins = 0;
}
delay(100);
break;
}
case 16753245 :
{
//CH-
mins -=1;
//Makes sure user doesn't input invalid minutes
if(mins < 0){
mins = 59;
}
break;
}
case 16738455 : //This case is only for this TinkerCad Simulation and represents the RTC hitting the desired alarm time.
{
alarmHrs = 17;
alarmMins = 20;
}
case 16748655 :
{
EQpressed = true;
//EQ
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Time: "); lcd.print(myRTC.hours); lcd.print(":"); lcd.print(myRTC.minutes);
lcd.setCursor(0,1);
lcd.print("Input Valid");
delay(1000);
lcd.setCursor(0,1);
lcd.print("Alarm Set for ");
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Time: "); lcd.print(myRTC.hours); lcd.print(":"); lcd.print(myRTC.minutes);
lcd.setCursor(0,1);
lcd.print(hrs); lcd.print(":"); lcd.print(mins);
delay(1000);
results.value = 0;
break;
}
}//End of Switch Statement
irrecv.resume(); // Restart the ISR state machine and Receive the next value
}//End of If statement
myRTC.updateTime();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Time: "); lcd.print(myRTC.hours); lcd.print(":"); lcd.print(myRTC.minutes);
lcd.setCursor(0,1);
lcd.print(hrs); lcd.print(":"); lcd.print(mins);
delay(1000);
//Alarm code
if( hrs == myRTC.hours && mins == myRTC.minutes && EQpressed == true ){
myRTC.updateTime();
lcd.setCursor(0,0);
lcd.print("Time: "); lcd.print(myRTC.hours); lcd.print(":"); lcd.print(myRTC.minutes);
TimerFreeTone( 3, 1000, 1000 , 10 );
TimerFreeTone( 3, 800, 1000 , 10 );
TimerFreeTone( 3, 600, 1000 , 10 );
TimerFreeTone( 3, 400, 1000 , 10 );
TimerFreeTone( 3, 600, 1000 , 10 );
TimerFreeTone( 3, 800, 1000 , 10 );
TimerFreeTone( 3, 1000, 1000 , 10 );
myRTC.updateTime();
lcd.setCursor(0,0);
lcd.print("Time: "); lcd.print(myRTC.hours); lcd.print(":"); lcd.print(myRTC.minutes);
//RGB Light Pattern
r=255;
g = 0;
b = 0;
analogWrite(LEDG, r);
analogWrite(LEDG, g);
analogWrite(LEDB, b);
delay(1000);
r-0;
g = 255;
b =0;
analogWrite(LEDG, r);
analogWrite(LEDG, g);
analogWrite(LEDB, b);
delay(1000);
r=0;
g = 0;
b =255;
analogWrite(LEDG, r);
analogWrite(LEDG, g);
analogWrite(LEDB, b);
delay(1000);
}
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
//String content= "";
byte letter;
for (byte i = 0; i < mfrc522.uid.size; i++)
{
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte[i], HEX));
}
content.toUpperCase();
Serial.println( content.substring(1) );
if (content.substring(1) == "C6 E9 4C 63") //change here the UID of the card/cards that you want to give access
{
TimerFreeTone( 3, 2000, 3000 , 10 );
EQpressed == false;
content = "";
myRTC.updateTime();
r=0;
g=0;
b=0;
analogWrite(LEDG, r);
analogWrite(LEDG, g);
analogWrite(LEDB, b);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Time: "); lcd.print(myRTC.hours); lcd.print(":"); lcd.print(myRTC.minutes);
lcd.setCursor(0,1);
lcd.print("Alarm Off");
delay(1000);
}
}