// accompanies thread in German Arduino forum: http://forum.arduino.cc/index.php?topic=313942.0
// Compare results against: http://www.esrl.noaa.gov/gmd/grad/solcalc/
// Results are reasonable, but not too accurate, so do not use this for scientific purposes!
#include <avr/pgmspace.h> // save RAM with AVR/Atmega controllers in some functions using PROGMEM
#include <Wire.h> // include Wire library for I2C devices
#include "jursRTCtimeEasy.h" // include RTC time library
#include "sunrise.h" // include library for sunrise/sunset calculations
/*void serialTask()
{ // Check Serial input for time setting command
char* command=readSerialCommand();
if (command!=NULL) // we have found a command from Serial
{
Serial.print(command);Serial.print('\t');
if (strncmp_P(command,PSTR("setc "),5)==0 || strncmp_P(command,PSTR("setd "),5)==0) // compare for command
{
if (handleRtcCommand(command)) Serial.println(F("OK"));
else Serial.println(F("ERROR: invalid time not set"));
}
else Serial.println(F("Unknown command")); // handle other commands here
}
}
*/
enum {DAY, NIGHT, DAWN};
int getDayState(sTime time, int dawn, int sunrise, int sunset, int dusk)
{ // returns DAY, NIGHT or DAWN value, depending on time and sun position
int minuteAfterMidnight= time.bHour*60+time.bMinute;
if (minuteAfterMidnight>=sunrise && minuteAfterMidnight<sunset) return DAY;
else if (minuteAfterMidnight<dawn || minuteAfterMidnight>=dusk) return NIGHT;
else return DAWN;
}
#define UPDATEINTERVAL 10000 // milliseconds
#define LAT 46.95 // latitude in degrees, south is negative (used in sun calculation)
#define LON 15.05 // longitude in degrees, west is negative (used in sun calculation)
const char timeFormat[] PROGMEM="%02d.%02d.%04d %02d:%02d:%02d";
void rtcTask()
{
static unsigned long lastUpdate; // remember last time this function has run
sTime time; // variable to hold current time/timezone
char buf[61]; // variable for output formatting
if (millis()-lastUpdate>=UPDATEINTERVAL)
{
lastUpdate+=UPDATEINTERVAL;
RTCreadLocalTime(time); // read localTime (use summertime, if applicable)
snprintf_P(buf,sizeof(buf),timeFormat,time.bDay,time.bMonth,time.iYear,time.bHour,time.bMinute,time.bSecond);
if (time.sTimezone==TIMEZONE+1); // strcat(buf," *"); // indicates summertime
Serial.println(buf); // show it on serial
// now do the calculation for dawn/sunrise/sunset/dusk times today
int sunrise, sunset, dawn, dusk;
sunCalc(time.iYear,time.bMonth,time.bDay,time.sTimezone, LAT, LON, sunrise, sunset, dawn, dusk);
snprintf_P(buf, sizeof(buf), PSTR("Dawn: %02d:%02d, Sunrise: %02d:%02d, Sunset: %02d:%02d, Dusk: %02d:%02d"), dawn/60, dawn%60, sunrise/60, sunrise%60, sunset/60, sunset%60, dusk/60, dusk%60);
Serial.println(buf);
// Finally find out whether we have DAY, NIGHT or TWILIGHT time of the day
switch (getDayState(time, dawn, sunrise, sunset, dusk))
{
case DAY: Serial.println("DAY");break;
case NIGHT: Serial.println("NIGHT");break;
default: Serial.println("TWILIGHT");break;
Serial.println(dawn);
}
Serial.println();
}
}
/*void usage()
{ // show some information about program usage
Serial.println(F("\nRTC date and time library demo by 'jurs' for German Arduino forum\n"));
Serial.println(F("Usage for setting RTC date/time from Serial monitor in format"));
Serial.println(F("\nOr you can use German date formatting with the 'setd' command:"));
Serial.println(F("setd 24.12.2016 9:30:12 (24. Dezember 2016 um 9:30:12 Uhr)"));
Serial.println(F("Time setting is in local time during summertime!\n\n"));
}
*/
void setup()
{
Serial.begin(9600);
// usage();
if (RTCinit()) Serial.println(F("RTC OK"));
else Serial.println(F("RTC/I2C ERROR"));
}
void loop()
{
// serialTask();
rtcTask();
}
Hallo, ich hab folgenden Sonnenaufgang Sketch in Verwendung. Nun würde ich gern mit der Sonnenaufgangszeit bzw. Untergangszeit weiterrechnen. Will nämlich für meinen Hühnerstall eine zusätzliche Beleuchtung im Stall einschalten wenn zwischen Sonnenaufgangzeit und Untergangszeit <16h liegen.
Wie kann ich sowas am Besten angehen? DANKE