I am looking for some help on the below code I found online. This is meant to cycle through different led programs (Fasttled library palettes).
I am trying to adapt it to cycle through every minute but I cannot make it work !
Can someone explain me the need of "lastSecond" ? in
static uint8_t lastSecond = 99;
lastSecond = secondHand;
The "SecondHand" goes up to 60 and start over to 0, so basicaly the if statement is repeted until 0..
Thanks
This variable lastSecond (could be named previousSecond) is used to detect a change of the value of the secondHand variable, so that the code inside the if will run only once per second. If there was no if, the code would run thousand times doing the same calculation for the same result, which is a waste of cycles
It works only because lastSecond is static (it's like a global variable but limited to the current scope)
secondHand can only have a value 0 to 59, so any other value can be used to initialize lastSecond for a change to be detected immediately (when ChangePalettePeriodically is called for the first time)
As "secondHand" can be started from 0-59 at anytime when you call the "ChangePalettePeriodically" function. You can set "lastSecond" to any number except 0-59 to make the below statement on the first scan TRUE.
if( lastSecond != secondHand)
For example, static uint8_t lastSecond = 60;
First Scan will be compared as follows, if(lastSecond /*60*/ != secondHand /*0*/)