Optimize if statements with switch case

// 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 :slight_smile:_

Using switch/case will not necessarily make the program more efficient or optimize the code but it could make the program easier to read and understand particularly if you use an enum to define the days of the week. so that you can use names in the case statements.

UKHeliBob:
Using switch/case will not necessarily make the program more efficient or optimize the code but it could make the program easier to read and understand particularly if you use an enum to define the days of the week. so that you can use names in the case statements.

I've never used enums before, you mean something like

enum DayOfWeek{
Monday, Tuesday, Wednesday,Thursday,Friday,Saturday,Sunday
};

The thing is that the day of the week is given from an RTC and is a byte with value 1-7, 1 being Sunday. This is indeed a bit annoying as in my country Monday is day 1 of the week, not Sunday.

Should I then do something like "If RTCValue == 1 then DayOfWeek = Sunday"?
That adds an extra step, but also clarity

EDIT: I suppose I could also just do #define Sunday 1 etc. to make the code more legible.

How about

enum DayOfWeek{
Sunday=1, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
};

Whether Sunday or Monday is the first day of a week, does not really matter in your lightswitching scheme, does it?

I've never used enums before, you mean something like

enum DayOfWeek{
Monday, Tuesday, Wednesday,Thursday,Friday,Saturday,Sunday
};

Yes

Should I then do something like "If RTCValue == 1 then DayOfWeek = Sunday"?

You could put the days of the week in an array and use the value given by the RTC as the index to the array to give you the day of the week.

The thing is that the day of the week is given from an RTC and is a byte with value 1-7, 1 being Sunday. This is indeed a bit annoying as in my country Monday is day 1 of the week, not Sunday.

Using an array of values you can transform which number represents which day if you want to.

Whandall:
How about

enum DayOfWeek{

Sunday=1, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
};



Whether Sunday or Monday is the first day of a week, does not really matter in your lightswitching scheme, does it?

Well it does in the sense that the RTC returns 1 for Sunday, but other than that, no, not other than the fact that its not the way I see the week in my home country. It throws me off a bit when I get like day 4 and I have to start counting from Sunday when I'm used to having Monday as the beginning of my week.

I've used #define to make the day names into numbers, so it should be easier to keep track.

From the DS1307 datasheet as an example:
"The day-of-week register increments at midnight. Values that correspond to the day of week are user-defined but must be sequential (i.e., if 1 equals Sunday, then 2 equals Monday, and so on.)"
After entering the current date & time, I believe you can change the day of week register to be whatever number you want from 1 to 7.

...I have to start counting from Sunday when I'm used to having Monday as the beginning of my week.

enum DayOfWeek{
  Monday = 1, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
};

econjack:

enum DayOfWeek{

Monday = 1, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
};

If his RTC thinks Sunday is day 1 and sends a 1 to his sketch, his sketch will think it's Monday and be a day out.
Something like

DayofWeek = DayofWeekFromRTC -1; 
if (DayofWeek == 0) DayofWeek = 7;

should be the way to go.

should be the way to go.

Or just use an array to convert one value to the other.

UKHeliBob:
Or just use an array to convert one value to the other.

I prefer to KISS.

Henry_Best:
I prefer to KISS.

So do I, hence the suggestion to use an array

DayOfWeek = arrayOfDays[DayofWeekFromRTC ];

@UKHeliBob
I think we'll have to agree to disagree on this.

Henry_Best:
@UKHeliBob
I think we'll have to agree to disagree on this.

Agreed :slight_smile: