#include <LiquidCrystal_I2C.h>
#include <RTClib.h>
#include <Wire.h>
#include "OneButton.h"
#include <Adafruit_NeoPixel.h>
#define LED_PIN 7
#define LED_COUNT 16 // change number of leds here
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
LiquidCrystal_I2C lcd(0x27,16,2);
RTC_DS1307 RTC;
int hours = 0;
int minutes = 0;
bool cursorBlink = false;
bool settingHours = false;
unsigned long previousMillis1 = 0;
const long interval1 = 500; // cursor blink time
unsigned long total_time=0,set_time=0;
int brightness=50;
OneButton set(4, true,true);
OneButton up(3, true,true);
OneButton down(2, true,true);
int set_hours=0,set_min=0;
int rtc_hour=0,rtc_minute=0;
bool started=false;
enum states{
RTC_TIME,
LIGHT_TIME,
LIGHT_SET,
SET_TIME,
WORKING
} current;
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
strip.begin();
strip.show();
RTC.begin();
up.attachClick(upchange);
down.attachClick(downchange);
set.attachClick(setchange);
set.attachDuringLongPress(longset);
current=RTC_TIME;
set.setLongPressIntervalMs(3000); //long press time in milliseconds
// RTC.adjust(DateTime(2022,9,30,18,13,40)); // uncomment this line with the updated time when want to set
}
void loop() {
brightness=analogRead(A0);
brightness=map(brightness,8,1015,0,255); // change 8,1015 if required. 8 shows full light , 1015 shows no light
// Serial.println(brightness);
strip.setBrightness(brightness);
DateTime now=RTC.now();
up.tick();
down.tick();
set.tick();
if(current==RTC_TIME)
rtcTime();
else if(current==LIGHT_TIME)
displayTime();
else if(current==SET_TIME){
lcd.setCursor(0,0);lcd.print("TIME ");// lcd.print("DATE: ");
//lcd.print(now.year()); lcd.print("/"); lcd.print(now.month()); lcd.print("/"); lcd.print(now.day());
lcd.setCursor(0,1);
lcd.print(now.hour()); lcd.print(":"); lcd.print(now.minute()); lcd.print(":"); lcd.print(now.second());
}else if(current==LIGHT_SET){
displayTime1();
} else if(current == WORKING){
lcd.setCursor(0,0); lcd.print("WORKING");
total_time=(hours*3600000)+(minutes*1000); // in seconds for testing (minutes+60000 for real time)
if(set_hours==now.hour() && set_min == now.minute() && started==false)
{
started=true;
set_time=millis();
Serial.println("Time started");
}
if(started){
for (int i=0; i< LED_COUNT; i++)
strip.setPixelColor(i, 255, 0, 0); // you can change the color here. r,g,b
strip.show();
if(millis()-set_time>total_time){
strip.clear();
strip.show();
current=SET_TIME;
started=false;
}
}
}
}
void upchange(){
if(current==RTC_TIME){
Serial.println("UP change");
if (settingHours) {
rtc_hour = (rtc_hour + 1) % 24;
} else {
rtc_minute = (rtc_minute + 1) % 60;
}
}
else if(current==LIGHT_TIME){
Serial.println("UP change");
if (settingHours) {
hours = (hours + 1) % 24;
} else {
minutes = (minutes + 1) % 60;
}
}else if(current==LIGHT_SET){
Serial.println("UP change");
if (settingHours) {
set_hours = (set_hours + 1) % 24;
} else {
set_min = (set_min + 1) % 60;
}
}
}
void downchange(){
if(current==RTC_TIME){
Serial.println("UP change");
if (settingHours) {
rtc_hour = (rtc_hour + 23) % 24;
} else {
rtc_minute = (rtc_minute + 59) % 60;
}
}
else if(current==LIGHT_TIME){
Serial.println("DOWN change");
if (settingHours) {
hours = (hours + 23) % 24;
} else {
minutes = (minutes + 59) % 60;
}
} else if(current==LIGHT_SET){
Serial.println("DOWN change");
if (settingHours) {
set_hours = (set_hours + 23) % 24;
} else {
set_min = (set_min + 59) % 60;
}
}
}
void setchange(){
if(current==LIGHT_TIME || current==LIGHT_SET ||current==RTC_TIME){
settingHours = !settingHours;
}
}
void longset(){
lcd.clear();
if(current==RTC_TIME){
RTC.adjust(DateTime(2024,6,4,rtc_hour,rtc_minute,00));
current=LIGHT_TIME;
}
else if(current==LIGHT_TIME)
current=LIGHT_SET;
else if(current==LIGHT_SET)
current=SET_TIME;
else if(current==SET_TIME){
//set_time=millis();
current=WORKING;
}
else if(current==WORKING)
current=RTC_TIME;
}
void displayTime() {
unsigned long currentMillis1 = millis();
if (currentMillis1 - previousMillis1 >= interval1) {
previousMillis1 = currentMillis1;
cursorBlink = !cursorBlink;
}
lcd.setCursor(0, 0);
lcd.print("SET LIGHT TIME");
lcd.setCursor(0, 1);
if (cursorBlink) {
if (settingHours) {
lcd.setCursor(0, 1);
lcd.print(" "); // Blank out minutes
} else {
lcd.setCursor(3, 1);
lcd.print(" "); // Blank out hours
}
} else {
lcd.setCursor(0, 1);
if (hours < 10) {
lcd.print("0");
}
lcd.print(hours);
lcd.print(":");
if (minutes < 10) {
lcd.print("0");
}
lcd.print(minutes);
}
}
void displayTime1() {
unsigned long currentMillis1 = millis();
if (currentMillis1 - previousMillis1 >= interval1) {
previousMillis1 = currentMillis1;
cursorBlink = !cursorBlink;
}
lcd.setCursor(0, 0);
lcd.print("TIME FOR LIGHT");
lcd.setCursor(0, 1);
if (cursorBlink) {
if (settingHours) {
lcd.setCursor(0, 1);
lcd.print(" "); // Blank out minutes
} else {
lcd.setCursor(3, 1);
lcd.print(" "); // Blank out hours
}
} else {
lcd.setCursor(0, 1);
if (set_hours < 10) {
lcd.print("0");
}
lcd.print(set_hours);
lcd.print(":");
if (set_min < 10) {
lcd.print("0");
}
lcd.print(set_min);
}
}
void rtcTime() {
unsigned long currentMillis1 = millis();
if (currentMillis1 - previousMillis1 >= interval1) {
previousMillis1 = currentMillis1;
cursorBlink = !cursorBlink;
}
lcd.setCursor(0, 0);
lcd.print("SET RTC TIME");
lcd.setCursor(0, 1);
if (cursorBlink) {
if (settingHours) {
lcd.setCursor(0, 1);
lcd.print(" "); // Blank out minutes
} else {
lcd.setCursor(3, 1);
lcd.print(" "); // Blank out hours
}
} else {
lcd.setCursor(0, 1);
if (rtc_hour < 10) {
lcd.print("0");
}
lcd.print(rtc_hour);
lcd.print(":");
if (rtc_minute < 10) {
lcd.print("0");
}
lcd.print(rtc_minute);
}
}