// Deleting the given set alarm time
void deleteLastCharacter(){
if (cursorPos > 0){
cursorPos--;
if(cursorPos==2)
cursorPos=1;
}
}
// Clearing all the set alarm time
void clearArray(){
arrayIndex = 0;
for (int i = 0; i < maxInput; i++){
inputArray[i] = '\0';
}
}
// Clears the LCD
void clearLCD(){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Input");
lcd.setCursor(0, 1);
lcd.print("Cleared");
delay(2000); // Display the error message for 2 seconds
lcd.clear();
}
// Displays an Error message if the user inputs a higher number of 24-H
void displayErrorMessage() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Error Time Input");
lcd.setCursor(0, 1);
lcd.print("Only 24-H Time");
delay(2000); // Display the error message for 2 seconds
lcd.clear();
}
// Displaying the Current Time
//is this code applicable enough to make the time continuous?
void displayCurrentTime(void *currentTime) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Current Time:");
lcd.setCursor(0, 1);
lcd.print(currentTime.hour(), DEC);
lcd.print(":");
lcd.print(currentTime.minute(), DEC);
lcd.print(":");
lcd.print(currentTime.second(), DEC);
}
// Setting the Alarm Time (Where they keypad functionalities are)
void setAlarmTime(void *pvParameters) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set Alarm " + String(currentAlarm + 1) + ":");
lcd.setCursor(0, 1);
lcd.print("HH:MM");
char key = NO_KEY;
while (key != '#') {
key = keypad.getKey();
if(xSemaphoreTake(setAlarm2, portMAX_DELAY) == pdTRUE){
if (key != NO_KEY){
if(((inputBuffer[0] - '0') * 10 + (inputBuffer[1] - '0') <= 23) && (((inputBuffer[3] - '0') * 10)<= 50)){
if (key >= '0' && key <= '9'){
inputBuffer[cursorPos] = key;
lcd.setCursor(cursorPos, 1);
lcd.print(key);
cursorPos++;
if (cursorPos == 2){
cursorPos += 1;
}
if (cursorPos == 5)
break;
}
else if (key == '*') {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set Alarm " + String(currentAlarm + 1) + ":");
lcd.setCursor(0, 1);
lcd.print("HH:MM");
cursorPos = 0;
}
else if (key == 'D'){
deleteLastCharacter();
lcd.setCursor(cursorPos, 1);
}
else if (key == 'C'){
DateTime now = rtc.now();
clearArray();
clearLCD();
lcd.setCursor(0, 0);
lcd.print("Current Time:");
lcd.setCursor(0, 1);
lcd.print(now.hour(), DEC);
lcd.print(":");
lcd.print(now.minute(), DEC);
lcd.print(":");
lcd.print(now.second(), DEC);
vTaskSuspend();
}
}
else{
DateTime now = rtc.now();
displayErrorMessage();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Current Time:");
lcd.setCursor(0, 1);
lcd.print(now.hour(), DEC);
lcd.print(":");
lcd.print(now.minute(), DEC);
lcd.print(":");
lcd.print(now.second(), DEC);
}
}
xSemaphoreGive(setAlarm2);
}
}
if (cursorPos == 5) {
alarms[currentAlarm].hour = (inputBuffer[0] - '0') * 10 + (inputBuffer[1] - '0');
alarms[currentAlarm].minute = (inputBuffer[3] - '0') * 10 + (inputBuffer[4] - '0');
alarms[currentAlarm].enabled = true;
lcd.setCursor(0, 1);
lcd.print("Alarm set ");
delay(1000);
lcd.clear();
currentAlarm++;
if (currentAlarm >= 3) {
currentAlarm = 0;
}
}
lcd.setCursor(0, 0);
lcd.print("Press '*' to set");
lcd.setCursor(0, 1);
lcd.print("alarm time");
delay(3000);
cursorPos = 0;
}
// The Buzzer of the Alarm
void triggerAlarm(void *int alarmIndex) {
if(xSemaphoreTake(triggerAlarm, portMAX_DELAY) == pdTRUE){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("ALARM " + String(alarmIndex + 1) + "!");
xSemaphoreGive(triggerAlarm);
delay(2000); // Delay to show the alarm message
delay(100);
tone(buzzer, 1000, 200);
delay(100);
tone(buzzer, 2000, 200);
delay(100);
tone(buzzer, 3000, 200);
delay(100);
tone(buzzer, 4000, 200);
delay(100);
return;// in freeRTOS go back to normal when button clicked?
}
}