Sunrise class library for Arduino
Note: Development of this library has been halted in favor of our TimeLord library, which includes all the Sunrise functions, and much more.
I need to know the time of sunrise and sunset for many of my projects.
However, doing so with a microcontroller can severely impact time and
memory constraints.
Floating point math is slow, all the
variables required take up lots of RAM, and the number of calculations
drives up
the program space required.
What I need is an algorithm that is light on the memory, consumes
few CPU cycles, and yet is accurate enough for the projects I work on.
The algorithm I use is derived from a NOAA document entitled 'General Solar
Position Calculations'.
It's Light, in that is consumes less storage than most other algorithms.
It's Quick, in that it uses fewer calculations than most other
algorithms.
Its
Accurate, in that it's within a few minutes of reality (maximum
deviation compared to a more accurate NOAA algorithm is 5 minutes).
I thought other Arduino
fans may find it of use, so I have now packaged it as an Arduino Library (download).
To install, unzip the library, then move the Sunrise folder to the
Libraries subfolder of your Sketchbook directory. You may need to
restart the IDE if it is already running. You can then find an example
sketch in the IDEs file menu.
Using the library
Import the library into
your sketch (#include <Sunrise.h>).
Create a Sunrise object for a particular location and timezone.
Compute the rise or set time for a particular date.
Read the hour and minute of the event.
Methods
Sunrise( float latitude, float longitude, float timezone);
This is the constructor.
Latitude and longitude are given in decimal degrees, with South and
West being negative.
Timezone is given in hours from UTC. It is a float because there are
still some localities with fractional offsets.
No attempt is made to account for Daylight Savings, so set timezone
accordingly.
Sunrise.Actual(), Sunrise.Civil(),
Sunrise.Nautical(),
Sunrise.Astronomical();
If you need this library, you probably
already know what these mean. Suffice it to say that the default is
Actual(), which is the time at which the upper edge of the suns disc is
just touching the (imaginary) horizon.
int Sunrise.Rise(byte month, byte day), int Sunrise.Set(byte month, byte day)
Returns the time of day
(in minutes from midnight) of the event.
The value ranges from 0 to 1440 minutes.
You can also obtain the hour and minute of the event separately after
making this call, see below.
If the return value is negative, the computation failed.
The only good reason for a failure is that the location is inside the
(Ant)Arctic circle, and the event does not occur on the specified date.
(Sunrise at the North Pole on Christmas?... not gonna happen).
byte Sunrise.Hour();
Returns the hour of the
last computed
event. Ranges from 0 thru 23... if over 23 then either no computation
has been performed, or the location is very chilly (see above).
byte Sunrise.Minute();
Returns the minute of the
last computed event.