Blink Without delay help again.

(edit: this is a reply to comment #18)

Good answers :slight_smile:

As for the "no harm if I put a return there"... well technically it's true but it's a good habit to know if a code line is of any use instead of putting it in "just in case". :slight_smile:
In your particular piece of code, the (logical) error is changing a global variable and returning that same variable. You must decide how your function and the code that uses it communicate: either via the function return value or via a global variable.

Yes, global variables are generally a bad programming practice, but in an Arduino sketch it's ok to have a few global variables that match the particular hardware setup one is using.
For example, it's common to have an lcd global variable of type LiquidCrystal if a sketch uses an lcd display.

If all your program is doing is blinking ONE led, it's ok to have e.g. ledPin and ledState global variables.
Instead, if you want to write a couple of functions that can be used to blink any led the user specifies, then global variables must be avoided. A class should be used: the variables used to make the led blink (and remember its state) get incapsulated as private data members, and the functions become methods, which work on those private variables.
Each instance of that class (i.e. object) will have its own private state variables, so each led will blink independently of the other leds.

The keyword here is "state". If you have int sum(int a, int b) { return a + b; } you don't have any state to keep between two calls of that function. Instead when you blink an led you call e.g. updateLed() and you have to remember in which state the led was the last time you called that function. Local variables are not enough, as they last only for a function call. Globals (or better yet class private data members) are needed.

I hope it all makes sense... :slight_smile: