I want my photon to enter sleep mode during the evening. I also want the photon to stay awake for at least two hours after I have pushed the reset button. I have created a boolean function for evaluating if the current time is in between the go-to-sleep and wakeup time (which is different on thursdays than on other days) and a boolean function that evaluates if it has been more than 2 hours since setup ran.
In the main loop I use an if to check if these two booleans are true, and then put the photon to sleep until wakeup time.
I have however not yet gotten this to work, mainly because the main if block in loop always evaluates to true. So sleep automatically enables even though the twoHoursSinceStartup function should not evaluate to true. What am I doing wrong?
//When in sleep mode, reactivate by pressing the reset button, in which case it will stay on for at least two hours
unsigned long resetTime;
//Sleeping times, immediately correcting for notation in Unix Time instead of my own GMT+1 timezone:
int sleepingHourUsual = 22 - 1; //o'clock in the evening
int sleepingHourFriday = 4 - 1; //o'clock on fridaymorning. This is Thursday's sleepcycle starting on friday, currently only works for am
int wakingHourUsual = 9 - 1;
void setup() {
resetTime = millis();
}
void loop() {
if (sleepingTime(Time.weekday()) && twoHoursSinceStartup) {
if (Time.hour() > wakingHourUsual) //if sleeping starts at pm
{
System.sleep(((wakingHourUsual + (24 - Time.hour())) * 3600));
}
else //if sleeping starts at am
{
System.sleep(((wakingHourUsual - Time.hour()) * 3600));
}
}
digitalWrite(D7,HIGH);
delay(1000);
digitalWrite(D7,LOW);
delay(1000);
}
bool twoHoursSinceStartup() {
bool twoHoursPassed = ((millis() - resetTime) >= 7200000);
return twoHoursPassed;
}
//returns if photon should sleep based on the weekday and current hour. Different sleep cycle for Thursday night/Friday morning
bool sleepingTime(int weekday) {
bool sleepDuringCurrentHour;
if (!(weekday == (5 || 6))) { //if not Thursday or Friday. This works for both pm and am as starting sleeptime
//if time(am) inbetween am sleepstart and am sleepend || if time(pm) inbetween pm sleepstart and am sleepend || if time(am) inbetween pm sleepstart and am sleepend
sleepDuringCurrentHour = (Time.hour() < wakingHourUsual && Time.hour() >= sleepingHourUsual) ||
(Time.hour() >= sleepingHourUsual && sleepingHourUsual > wakingHourUsual) ||
(Time.hour() < wakingHourUsual && sleepingHourUsual > wakingHourUsual);
} else if (weekday == 5) { //if Thursday.
//if time(am) inbetween am sleepstart and am sleepend || if time(pm) inbetween pm sleepstart and am sleepend || if time(am) inbetween pm sleepstart and am sleepend
sleepDuringCurrentHour = (Time.hour() < wakingHourUsual && Time.hour() >= sleepingHourUsual) ||
(Time.hour() < wakingHourUsual && sleepingHourUsual > wakingHourUsual);
} else if (weekday == 6) { //if Friday, since thursdays sleep cycle is started on fridaymorning. This works for am as starting sleeptime
//if time(am) inbetween am sleepstart and am sleepend || if time(pm) after friday pm sleepstart
sleepDuringCurrentHour = (Time.hour() < wakingHourUsual && Time.hour() >= sleepingHourFriday) ||
(Time.hour() >= sleepingHourUsual && sleepingHourUsual > wakingHourUsual);
}
return sleepDuringCurrentHour;
}