#include <Wire.h>
#include <hd44780.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
unsigned long previousMillis;
const unsigned long interval = 1000; // 1 second
int hours, minutes, seconds;
// Relay Controls
int rt = 1000;
const int RELAY_PIN = 3;
// ON & OFF Time 1
int OnRelayHour = 05;
int OnRelayMin = 00;
int OffRelayHour = 05;
int OffRelayMin = 01;
int x = OffRelayMin - OnRelayMin;
int y = 0;
// Button Int
const int pushbtn1 = 9; // LCD Backlight
const int pushbtn2 = 5; // Manual Relay activation
int btnstate1; // LCD Backlight
int btnstate2; // Manual Relay activation
void setup()
{
Wire.begin();
pinMode(pushbtn1, INPUT); // Green LCD Back light
pinMode(pushbtn2, INPUT); // Blue Manual Relay activation
pinMode(3, OUTPUT); // Manual Relay activation
Serial.begin( 9600);
Serial.setTimeout( 100); // 100ms timeout for serial input
Serial.println( "Starting...");
// LCD Code 26 Dec 2022
lcd.init();
lcd.backlight();
lcd.setCursor(1, 0);
lcd.print( "Starting...");
delay(1000);
lcd.clear();
}
void loop()
{
// LCD Backlight Button
if(digitalRead(9)==HIGH)
{
lcd.backlight();
}
if(digitalRead(9)==LOW)
{
lcd.noBacklight();
}
// Manual Relay Activation
digitalWrite(3,digitalRead(5));
// Relay on and off time
if(hours == OnRelayHour && minutes == OnRelayMin)
for (y= 0; y <x*60/7; y++)
{
digitalWrite(RELAY_PIN, HIGH);
delay(rt);
digitalWrite(RELAY_PIN, LOW);
delay(rt);
}
{
delay(rt);
digitalWrite(RELAY_PIN, LOW);
}
unsigned long currentMillis = millis();
if( Serial.available() > 0)
{
delay( 100); // wait 100ms to get whole line
int h, a, m, b, s;
h = Serial.parseInt(); // get integer with timeout
a = Serial.read(); // get character, no timeout
m = Serial.parseInt(); // get integer with timeout
b = Serial.read(); // get character, no timeout
s = Serial.parseInt(); // get integer with timeout
if( a == ':' && b == ':') // check for right format
{
hours = constrain( h, 0, 23);
minutes = constrain( m, 0, 59);
seconds = constrain( s, 0, 59);
}
else
{
Serial.println( "New time not accepted");
}
// Clear remaining characters from the serial input buffer.
while( Serial.available() > 0)
{
Serial.read();
}
}
if( currentMillis - previousMillis >= interval)
{
// When previousMillis would be set to currentMillis,
// then a delay in the code would delay the clock.
// When previousMillis is incremented with 1 second,
// then it stays synchronized.
previousMillis += interval; // increment with 1 second
seconds++;
if( seconds >= 60)
{
seconds = 0;
minutes++;
if( minutes >= 60)
{
minutes = 0;
hours++;
if( hours >= 24)
{
hours = 0;
}
}
}
// Update the time to the serial monitor.
// The format is hh:mm:ss.
// For example 09:30:55
if( hours < 10)
Serial.print( "0");
Serial.print( hours);
Serial.print( ":");
if( minutes < 10)
Serial.print( "0");
Serial.print( minutes);
Serial.print( ":");
if( seconds < 10)
Serial.print( "0");
Serial.print( seconds);
Serial.println();
// LCD code
lcd.setCursor(1,1);
if( hours < 10)
lcd.print("0");
lcd.print(hours);
lcd.print(":");
if( minutes < 10)
lcd.print( "0");
lcd.print( minutes);
lcd.print( ":");
if( seconds < 10)
lcd.print( "0");
lcd.print( seconds);
lcd.println();
// Attempt of On Off Code
}
}