Hello, I am building an alarm clock using a lcd, keypad, and rtc. The objective is when the alarm goes off, you have to enter the current time and a passcode via the keypad to turn off the alarm. So far, everything works as intended, however when the alarm sounds the keypad inputs don't register.
Here is the code. Any help is appreciated.
/*
* Alarm Clock
* Displays Date and Time On LCD Screen
* Enter Passcode To Turn Off Alarm
*/
#include <Time.h>
#include <Wire.h>
#include <DS1307RTC.h>
#include <LiquidCrystal.h>
#include <Tone.h>
#include <Keypad.h>
// 4-Digit passcode to turn off the alarm
int PassCodeDigit1 = 1;
int PassCodeDigit2 = 2;
int PassCodeDigit3 = 3;
int PassCodeDigit4 = 4;
// Varibales to hold time entry passcode
int timeEntry1;
int timeEntry2;
int timeEntry3;
int timeEntry4;
int passEntry1;
int passEntry2;
int passEntry3;
int passEntry4;
//Variables to count the passcode and time digit
int tCounter = 1;
int pCounter = 1;
//Variable for final time/passcode entry
int passcodeHrs;
int passcodeMins;
//Alarm time
int alarmHr = 17;
int alarmMin = 33;
// Initialize keypad
const byte ROWS = 4; //four rows
const byte COLS = 3; //four columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'#','0','*'}
};
byte rowPins[ROWS] = {14, 15, 16, 17};
byte colPins[COLS] = {10, 9, 8};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
char key = keypad.getKey();
KeypadState getState();
// Initialize LCD
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// Initialize Tone
Tone alarmTone;
// Enter date, time, and passcode when alarm sounds
boolean enterTime = false;
boolean enterDate = false;
boolean enterPass = false;
// Alarm status
boolean alarmOn = false;
// Time without delay
long interval = 1000;
long previousMillis = 0;
unsigned long currentMillis = millis();
void setup() {
Serial.begin(9600);
setSyncProvider(RTC.get); // the function to get the time from the RTC
if(timeStatus() != timeSet)
Serial.println("Unable to sync with the RTC");
else
Serial.println("RTC has set the system time");
lcd.begin(16,2);
lcd.clear();
alarmTone.begin(6);
}
void loop(){
digitalClockDisplay();
alarmCheck();
delay(1000);
/* This doesnt seem to want to work, also overflow issues so delay is used
if(currentMillis - previousMillis > interval){
digitalClockDisplay();
alarmCheck();
previousMillis = currentMillis;
} */
}
//Disply the time on the LCD
void digitalClockDisplay(){
setSyncProvider(RTC.get);
printHours(hour());
printMinutes(minute());
printSeconds(second());
}
//How to print hours
void printHours(int hrs){
lcd.setCursor(4,0);
if(hrs < 10){
lcd.print("0");
lcd.setCursor(5,0);
lcd.print(hrs);
}
else{
lcd.print(hrs);
}
}
//How to print mins
void printMinutes(int mins){
lcd.setCursor(6,0);
lcd.print(":");
lcd.setCursor(7,0);
if(mins < 10){
lcd.print('0');
lcd.setCursor(8,0);
lcd.print(mins);
}
else{
lcd.print(mins);
}
}
//How to print secs
void printSeconds(int secs){
lcd.setCursor(9,0);
lcd.print(":");
lcd.setCursor(10,0);
if(secs < 10){
lcd.print("0");
lcd.setCursor(11,0);
lcd.print(secs);
}
else{
lcd.print(secs);
}
}
//Check if the alam time == curent time
void alarmCheck(){
lcd.setCursor(5,1);
if(alarmHr == hour() && alarmMin == minute()){
alarmOn = true;
}
if(alarmOn == true){
alarm();
}
}
//Sound the alarm
void alarm(){
if (alarmOn == true){
alarmTone.play(NOTE_A4);
enterTime = true;
TimeEntry();
}
}
//Enter current time. hours and minutes
void TimeEntry(){
if(enterTime == true){
digitalClockDisplay();
lcd.setCursor(0,1);
lcd.print("Enter Time");
lcd.setCursor(11,1);
lcd.blink();
tCounter = 1;
if (tCounter == 1){
if (key != NO_KEY){
timeEntry1 = key; //First digit of time entry is the first key pressed
lcd.print(timeEntry1);
lcd.setCursor(12,1);
tCounter = 2;
}
}
else{
if (tCounter == 2){
if (key != NO_KEY){
timeEntry2 = key; //Second digit of time entry is second key pressed
lcd.print(timeEntry2);
lcd.setCursor(13,1);
lcd.print(":");
lcd.setCursor(14,1);
tCounter = 3;
}
}
else{
if(tCounter == 3){
if(key != NO_KEY){
timeEntry3 = key; //etc.
lcd.print(timeEntry3);
lcd.setCursor(15,1);
tCounter = 4;
}
}
else{
if(tCounter == 4){
if(key != NO_KEY){
timeEntry4 = key;
lcd.print(timeEntry4);
lcd.noBlink();
tCounter = 1;
}
}
}
}
}
}
if (timeEntry1 == 0){
passcodeHrs = timeEntry2;
}
else{
passcodeHrs = (timeEntry1 * 10) + timeEntry2;
}
if (timeEntry3 == 0){
passcodeMins = timeEntry4;
}
else{
passcodeMins = (timeEntry3 * 10) + timeEntry4;
}
if (passcodeHrs == hour() && passcodeMins == minute()){
enterPass = true;
PassEntry();
}
else{
return;
}
}
void PassEntry(){
if (enterPass == true){
lcd.clear();
digitalClockDisplay();
lcd.setCursor(1,1);
lcd.print("Passcode");
lcd.setCursor(9,1);
lcd.blink();
if (pCounter == 1){
if (key != NO_KEY){
passEntry1 = key; //Same idea as time entry
lcd.print("*");
lcd.setCursor(10,1);
pCounter = 2;
}
}
else{
if (pCounter == 2){
if (key != NO_KEY){
passEntry2 = key;
pCounter = 3;
lcd.print("*");
lcd.setCursor(11,1);
}
}
else{
if (pCounter == 3){
if (key != NO_KEY){
passEntry3 = key;
pCounter = 4;
lcd.print("*");
lcd.setCursor(12,1);
}
}
else{
if (pCounter == 4){
if (key != NO_KEY){
passEntry4 = key;
pCounter = 1;
lcd.print("*");
lcd.noBlink();
}
}
}
}
}
}
if (passEntry1 == PassCodeDigit1 && passEntry2 == PassCodeDigit2 && passEntry3 == PassCodeDigit3 && passEntry4 == PassCodeDigit4){
alarmOn = false;
lcd.clear();
}
else{
return;
}
}