I want to display the sunset and sunrise times in my weather station project.
I was thinking I could use the Dusk2Dawn library, but when I use the example code to test things, it gives me an error in the serial monitor:
418
1014
-1
06:58
16:53
11:56
ERROR
Uh oh!
I think this is a slightly major error!
How do I get this example code to work?
@sterretje
Why does it say Uh Oh, Error?
Also, do I have to set my timezone? The serial monitor is not displaying accurate sunset and sunrise times for today in my area.
[code]
#include <Dusk2Dawn.h>
void setup() {
Serial.begin (9600);
Dusk2Dawn myhome(myLongitude, myLatitude, 10);
int homeSunrise = myhome.sunrise(2022, 7, 11, false);
int homeSunset = myhome.sunset(2022, 7, 11, false);
// Time is returned in minutes elapsed since midnight. If no sunrises or
// sunsets are expected, a "-1" is returned.
Serial.println(homeSunrise);
Serial.println(homeSunset);
// A static method converts the returned time to a 24-hour clock format.
// Arguments are a character array and time in minutes.
char time[6];
Dusk2Dawn::min2str(time, homeSunrise);
Serial.println(time); // 06:58
// Alternatively, the array could be initialized with a dummy. This may be
// easier to remember.
char time2[] = "00:00";
Dusk2Dawn::min2str(time2, homeSunset);
Serial.println(time2); // 16:53
// Do some calculations with the minutes, then convert to time.
int homeSolarNoon = homeSunrise + (homeSunset - homeSunrise) / 2;
char time3[] = "00:00";
Dusk2Dawn::min2str(time3, homeSolarNoon);
Serial.println(time3); // 11:56
// In case of an error, an error message is given. The static method also
// returns a false boolean value for error handling purposes.
char time4[] = "00:00";
bool response = Dusk2Dawn::min2str(time4, homeSunrise);
if (response == false) {
Serial.println(time4); // "ERROR"
Serial.println("Uh oh!");
}
}
void loop() {
}
[/code]