#include <Wire.h> // Include library for I2C communication
#include <RTClib.h> // Include library for RTC module
RTC_DS1307 rtc; // Create an RTC object
// Constants for serial data format
char startChar = '*'; // Start delimiter
char endChar = '#'; // End delimiter
// Arrays to store time slots
char slot1[6] = "";
char slot2[6] = "";
char slot3[6] = "";
char slot4[6] = "";
// LED pins
int led1 = 12;
int led2 = 11;
int led3 = 10;
int led4 = 9;
int button = 8;
int draw = 7;
int buzzer = 6;
// Variables to process incoming serial data
String incomingData = "";
int comma1, comma2, comma3, comma4, comma5;
void setup()
{
Serial.begin(9600); // Initialize serial communication
rtc.begin(); // Initialize RTC
// Set LED pins as output
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(draw, OUTPUT);
pinMode(buzzer, OUTPUT);
pinMode(button, INPUT);
}
void loop()
{
DateTime now = rtc.now(); // Get the current time from the RTC
char currenttime[9]; // Array to store formatted current time
sprintf(currenttime, "%02d:%02d", now.hour(), now.minute()); // Format time as HH:MM
delay(1000); // Short delay
Serial.println(currenttime); // Print current time to serial
delay(5000); // Delay for better readability
// Check if data is available on serial
if (Serial.available() > 0)
{
incomingData = Serial.readStringUntil(endChar); // Read data until end character
// Process data if it starts with the correct start character
if (incomingData[0] == startChar)
{
// Find positions of commas to split the data
comma1 = incomingData.indexOf(',', 1);
comma2 = incomingData.indexOf(',', comma1 + 1);
comma3 = incomingData.indexOf(',', comma2 + 1);
comma4 = incomingData.indexOf(',', comma3 + 1);
comma5 = incomingData.indexOf(',', comma4 + 1);
// Extract and store slot times
incomingData.substring(comma1 + 1, comma2).toCharArray(slot1, 6);
incomingData.substring(comma2 + 1, comma3).toCharArray(slot2, 6);
incomingData.substring(comma3 + 1, comma4).toCharArray(slot3, 6);
incomingData.substring(comma4 + 1, comma5).toCharArray(slot4, 6);
// Print separated values for verification
Serial.println("Separated Values:");
Serial.print("SLOT 1: ");
Serial.println(slot1);
Serial.print("SLOT 2: ");
Serial.println(slot2);
Serial.print("SLOT 3: ");
Serial.println(slot3);
Serial.print("SLOT 4: ");
Serial.println(slot4);
}
}
// Check each slot time against the current time and activate LEDs if matched
// Check for slot 1
if (strcmp(slot1, currenttime) == 0)
{
digitalWrite(led1, HIGH); // Turn on LED 1
delay(5000); // Keep it on for 5 seconds
digitalWrite(led1, LOW); // Turn off LED 1
}
// Check for slot 2
if (strcmp(slot2, currenttime) == 0)
{
digitalWrite(led2, HIGH); // Turn on LED 2
delay(2000); // Keep it on for 2 seconds
digitalWrite(led2, LOW); // Turn off LED 2
}
// Check for slot 3
if (strcmp(slot3, currenttime) == 0)
{
digitalWrite(led3, HIGH); // Turn on LED 3
delay(2000); // Keep it on for 2 seconds
digitalWrite(led3, LOW); // Turn off LED 3
}
// Check for slot 4
if (strcmp(slot4, currenttime) == 0)
{
digitalWrite(led4, HIGH); // Turn on LED 4
delay(2000); // Keep it on for 2 seconds
digitalWrite(led4, LOW); // Turn off LED 4
}
// Check if the current time matches any slot and wait for button press
if (strcmp(slot1, currenttime) == 0 || strcmp(slot2, currenttime) == 0 ||
strcmp(slot3, currenttime) == 0 || strcmp(slot4, currenttime) == 0)
{
digitalWrite(buzzer,HIGH);
// Wait for the button to be pressed
if (digitalRead(button) == LOW) // Button is pressed
{
digitalWrite(draw, HIGH); // Turn on the 5th LED
delay(3000); // Keep it on for 3 seconds
digitalWrite(draw, LOW); // Turn off the 5th LED
}
digitalWrite(buzzer,LOW);
}
}