Dusk2Dawn Example not working

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?

Don't expect us to always know what you're talking about :wink: Please post a link to the DuskToDown library as well as the code that you use.

1 Like

@sterretje
Dusk2Dawn library:

Looks like the example code works as it is supposed to. See the below comment in the example

  // Time is returned in minutes elapsed since midnight. If no sunrises or
  // sunsets are expected, a "-1" is returned.

@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.

Because according to the library, there is no sunset/sunrise in Antartica.

I've switched my PC off but you need to set the coordinates of your location. The examples are for Los Angeles and Antartica.

1 Like

The working code:

[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]

Hello Cowboydaniel, It might be better if you didn't show your location with such precision .

2 Likes

Oops

Why?

@Paul_B
Because people can see where I live, and there are some weird people out there who like being weird.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.