// FINAL PROJECT //
// GROUP 2 - BETCPET 3C NS //
// ALO, AQUINO AALIYAH, AQUINO KEN, ASERDANO, PENTUAN, ROSCO //
#include <RTClib.h>
#include <IRremote.h>
#include <LiquidCrystal_I2C.h>
#define NUM_LEDS 4
int relayPins[NUM_LEDS] = {17, 5, 18, 19};
bool ledStates[NUM_LEDS] = {false, false, false, false};
int onHours[NUM_LEDS] = {0, 0, 0, 0};
int onMinutes[NUM_LEDS] = {0, 0, 0, 0};
int onSeconds[NUM_LEDS] = {0, 0, 0, 0};
int offHours[NUM_LEDS] = {0, 0, 0, 0};
int offMinutes[NUM_LEDS] = {0, 0, 0, 0};
int offSeconds[NUM_LEDS] = {0, 0, 0, 0};
#define recv 16
#define pushbutton1 15
#define pushbutton2 2
#define pushbutton3 0
#define pushbutton4 4
#define pushbutton5 12
#define pushbutton6 13
#define pushbutton7 14
#define pushbutton8 25
#define pushbutton9 26
RTC_DS1307 rtc;
IRrecv recv_pin(recv);
LiquidCrystal_I2C lcd(0x27, 20, 4);
int setHour = 0;
int setMinute = 0;
int setSecond = 0;
bool settingOnTime = false;
bool settingOffTime = false;
String formatTime(int hour, int minute, int second) {
return String(hour) + ":" + (minute < 10 ? "0" : "") + String(minute) + ":" + (second < 10 ? "0" : "") + String(second);
}
void displaySetTime() {
lcd.setCursor(0, 3);
lcd.print("Set Time: " + formatTime(setHour, setMinute, setSecond));
}
void setup() {
for (int i = 0; i < NUM_LEDS; i++) {
pinMode(relayPins[i], OUTPUT);
}
pinMode(pushbutton1, INPUT_PULLUP);
pinMode(pushbutton2, INPUT_PULLUP);
pinMode(pushbutton3, INPUT_PULLUP);
pinMode(pushbutton4, INPUT_PULLUP);
Serial.begin(115200);
lcd.init();
lcd.clear();
lcd.backlight();
recv_pin.enableIRIn();
recv_pin.blink13(true);
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
if (!rtc.isrunning()) {
Serial.println("RTC is NOT running, let's set the time!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
pinMode(pushbutton5, INPUT_PULLUP);
pinMode(pushbutton6, INPUT_PULLUP);
pinMode(pushbutton7, INPUT_PULLUP);
pinMode(pushbutton8, INPUT_PULLUP);
pinMode(pushbutton9, INPUT_PULLUP);
}
void loop() {
lcd.setCursor(0, 0);
lcd.print("Status:");
// Handle IR remote input
if(recv_pin.decode()) {
String response = String(recv_pin.decodedIRData.command);
// Handle IR remote input
if(recv_pin.decode()) {
String response = String(recv_pin.decodedIRData.command);
// Handle IR remote commands for each LED
for (int i = 0; i < NUM_LEDS; i++) {
if (response == String(48 + i)) {
ledStates[i] = !ledStates[i];
digitalWrite(relayPins[i], ledStates[i] ? HIGH : LOW);
lcd.setCursor(10 + (i * 7), i % 2); // Adjust the LCD cursor placement
lcd.print(ledStates[i] ? "ON " : "OFF");
}
}
}
// Handle IR remote power off command
if (response == "162") {
for (int i = 0; i < NUM_LEDS; i++) {
ledStates[i] = false;
digitalWrite(relayPins[i], LOW);
lcd.setCursor(10 + (i * 7), i % 2); // Adjust the LCD cursor placement
lcd.print("OFF");
}
}
// Handle IR remote test command
if (response == "34") {
for (int i = 0; i < NUM_LEDS; i++) {
ledStates[i] = !ledStates[i];
digitalWrite(relayPins[i], ledStates[i] ? HIGH : LOW);
lcd.setCursor(10 + (i * 7), i % 2); // Adjust the LCD cursor placement
lcd.print(ledStates[i] ? "ON " : "OFF");
}
}
recv_pin.resume();
}
// Handle pushbutton states for each LED
for (int i = 0; i < NUM_LEDS; i++) {
if (digitalRead(pushbutton1 + i) == LOW) {
ledStates[i] = !ledStates[i];
digitalWrite(relayPins[i], ledStates[i] ? HIGH : LOW);
lcd.setCursor(10 + (i * 7), i % 2); // Adjust the LCD cursor placement
lcd.print(ledStates[i] ? "ON " : "OFF");
delay(500); // Add a small delay to debounce the button
}
}
// Handle setting time
if (digitalRead(pushbutton5) == LOW) {
setHour++;
if (setHour > 23) {
setHour = 0;
}
displaySetTime();
delay(500); // Add a small delay to debounce the button
}
if (digitalRead(pushbutton6) == LOW) {
setMinute++;
if (setMinute > 59) {
setMinute = 0;
}
displaySetTime();
delay(500); // Add a small delay to debounce the button
}
if (digitalRead(pushbutton7) == LOW) {
setSecond++;
if (setSecond > 59) {
setSecond = 0;
}
displaySetTime();
delay(500); // Add a small delay to debounce the button
}
// Handle confirming off time
if (digitalRead(pushbutton8) == LOW) {
for (int i = 0; i < NUM_LEDS; i++) {
offHours[i] = setHour;
offMinutes[i] = setMinute;
offSeconds[i] = setSecond;
}
settingOffTime = true;
settingOnTime = false;
lcd.setCursor(0, 3);
lcd.print("Off Time Set: " + formatTime(offHours[0], offMinutes[0], offSeconds[0]));
delay(500); // Add a small delay to debounce the button
}
// Handle confirming on time
if (digitalRead(pushbutton9) == LOW) {
for (int i = 0; i < NUM_LEDS; i++) {
onHours[i] = setHour;
onMinutes[i] = setMinute;
onSeconds[i] = setSecond;
}
settingOnTime = true;
settingOffTime = false;
lcd.setCursor(0, 3);
lcd.print("On Time Set: " + formatTime(onHours[0], onMinutes[0], onSeconds[0]));
delay(500); // Add a small delay to debounce the button
}
DateTime time = rtc.now();
// Handle turning off for each LED
for (int i = 0; i < NUM_LEDS; i++) {
if (time.hour() == offHours[i] && time.minute() == offMinutes[i] && time.second() == 0) {
ledStates[i] = false;
digitalWrite(relayPins[i], LOW);
lcd.setCursor(10 + (i * 7), i / 2);
lcd.print("OFF");
}
}
// Handle turning on for each LED
for (int i = 0; i < NUM_LEDS; i++) {
if (time.hour() == onHours[i] && time.minute() == onMinutes[i] && time.second() == 0) {
ledStates[i] = true;
digitalWrite(relayPins[i], HIGH);
lcd.setCursor(10 + (i * 7), i / 2);
lcd.print("ON ");
}
}
// Display Date on the second row
lcd.setCursor(0, 2);
lcd.print("Date: ");
lcd.print(time.timestamp(DateTime::TIMESTAMP_DATE));
// Display Time on the third row
lcd.setCursor(0, 3);
lcd.print("Time: ");
lcd.print(time.timestamp(DateTime::TIMESTAMP_TIME));
}