Hi,
I am also try to do the same but I add sunMoon.h library (Astronomical-clock ) which can change the sunrise and sunset time day by day in 2X sun tracker with 03 different mode of operation.
second think I am using DS3232RTC.h with DS3231 model with i2c 20*4 LCD and get the same result.
Some comment about librarys
library functions are return the sunrise and sunset times in a form of unsigned 32-bit word - in a UNIX time format (a seconds elapsed from 1st January 1970). And date and time in human readable format using a function similar to printDate(). Another library, timeLib, has a function as day(), month() and so on, that take a UNIX time and return a day, month, year, hour and minutes as a decimal numbers.
If we can fix these day night definition with sunMoon.h library it will help a lot.
My code is attached below I use LDR with RTC and if else statement but it is not good way to handle this.
It is lengthy code and many think are may be useless I apologies for that you can ignore I want help in"case 3://LED_03 on " day night definition when time is argument.
Thanks
case 3://LED_03 on
lcd.setCursor(4, 1);
lcd.print("Cloud ");
digitalWrite(LED_01, LOW);
digitalWrite(LED_02, LOW);
digitalWrite(LED_03, HIGH);
int tol = 20;
int lt = analogRead(ldrlt); // top left
int rt = analogRead(ldrrt); // top right
int ld = analogRead(ldrld); // down left
int rd = analogRead(ldrrd); // down rigt
/*
lt = map(lt, 0, 1023, 0, 255);
rt = map(rt, 0, 1023, 0, 255);
ld = map(ld, 0, 1023, 0, 255);
rd = map(rd, 0, 1023, 0, 255);
delay(1);
int difflt = abs(lt - oldlt);
int diffrt = abs(rt - oldrt);
int diffld = abs(ld - oldld);
int diffrd = abs(rd - oldrd);
if (difflt > TOLERANCE) {
oldlt = lt;
} // only save if the val has changed enough to avoid slowly drifting}
if (diffrt > TOLERANCE) {
oldrt = rt;
} // only save if the val has changed enough to avoid slowly drifting}
if (diffld > TOLERANCE) {
oldld = ld;
} // only save if the val has changed enough to avoid slowly drifting}
if (diffrd > TOLERANCE) {
oldrd = rd;
} // only save if the val has changed enough to avoid slowly drifting}
int avt = (lt + rt) / 2; // average value top
int avd = (ld + rd) / 2; // average value down
int avl = (lt + ld) / 2; // average value left
int avr = (rt + rd) / 2; // average value right
int dvert = avt - avd; // check the diffirence of up and down
int dhoriz = avl - avr;// check the diffirence og left and rigt
*/
time_t sRise = sm.sunRise();
time_t sSet = sm.sunSet();
if (RTC.get() > sRise && RTC.get() < sSet && lt > 680 )
{
Serial.println("Day");
lcd.setCursor(4, 3);
lcd.print("Day");
}
else if (RTC.get() > sSet && lt <590 || RTC.get() > sSet && lt <590)
{
Serial.println("Night ");
lcd.setCursor(4, 3);
lcd.print("Night");
}
else
{
Serial.println("Error ");
lcd.setCursor(4, 3);
lcd.print("Error");
}
break;
1.1_Equation_based_lcd_and_temp_23-3-18.ino (22.2 KB)