#include <Arduino.h>
#include <uRTCLib.h>
#include <LiquidCrystal.h>
uRTCLib rtc(0x68); // Initialize RTC
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // Initialize the 16x2 LCD with specific pins
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
const int ledPin = 10; // Pin for the LED
//const int motorPin = 11; // Pin for the vibration motor
//const int buzzerPin = 12; // Pin for the buzzer
const int buttonPin = 7; // Pin for the push button
int mode = 0; // Current mode (0-3)
void setup() {
Serial.begin(9600);
URTCLIB_WIRE.begin(); // Initialize the RTC
lcd.begin(16, 2); // Initialize the LCD (16 columns and 2 rows)
lcd.backlight(); // Turn on the LCD backlight
pinMode(ledPin, OUTPUT); // Set LED pin as output
pinMode(motorPin, OUTPUT); // Set motor pin as output
pinMode(buzzerPin, OUTPUT); // Set buzzer pin as output
pinMode(buttonPin, INPUT_PULLUP); // Set button pin as input with internal pull-up
// Initialize devices to off
digitalWrite(ledPin, LOW);
digitalWrite(motorPin, LOW);
digitalWrite(buzzerPin, LOW);
}
void loop() {
rtc.refresh(); // Refresh the RTC to get the current time
// Display the current date and time on the LCD
lcd.setCursor(0, 0);
lcd.print(rtc.day());
lcd.print('/');
lcd.print(rtc.month());
lcd.print('/');
lcd.print(rtc.year());
lcd.print(" (");
lcd.print(daysOfTheWeek[(rtc.dayOfWeek() + 5) % 7]);
lcd.print(") ");
lcd.setCursor(0, 1);
lcd.print(rtc.hour());
lcd.print(':');
lcd.print(rtc.minute());
lcd.print(':');
if (rtc.second() < 10) {
lcd.print(0);
}
lcd.println(rtc.second());
// Button press handling: Change mode on button press
if (digitalRead(buttonPin) == LOW) {
mode++; // Increment mode
if (mode > 4) { // If mode exceeds 3, reset to 0
mode = 1;
}
delay(500); // Simple debounce: wait for 500ms before next press
Serial.print("Mode changed to: ");
Serial.println(mode);
}
// Handle mode-based actions
if (mode == 1) { // Mode 1: Monday to Friday 7:00 AM (Wake up early mode)
if (rtc.hour() == 7 && rtc.minute() == 0 && rtc.dayOfWeek() >= 1 && rtc.dayOfWeek() <= 5) {
activateAlarm();
} else {
deactivateAlarm();
}
}
else if (mode == 2) { // Mode 2: Monday to Friday 10:00 PM (Sleep reminder)
if (rtc.hour() == 22 && rtc.minute() == 0 && rtc.dayOfWeek() >= 1 && rtc.dayOfWeek() <= 5) {
activateAlarm();
} else {
deactivateAlarm();
}
}
else if (mode == 3) { // Mode 3: Saturday and Sunday 10:00 AM (Holiday mode)
if (rtc.hour() == 10 && rtc.minute() == 0 && (rtc.dayOfWeek() == 6 || rtc.dayOfWeek() == 0)) {
activateAlarm();
} else {
deactivateAlarm();
}
}
else if (mode == 4) { // Mode 4: Sunday to Monday 9:00 PM (Sleep early mode)
if (rtc.hour() == 21 && rtc.minute() == 0 && (rtc.dayOfWeek() == 0 || rtc.dayOfWeek() == 1)) {
activateAlarm();
} else {
deactivateAlarm();
}
}
delay(1000); // Wait for 1 second before refreshing the LCD
}
// Function to activate alarm (LED flashes, motor vibrates, buzzer sounds)
void activateAlarm() {
Serial.println("Alarm triggered!");
// Flash LED
digitalWrite(ledPin, HIGH);
digitalWrite(motorPin, HIGH);
digitalWrite(buzzerPin, HIGH);
delay(500); // Flash for 500ms
digitalWrite(ledPin, LOW);
digitalWrite(motorPin, LOW);
digitalWrite(buzzerPin, LOW);
delay(500); // Flash off for 500ms
}
// Function to deactivate alarm (turn off LED, motor, and buzzer)
void deactivateAlarm() {
digitalWrite(ledPin, LOW);
digitalWrite(motorPin, LOW);
digitalWrite(buzzerPin, LOW);
}