Time sensitive actions

I’m currently tying myself up in knots trying to do time context aware signalling.

I’d like to (for example) turn an LED on if it’s between 9am and 11am AND a software flag is set to 1.

I’d like to (for example) FLASH that LED if it’s between 12 and 1pm AND a software flag isn’t set to 1.

I’d like to (for example) turn that same LED off if it’s between 9am and 11am AND a software flag ISN’T set to 1.

I’d like to (for example) turn an LED off if it’s between 4pm and 5pm AND a software flag is set to 1.

How would you approach this? I am currently doing this sort of thing, and it’s VERY difficult for me to read and predict how the LED will actually behave as my “if nests” are for me not intuitive to build and interpret.

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

Thanks

Whichever if clause is executed first will be the only one that is executed.

To keep things simple though, a a bunch of else if’s are not the best way to do this. Check the docs for “Switch…case” statements, they will do what you want in a much cleaner and easier to read way. Be careful with your “breaks” in a switch statement though.