Hello,
The begintimer and eindtimer (endtimer) won't change their values to now()
I don't know what causes this
To set the time im using Setserial by JChristensen
Here's my code
#include <DS3232RTC.h> // https://github.com/JChristensen/DS3232RTC
#include <Streaming.h> // http://arduiniana.org/libraries/streaming/
#define licht A1 //Lichtsensor krijgt analoge pinwaarde A1
int relais_licht = 4;
unsigned long begintimer = 0;
unsigned long eindtimer = 0;
unsigned long aantal_uren_zonlicht = 0;
unsigned long previousTime_licht = 0;
const byte interval_licht = 10; // dit is 10 seconden
const byte som_waarde = 29;
boolean flag_bijbelichting = false;
void setup()
{
Serial.begin(115200);
// setSyncProvider() causes the Time library to synchronize with the
// external RTC by calling RTC.get() every five minutes by default.
setSyncProvider(RTC.get);
Serial << F("RTC Sync");
if (timeStatus() != timeSet) Serial << F(" FAIL!");
Serial << endl;
pinMode(relais_licht, OUTPUT);
}
void loop()
{
lichtsysteem();
static time_t tLast;
time_t t;
tmElements_t tm;
// check for input to set the RTC, minimum length is 12, i.e. yy,m,d,h,m,s
if (Serial.available() >= 12) {
// note that the tmElements_t Year member is an offset from 1970,
// but the RTC wants the last two digits of the calendar year.
// use the convenience macros from the Time Library to do the conversions.
int y = Serial.parseInt();
if (y >= 100 && y < 1000)
Serial << F("Error: Year must be two digits or four digits!") << endl;
else {
if (y >= 1000)
tm.Year = CalendarYrToTm(y);
else // (y < 100)
tm.Year = y2kYearToTm(y);
tm.Month = Serial.parseInt();
tm.Day = Serial.parseInt();
tm.Hour = Serial.parseInt();
tm.Minute = Serial.parseInt();
tm.Second = Serial.parseInt();
t = makeTime(tm);
RTC.set(t); // use the time_t value to ensure correct weekday is set
setTime(t);
Serial << F("RTC set to: ");
printDateTime(t);
Serial << endl;
// dump any extraneous input
while (Serial.available() > 0) Serial.read();
}
}
t = now();
if (t != tLast) {
tLast = t;
printDateTime(t);
if (second(t) == 0) {
float c = RTC.temperature() / 4.;
float f = c * 9. / 5. + 32.;
Serial << F(" ") << c << F(" C ") << f << F(" F");
}
Serial << endl;
}
}
// print date and time to Serial
void printDateTime(time_t t)
{
printDate(t);
Serial << ' ';
printTime(t);
}
// print time to Serial
void printTime(time_t t)
{
printI00(hour(t), ':');
printI00(minute(t), ':');
printI00(second(t), ' ');
}
// print date to Serial
void printDate(time_t t)
{
printI00(day(t), 0);
Serial << monthShortStr(month(t)) << _DEC(year(t));
}
// Print an integer in "00" format (with leading zero),
// followed by a delimiter character to Serial.
// Input value assumed to be between 0 and 99.
void printI00(int val, char delim)
{
if (val < 10) Serial << '0';
Serial << _DEC(val);
if (delim > 0) Serial << delim;
return;
}
////
////
////
////
////
////
////
////
////
////
////
////
////
////
////
////
////
////
////
////
void lichtsysteem()
{
int licht_waarde = analogRead(licht);// De lichtwaarde wordt ingelezen
int M_Volt = map(licht_waarde,0, 1023, 0, 5000);// De waarde wordt aan 5000 gezet
unsigned long currentTime_licht = now();
if (currentTime_licht - previousTime_licht >= interval_licht) {
Serial.println(begintimer);
previousTime_licht = currentTime_licht;
Serial.println(" ");
Serial.print(M_Volt);// print millivolt
Serial.println( "mV ");
delay(500); // wacht 500ms
}
if(licht_waarde >= 1000 /*juiste waarde*/) //als de zon schijnt dan ...
{
if(begintimer == 0)
{
Serial.println("Eindtimer en begintimer geset");
begintimer = now();
eindtimer = now();
}
else
{
Serial.println("Eindtimer geset");
eindtimer = now();
}
}
else
{
if(begintimer > 0 && eindtimer > 0)
{
aantal_uren_zonlicht = aantal_uren_zonlicht + eindtimer - begintimer;
begintimer = 0;
eindtimer = 0;
}
else
{
begintimer = 0;
eindtimer = 0;
}
}
////// TOT HIER IS AL JUIST
if(flag_bijbelichting == false)
{
byte som_tijd = som_waarde - hour();
if(som_tijd <= 11 - aantal_uren_zonlicht)
{
flag_bijbelichting = true;
lichtsysteem2();
}
else
{
if(hour()>22 && aantal_uren_zonlicht <= 11)
{
flag_bijbelichting = true;
lichtsysteem2();
}
else
{
}
}
}
}
void lichtsysteem2()
{
if(relais_licht == HIGH)
{
}
else
{
relais_licht = HIGH;
}
if(begintimer = 0)
{
begintimer = hour();
eindtimer = hour();
}
else
{
eindtimer = hour();
}
if(aantal_uren_zonlicht + eindtimer - begintimer > 11)
{
aantal_uren_zonlicht = 0;
relais_licht = LOW;
flag_bijbelichting = false;
}
}