0% found this document useful (0 votes)
22 views2 pages

Arduino Alarm Clock with LCD Display

Uploaded by

june cadayona
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views2 pages

Arduino Alarm Clock with LCD Display

Uploaded by

june cadayona
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include <Wire.

h>
#include <LiquidCrystal_I2C.h>
#include <DS1302.h>

// Initialize LCD with address 0x3F, 16 characters, and 2 lines


LiquidCrystal_I2C lcd(0x27F 16, 2);

int Hour, Min;


int pset = 8; // pushbutton for setting alarm
int phour = 9; // pushbutton for hour
int pmin = 10; // pushbutton for minutes
int pexit = 11; // pushbutton for exit of set alarm
int buzzer = 6; // buzzer pin

int h = 0; // Alarm hour


int m = 0; // Alarm minute
int activate = 0; // Alarm setting mode flag

DS1302 rtc(2, 3, 4); // DS1302 RTC with data, clk, and reset pins

void setup() {
pinMode(pset, INPUT);
pinMode(phour, INPUT);
pinMode(pmin, INPUT);
pinMode(pexit, INPUT);
pinMode(buzzer, OUTPUT);

// Set the clock to run mode, and disable write protection


rtc.halt(false);
rtc.writeProtect(false);

// Setup LCD to 16x2 characters


lcd.begin();
lcd.backlight();

// Uncomment the following lines to initialize the DS1302 with specific values
rtc.setDOW(SATURDAY); // Set Day-of-Week to SATURDAY
rtc.setTime(10, 0, 0); // Set time to 10:00:00 (24hr format)
rtc.setDate(11, 11, 2017); // Set date to 11/11/2017
}

void loop() {
if (activate == 0) {
// Display current time
lcd.setCursor(0, 0);
lcd.print("Time: ");
lcd.setCursor(6, 0);
lcd.print(rtc.getTimeStr());

// Display date
lcd.setCursor(0, 1);
lcd.print("Date: ");
lcd.setCursor(6, 1);
lcd.print(rtc.getDateStr());

// Read current time


Time t = rtc.getTime();
Hour = t.hour;
Min = t.min;
// Check if setting button is pressed
if (digitalRead(pset) == HIGH) {
activate = 1;
lcd.clear();
}

// Check if alarm time matches current time


if (Hour == h && Min == m) {
tone(buzzer, 400);
} else {
noTone(buzzer);
}

delay(1000); // Update every second


}
else if (activate == 1) {
// Alarm setting mode
lcd.setCursor(0, 0);
lcd.print("Set Alarm");
lcd.setCursor(0, 1);
lcd.print("Hour:");
lcd.setCursor(9, 1);
lcd.print("Min:");

// Set hours
if (digitalRead(phour) == HIGH) {
h = (h + 1) % 24; // Increment hour and wrap around at 23
lcd.setCursor(5, 1);
lcd.print(h < 10 ? "0" : ""); // Leading zero for single-digit hours
lcd.print(h);
delay(200); // Debounce delay
}

// Set minutes
if (digitalRead(pmin) == HIGH) {
m = (m + 1) % 60; // Increment minute and wrap around at 59
lcd.setCursor(13, 1);
lcd.print(m < 10 ? "0" : ""); // Leading zero for single-digit minutes
lcd.print(m);
delay(200); // Debounce delay
}

// Exit alarm setting mode


if (digitalRead(pexit) == HIGH) {
activate = 0;
lcd.clear();
}
}
}

You might also like