Every now and then I go over old code to see if I can do better. Revisiting one fairly extensive sketch that I wrote several years ago I noticed a single warning during compile, "switch condition has type bool"
I vaguely recall that this method saved me several lines of code and a variable or two but otherwise I don't know why I went this way other than thinking the color is either red (true) or it isn't (false).
In any event, the sketch works very well and this has never caused an issue however it raises the question, are all compile warnings necessarily a bad thing if you understand why?
// STARTING BOOLEANS
boolean red; // used to determine color, true = red, false = green
void setup()
{
}//end setup
void loop()
{
red = !red; //switch color
sirenFx(2, 500, 1000, 10 ); //system changing alert (type, lowFreq, highFreq, increment)
switchLast = ledTimer; //note the time this switch occured
}//end if
else switch (red) { //blink the currently active color
//the case never changes if the programmed switch time is 0 (no switching)
case true:
blinkRed();
break;
case false:
blinkGreen();
break;
}//end switch red
}//end if
}// end loop
//FUNCTIONS
void blinkGreen(void)
{
//last time color blinked
digitalWrite(redLED, LOW); //trap the red
if (ms - blinkLast > (ledState ? LED_ON : LED_OFF)) {
digitalWrite(greenLED, ledState = ! ledState); //switch states
blinkLast = ms;
}
}//end blinkGreen
void blinkRed(void)
{
// unsigned long blinkLast; //last time color blinked
digitalWrite(greenLED, LOW); //trap the green
if (ms - blinkLast > (ledState ? LED_ON : LED_OFF)) {
digitalWrite(redLED, ledState = ! ledState);
blinkLast = ms;
}
}//end blinkRed