Skip to content

Instantly share code, notes, and snippets.

@numberisnan
Created June 11, 2019 02:41
Show Gist options
  • Save numberisnan/d7b4ccf19025d96430e70d00fefdca6d to your computer and use it in GitHub Desktop.
Save numberisnan/d7b4ccf19025d96430e70d00fefdca6d to your computer and use it in GitHub Desktop.
A simple alarm clock application for Arduino
#include <Time.h>
#include <TimeLib.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
//User editable configurations
const short dismissPin = 7;
const short snoozePin = 8;
const short buzzerPin = 13;
//Don't touch these!
//Current time
short hours = 0;
short minutes = 0;
short seconds = 0;
bool AMPM = true;
//Alarm
short ahours = 0;
short aminutes = 0;
bool aAMPM = true;
bool dismissed = false;
bool alarm = false;
String formatTime(int dhours, int dminutes, int dseconds, bool AMPM) {
//Algorithm to generate time to display from minute and hours values
String hours = String(dhours);
String minutes = String(dminutes);
String seconds = String(dseconds);
if (dhours > 12 && !AMPM) {
hours = String(dhours - 12);
}
if (hours.length() < 2) {
hours = "0" + String(hours[0]);
}
if (minutes.length() < 2) {
minutes = "0" + String(minutes[0]);
}
if (seconds.length() < 2) {
seconds = "0" + String(seconds[0]);
}
return hours + ":" + minutes + " " + seconds + " " + (AMPM ? "AM" : "PM");
}
void updateSetupScreen(String message, int hours, int minutes, int seconds, bool AMPM) {
lcd.setCursor(0,0);
lcdWrite(lcd, message + formatTime(hours, minutes, seconds, AMPM));
}
void lcdWrite(LiquidCrystal lcd, String message) {
lcd.clear();
lcd.print(message);
}
void setup() {
Serial.begin(9600);
pinMode(dismissPin, INPUT);
pinMode(snoozePin, INPUT);
pinMode(buzzerPin, OUTPUT);
//Set time
bool setupComplete = false;
int mode = 1; //Which digit is being modified
String message = "Set: ";
while (!setupComplete) {
int set = digitalRead(dismissPin);
int increment = digitalRead(snoozePin);
if (set == HIGH) {
if (mode == 3) { //Message
lcdWrite(lcd, "Set Alarm");
delay(1000);
}
mode++;
delay(250);
} else if (increment == HIGH) {
switch (mode) {
case 1:
if (hours == 12) {
hours = 1;
AMPM = !AMPM;
} else {
hours++;
}
break;
case 2:
minutes += minutes <= 59 && minutes >= 50 ? -50 : 10;
break;
case 3:
minutes += String(minutes)[1] == '9' || (minutes < 10 && minutes == 9) ? -9 : 1;
break;
case 4:
if (ahours == 12) {
ahours = 1;
aAMPM = !aAMPM;
} else {
ahours++;
}
break;
case 5:
aminutes += aminutes <= 59 && aminutes >= 50 ? -50 : 10;
break;
case 6:
aminutes += String(aminutes)[1] == '9' || (aminutes < 10 && aminutes == 9) ? -9 : 1;
}
delay(50);
}
if (mode >= 7) {
setupComplete = true;
//Serial.print("ahours: " + String(ahours) + "\naminutes: " + String(aminutes) + "\naAMPM" + String(aAMPM))
} else if (mode <= 6 && mode >= 4) {
updateSetupScreen("", ahours, aminutes, 0, aAMPM);
} else {
updateSetupScreen("Set: ", hours, minutes, seconds, AMPM);
}
delay(50);
}
//Set internal time to inputted
setTime(AMPM ? hours : hours + 12, minutes, 0, 0, 0, 0);
lcdWrite(lcd,"Time set!");
delay(1000);
}
void loop() {
int d = 1000; //Delay if alarm is on
int time[] = {hour(),minute(),second(), isAM()};
lcd.setCursor(0,0);
lcd.print(formatTime(time[0], time[1], time[2], time[3]));
if (digitalRead(dismissPin) == HIGH && alarm) {
dismissed = true;
} else if (digitalRead(snoozePin) == HIGH && alarm) {
if (aminutes > 55) {
ahours++;
aminutes = 55 - aminutes;
} else {
aminutes += 5;
}
}
if (time[0] == (aAMPM ? ahours : ahours + 12) && time[1] == aminutes && time[3] == aAMPM && !dismissed) {
//Alarm
tone(buzzerPin, 3000, 50);
alarm = true;
d = 100; //Shorter delay when alarm is going to catch snooze and dismiss presses
}
delay(d);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment