// main loop
void loop()
{
getTime();
byte sunRise[6]; // create sunrise time array
byte sunSet[6]; // create sunset time array
for(int i=0; i<=5; i++)
{
sunRise = theTime*;*
sunSet = theTime*;*
* }*
* luleaTime.SunRise(sunRise); // calculate sunrise*
* luleaTime.SunSet(sunSet); // calculate sunset*
* //recalculate time to seconds from midnight for comparison values*
_ long theTimeSecond = (long)theTime[0] + (long)theTime[1]60 + (long)theTime[2]3600;
long sunRiseSecond = (long)sunRise[0]+(long)sunRise[1]60+(long)sunRise[2]3600;
long sunSetSecond = (long)sunSet[0]+(long)sunSet[1]60+(long)sunSet[2]3600;
long lightsOnTimeSecond;
Serial.print("theTime timestamp: ");
Serial.println(theTimeSecond);
* Serial.print("Sunrise timestamp: ");
Serial.println(sunRiseSecond);
Serial.print("Sunset timestamp: ");
Serial.println(sunSetSecond);
Serial.println();*_
//******************************************************************
* Serial.print("The time and date is (YY:MM:DD:HH:MM:SS): ");*
* for(int i=0; i<=5; i++)*
* {*
* Serial.print(theTime[5-i]);*
* if (i != 5) Serial.print(":");*
* }*
* Serial.println();*
* Serial.print("Sun rises at: ");*
* for(int i=0; i<=2; i++)*
* {*
* Serial.print(sunRise[2-i],DEC);*
* if (i != 2) Serial.print(":");*
* }*
* Serial.println();*
* Serial.print("Sun sets at: ");*
* for(int i=0; i<=2; i++)*
* {*
* Serial.print(sunSet[2-i],DEC);*
* if (i != 2) Serial.print(":");*
* }*
* Serial.println();*
_//****************************************************************** _
/*
* Evaluate times and send commands to lights
*/
// check if the sun sets the current day, if not - do nothing
* if (sunRiseSecond < sunSetSecond)*
* {*
* //Calculate time offset to turn on lights a number of minutes before actual sunset*
* int timeOffset = 30; //number of minutes (<60) before sunset to turn lights on*
_ lightsOnTimeSecond = (long)sunSet[0]+(long)sunSet[1]60+(long)sunSet[2]3600-timeOffset60;
Serial.print("Adjusted lights on time: ");
Serial.print(lightsOnTimeSecond/3600);
Serial.print(":");
Serial.print((lightsOnTimeSecond%3600)/60);
Serial.print(":");
Serial.println(((lightsOnTimeSecond%3600)%60));
Serial.print("lightsOnTimeSecond: ");
Serial.println(lightsOnTimeSecond);
Serial.println();
Serial.println("WEEKDAY IF VARIABLES*");
* Serial.print("theTime hour: ");
Serial.println(theTime[2],DEC);
Serial.print("Day of the Week (Sunday = 1): ");
switch (luleaTime.DayOfWeek(theTime)){
case 1: Serial.println("Sunday"); break;
case 2: Serial.println("Monday"); break;
case 3: Serial.println("Tuesday"); break;
case 4: Serial.println("Wednesday"); break;
case 5: Serial.println("Thursday"); break;
case 6: Serial.println("Friday"); break;
case 7: Serial.println("Saturday"); break;
}
Serial.print("Lights status: ");
Serial.println(lightsOn);
Serial.println("");
Serial.println();_
__//__
_ //Weekday evening lights - turn lights on if time is more than 17:00, less than 23:00 and sunset occurred on a weekday
__ if( 81900 /22:45 in seconds/ > theTimeSecond && theTime[2] >= 17 && theTimeSecond >= lightsOnTimeSecond && 7 > luleaTime.DayOfWeek(theTime) && luleaTime.DayOfWeek(theTime) > 1 && lightsOn != true)__
{
turnLightsOn();
lightsOn = true;
Serial.println("TURNING WEEKDAY EVENING LIGHTS ON!");
}_
__//__
_ //Weekday morning lights on - turn lights on at 7 in the morning on weekdays if sunrise has not occured
else if (theTime[2] == 5 && theTime[1] == 30 && sunRiseSecond > theTimeSecond && luleaTime.DayOfWeek(theTime) != 7 && luleaTime.DayOfWeek(theTime) != 1 && lightsOn != true)
{
turnLightsOn();
lightsOn = true;
Serial.println("TURNING WEEKDAY MORNING LIGHTS ON!");
}_
__//__
_ //Weekday morning lights off - turn lights off at 7:40 on weekdays
else if (theTime[2] == 7 && theTime[1] == 40 && 7 > luleaTime.DayOfWeek(theTime) && luleaTime.DayOfWeek(theTime) > 1 && lightsOn != false)
{
turnLightsOff();
lightsOn = false;
Serial.println("TURNING WEEKDAY MORNING LIGHTS OFF!");
}_
__//__
_ //Weekends - turn lights on if time is more than 8:00, less than 23:00 and sun is not up on a weekend
else if(23 > theTime[2] && theTime[2] >= 8 && (sunRiseSecond > theTimeSecond || theTimeSecond >= sunSetSecond) && (luleaTime.DayOfWeek(theTime) == 1 || luleaTime.DayOfWeek(theTime) == 7) && lightsOn != true)
{
turnLightsOn();
lightsOn = true;
Serial.println("TURNING WEEKEND LIGHTS ON!");
}
//Weekends - turn lights off during daytime
else if(theTimeSecond > sunRiseSecond && theTimeSecond < sunSetSecond && lightsOn != false)
{
switch (luleaTime.DayOfWeek(theTime)){
case 1: turnLightsOff();
lightsOn = false;
break;
case 7: turnLightsOff();
* lightsOn = false;
break;
}
Serial.println("TURNING WEEKEND LIGHTS OFF!");
}_
__//__
_ //Weekday night lights off
else if (theTime[2] == 22 && theTime[1] == 45 && luleaTime.DayOfWeek(theTime) != 7 && luleaTime.DayOfWeek(theTime) != 1 && lightsOn != false)
{
turnLightsOff();
lightsOn = false;
Serial.println("NIGHT 22:45 LIGHTS OFF!");
}_
__//__
_ //Weekend night lights off
else if (theTime[2] == 23 && theTime[1] >= 30 && lightsOn != false)
{
switch (luleaTime.DayOfWeek(theTime)){
case 1: turnLightsOff();
lightsOn = false;
break;
case 7: turnLightsOff();
lightsOn = false;
break;
}
Serial.println("WEEKEND NIGHT 23:30 LIGHTS OFF!");
}_
__//****************************************************__
_ }
else Serial.println("LIGHT CONTROL SKIPPED TODAY!");
delay(20000);
}
[/code]
I had this idea that maybe I could use a switch case statement in the main loop to optimize the program a bit. I was thinking of doing a switch case for the days of the week (1=Sunday), something like this:
*_</em></em> <em><em><em>*switch(luleaTime.DayOfWeek(theTime)){ case 1: //Sunday //check time and sunset crieria for the day here, then run a function maybe? break; case 2 ... 5: //Monday-Thursday break; case 6: //Friday break; case 7: //Saturday break; }*</em></em></em> <em><em>_*
But this seems moot as I still need my different criterias for lights on/off depending on sunrise/sunset on the specific day. I feel a bit like I'm getting blind with my solution here, so any input appreciated on improving/simplifying the code is welcome _