What you have is basically pretty good. There are always other ways to do things, efficiencies to be had, but at this point, don’t worry to much about them. You’re not building real-time missle defense systems. Concentrate on understanding everything, then go for the quick lean code.
I’ll answer a couple of your questions below. All variables have scope, scope is the chunk of code in which the variable is valid. When you put a variable in the header of your function as something that’s passed in (like you did with void flashLedActive(int flashMe)), that variable’s scope is only that function. That means that after the function, the variable no longer exists, the code as no idea how to find it. So the reason you must declare flashMe as a global is that you use the variable in the loop function. Therefore loop() had to know about it. Putting it in the header of flashLedActive(int) only allowed that function to know about it. You actually didn’t need to pass it in at all. Because it’s global, every function can see that variable. So you could have simply not passed in anything and just used the global flashMe inside your function. But I notice that you’re not using the flashMe variable in that function, so I’m wondering why you pass it? BTW, when you have two variables with the same name, the “closer” scope takes precedence, if that makes sense. So right now, in the flashLedActive function, you cannot get to the flashMe global variable, because you have a local variable called that same name.
Most coders view global variables as sort of a hack way of getting around having to properly pass variables, passing is viewed as the more proper approach. But hey, it’s your board, do what you want, how you want
As far as durationBetween being negative, I’m not sure what the question is there. See the reference link off the arduino.cc homepage, they have nice documentation on the sizes of the variable types. int’s can hold negative numbers, no problem.
So for efficiency, I’d take a stab at the flashLedActive function, and change it like this.
// code contained in a function to turn flashLedPin on and off using code below
digitalWrite(yellowLedPin, HIGH); // turn on the yellow LED
for (int i=0; i<4; i++)
First, you don’t use anything non-global, so why pass a variable. The loop is just to make the code shorter and make it much easier to flash it as many times as you want. The digitalWrite coupled with a ! (not) and digitalRead is just a little trick to flip the signal. BTW, a good example of scope there. Because I declare “int i” inside the for statement, the scope of the variable i is that loop. Once the loop is done, i is gone. Also, there’s no good reason to reset the duration variables, because you’re code alwways returns to repeat the loop function, which hard sets those variables to another value based on the button press, so setting them to 0 here is just a wasted command.
Hope this helps.