Hardware components | ||||||
| × | 1 | ||||
| × | 1 | ||||
| × | 1 | ||||
| × | 1 | ||||
| × | 1 | ||||
| × | 1 | ||||
| × | 23 | ||||
Software apps and online services | ||||||
|
Youtube tutorial:Notes:
Read moreWhen downloading the code remember to download the code twice the first time with this line not commented out and the second time with it commented out.
//clock.setDateTime(__DATE__, __TIME__);
Notes for Downloading Code:When downloading the code make a new sketch and add 2 new tabs and paste each sketch of the code into these tabs.
#include <Wire.h>
#include <DS3231.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
DS3231 clock;
RTCDateTime dt;
int buzzer = 2; //Alarm
int button = 4; //Snooze
int button_read;
bool alarm_set;
bool alarm_beep;
String alarm_hour;
String alarm_min;
String alarm_AM_PM;
int time_hour;
String time_AM_PM;
int i; //Frequency of Alarm
void setup() {
lcd.begin(16, 2);
Serial.begin(9600);
pinMode(buzzer, OUTPUT);
pinMode(button, INPUT_PULLUP);
clock.begin();
//clock.setDateTime(__DATE__, __TIME__);
lcd.setCursor(6, 1);
lcd.print(" Set Alarm");
Serial.print("Enter Hour of Alarm");
Serial.println();
alarm_set = false;
alarm_beep = true;
}
void loop() {
dt = clock.getDateTime();
if (alarm_hour == 0) {
if (Serial.available() > 0) {
alarm_hour = Serial.readString();
Serial.println("Alarm Hour Set: " + alarm_hour);
lcd.clear();
Serial.print("Enter Minute of Alarm");
Serial.println();
Serial.end();
Serial.begin(9600);
}
}
if (alarm_min == 0) {
if (Serial.available() > 0) {
alarm_min = Serial.readString();
Serial.println("Alarm Minute Set: " + alarm_min);
lcd.clear();
Serial.print("Enter Ante Meridiem of Alarm");
Serial.println();
Serial.end();
Serial.begin(9600);
}
}
if (alarm_AM_PM == 0) {
if (Serial.available() > 0) {
alarm_AM_PM = Serial.readString();
Serial.println("Alarm Ante Meridiem Set: " + alarm_AM_PM);
lcd.clear();
Serial.print("Alarm Set!");
Serial.println();
Serial.end();
Serial.begin(9600);
alarm_set = true;
}
}
Time_Display();
Alarm_Display();
if ((alarm_beep == true) && (alarm_set == true) && (alarm_min.toInt() == dt.minute) && (alarm_hour.toInt() == time_hour) && (time_AM_PM == alarm_AM_PM)) {
for (i = 0; i < 80; i++)
{
digitalWrite(buzzer, HIGH);
delay(1);
digitalWrite(buzzer, LOW);
delay(1);
Time_Display();
}
for (i = 0; i < 100; i++)
{
digitalWrite(buzzer, HIGH);
delay(2);
digitalWrite(buzzer, LOW);
delay(2);
Time_Display();
}
button_read = digitalRead(button);
if (button_read == LOW) {
alarm_beep = false;
Time_Display();
}
}
}
void Alarm_Display() {
Time_Display();
lcd.setCursor(0, 1);
lcd.print("Alarm:");
if (alarm_set == true) {
if (alarm_hour.toInt() > 9) {
lcd.setCursor(6, 1);
lcd.print(alarm_hour.toInt());
}
else {
lcd.setCursor(7, 1);
lcd.print(alarm_hour.toInt());
}
lcd.setCursor(8, 1);
lcd.print(":");
if (alarm_min.toInt() > 9) {
lcd.setCursor(9, 1);
lcd.print(alarm_min.toInt());
}
else {
lcd.setCursor(9, 1);
lcd.print("0");
lcd.setCursor(10, 1);
lcd.print(alarm_min.toInt());
}
lcd.setCursor(12, 1);
lcd.print(alarm_AM_PM);
lcd.setCursor(11, 1);
lcd.print(" ");
lcd.setCursor(14, 1);
lcd.print(" ");
}
Time_Display();
}
void Time_Display() {
lcd.setCursor(0, 0);
lcd.print("Time:");
if (dt.hour > 12) {
time_hour = (dt.hour - 12);
if (time_hour > 9) {
lcd.setCursor(6, 0);
lcd.print(time_hour);
}
else {
lcd.setCursor(7, 0);
lcd.print(time_hour);
}
lcd.setCursor(8, 0);
lcd.print(":");
if (dt.minute > 9) {
lcd.setCursor(9, 0);
lcd.print(dt.minute);
}
else {
lcd.setCursor(9, 0);
lcd.print("0");
lcd.setCursor(10, 0);
lcd.print(dt.minute);
}
if (dt.hour != 12) {
lcd.setCursor(12, 0);
lcd.print("PM\n");
time_AM_PM = "PM\n";
}
else {
lcd.setCursor(12, 0);
lcd.print("AM\n");
time_AM_PM = "AM\n";
}
}
else {
time_hour = dt.hour;
if (dt.hour > 9) {
lcd.setCursor(6, 0);
lcd.print(dt.hour);
}
else {
lcd.setCursor(7, 0);
lcd.print(dt.hour);
}
lcd.setCursor(8, 0);
lcd.print(":");
if (dt.minute > 9) {
lcd.setCursor(9, 0);
lcd.print(dt.minute);
}
else {
lcd.setCursor(9, 0);
lcd.print("0");
lcd.setCursor(10, 0);
lcd.print(dt.minute);
}
if (dt.hour != 12) {
lcd.setCursor(12, 0);
lcd.print("AM\n");
time_AM_PM = "AM\n";
}
else {
lcd.setCursor(12, 0);
lcd.print("PM\n");
time_AM_PM = "PM\n";
}
}
lcd.setCursor(11, 0);
lcd.print(" ");
lcd.setCursor(14, 0);
lcd.print(" ");
}
Comments