Hi People.
I am new to programming and i am trying to make a board that (when pushed by a button)
starts a blinking sequence that looks like one that is used when there is a car-race. (red-red-red-green)
Now my question is how do i program the arduino so that if the button is pushed it starts the sequence 1 time?
i hope someone can help me.
Here is some pseudocode. Put it in the setup() function.
do { read input button } while input button is not pushed
turn red1 on
delay
turn red1 off
turn red2 on
delay
turn red2 off
turn red3 on
delay
turn red3 off
turn green on
PaulS:
No, because you said:Once the (psuedo)code is done, setup() ends, and loop() does nothing. To get the switch to do something again, you need to reset the Arduino.
I know, but the OP didn't ask for that. In fact I was deliberately taking it literally,
so that if the button is pushed it starts the sequence 1 time?
It's my way of forcing people to think about what they ask for.
Also the light tree logic would be different for multiple cycles. The tree shouldn't pause at "green" for a new race. Instead, initially all the lights should go out or at least the top red should be on (I don't know, I don't follow NASCAR). But all this should be fleshed out by the OP as a design learning experience.
This is why we program in C, not english. People quibbling over ambiguities like:
"so that [if the button is pushed it starts the sequence] 1 time?"
or
"so that if the button is pushed [it starts the sequence 1 time]?"
But in answer to the OP's question:
The easy way is to use delay(). In the loop, if the button is down AND the button was up last time the loop was executed, then do the flash sequence. It might be a good idea to move the flash sequence into a separate function. This can make the logic of the button-detection code clearer.