Elegant way to do this "If it's between these hours" routine

I’ve just spent a very frustrating evening trying to convince my Cores to light one LED if it’s after 7pm in the evening and before 7am the next morning and then light a second (different) LED for two hours from 7am-9am.

I’ve tied myself in knots with if, else, else if statements, < > and even time alarm libraries (various). I’m worn out and demoralised and I’ve only myself to blame.

I’m going to have another go tomorrow but before I do…any tips or words of advice? Time Alarms seem not to be such a good plan because if the device got powered down the LED (presumably?) wouldn’t come back on until the next alarm got triggered.

thanks!

if (Time.hour() < 7) ledOneOn();
else if (Time.hour() >= 7 && Time.hour() < 9) ledTwoOn();
else if (Time.hour() >= 19) ledOneOn();
else ledsOff();

This takes care of the case from

  • 00:00 to 06:59
  • 07:00 to 8:59
  • 19:00 to 11:59

Double-check me on that, but that might do for you.

2 Likes

You may find this discussion interesting (found via forum search "certain time")

Especially once you don't want to go for full hour boundaries you'll get yourself in a pickle with the most obvious solutions :wink:

3 Likes

thanks @scruffr and @heng this seemed like a really good fit for something I’m trying to do as well - so I’m trialling the code - but I found it didn’t work for me and was still showing a red LED at half past eight this morning, when I reset the Core (simply wondering what state it would boot up in) I got no LED at all.
So it’s almost as if it was able at some point to know what state to turn on, but it wasn’t able to transition either onto green OR off from red, if that makes sense? Is it because I swapped around one of the else ifs?

	if (Time.hour() < 7) redLED.on();
	else if (Time.hour() >= 19) redLED.on();
	else if (Time.hour() >= 7 && Time.hour() < 9) greenLED.on();
	else 
	redLED.off();
	greenLED.off();

thanks!

Have you considered my approach at all?
I have never had any issues setting any arbitrary time frame with the seconds based range.

I wrote that rather elaborate post to make people understand how it’s done and how to build their own solution with that paradigm in mind - not as a copy/paste solution :wink:

Hi @Scruffr I have considered it, understood it(!) and if necessary I will definitely go for it but because I don’t need to do things anything other than for the complete hour (ie I have no need for ten past four or quarter to five, I’m only interested in if it’s within the hour of four pm etc) I was hoping to be able to use the simpler solution. I’m just baffled as to why it doesn’t work. But I’m happy to do some more experimenting!

Two things, are you making sure the time is being set from the server?

and you forgot a curly bracket:

if (Time.hour() < 7) redLED.on();
	else if (Time.hour() >= 19) redLED.on();
	else if (Time.hour() >= 7 && Time.hour() < 9) greenLED.on();
	else {
	redLED.off();
	greenLED.off();
        }

Also, I hoped it would be clear, but redLED.on() needs to call greenLED.off() and vice versa.

	if (Time.hour() < 7) //time between midnight and 0700, red LED ON!
		redLED.on();
		greenLED.off();   
	else if (Time.hour() >= 19) // time between 1900 and 00:00, red LED ON!
		redLED.on();
		greenLED.off();	  
	else if (Time.hour() >= 7 && Time.hour() < 9) //time between 0700 and 0900, green LED on!
		greenLED.on();
		redLED.off();    
	else {
	redLED.off();
	greenLED.off();
	}

Do you mean like this? Sorry to be a bit slow on the uptake but I'm not sure if the final else condition takes into account turning off the opposing LED if it isn't specifically supposed to be on, or if I need to do that as shown in the above snippet! Thanks

Edit: I need curly brackets around the if, the else if and the second else if as well don't I?!

For compactness and readability sake I think you could turn:

if (Time.hour() < 7) //time between midnight and 0700, red LED ON!
redLED.on();
greenLED.off();
else if (Time.hour() >= 19) // time between 1900 and 00:00, red LED ON!
redLED.on();
greenLED.off();

simply into:

if (Time.hour() >= 19 || Time.hour() < 7) { //time between 1900 and 0700, red LED ON!
redLED.on();
greenLED.off(); }

Since you're doing the same thing in this time range. Leaving in redundant if-statements can quickly clutter up the code when you start adding in new things (as I experienced :wink: )

2 Likes

Yes, if() will conditionally execute the next statement or block. So

if (condition) greenLED.on();
redLED.on();

will only execute greenLED.on() if condition is TRUE but will always execute redLED.on()

To make both statements conditional, it would be

if (condition) {
  greenLED.on();
  redLED.on();
}

When I have a list of one-liners like this, I often omit the brackets and use spacing for readability, but you have to be very careful to not forget to add them if you need more than one statement after one of the conditionals.

if (Time.hour() < 7)                          ledOneOn();
else if (Time.hour() >= 7 && Time.hour() < 9) ledTwoOn();
else if (Time.hour() >= 19)                   ledOneOn();
else                                          ledsOff();

I did it that way originally because the conditions work their way around the clock from 0 to 23. Just a different way to organize things.

Thanks - really learning something @HEng and @Vitesze

but @HEng - in the above code you still don't explicitly turn off ledTwo in the code snippet, - is that because there's no need - it's dealt with in the "else ledsOff()" ? I will experiment to find out...

This was what I meant those functions to be:

void ledOneOn() {
  greenLED.on();
  redLED.off();
}

void ledTwoOn() {
  greenLED.off();
  redLED.on();
}

void ledsOff() {
  greenLED.off();
  redLED.off();
}
1 Like

ahhh! thanks.

Thanks for your patience everyone.