Imagine your states are:
Idle
Any_button_seen
Button1_flashing
Button_2_flashing
What would make the transition idle->Any_button_seen? How would you decide to go to the state Button1_flashing?
If you were in the state Button1_flashing, what should the Arduino do? How would it decide to finish flashing (hint: look at blinkwithoutdelay example)
When finished flashing, what state do you think it should go back to?
Look up state machine.
Try drawing out the state machine on a sheet of paper. What might make it move from one state to another?
You will need a variable representing a state, and a switch statement.
I am guessing you are learning and just building your own sketches to verify you understand (basically, that is where I am).
the for loop works if you set it up as a function instead of inside the void loop.
so for example
void loop ()
{check for a button press,
if received
check the state
kick out to the function that handles that
}
function state1
{do stuff}
function state2
{do different stuff}
Your for loop would be setup in the function state1 or 2.
When the if loop is done, it would automatically return to void loop and look for another button press.
you might also want to look at debounce (search debounce arduino)
I had the issue of my button press being seen more than once changing my state rapidly. Originally I used a delay, but that basically just paused the program instead of keeping multiple button presses from occurring.
Giland:
the for loop works if you set it up as a function instead of inside the void loop.
When the if loop is done, it would automatically return to void loop and look for another button press.
I can't interpret either of those sentences in any way that makes sense.
how about
the for command works if used in a separate function instead of being inside the void loop().
when the if loop has i in the function reach 5 (no longer less than 5), it will return back to the void loop() and wait for another button press.
for example
void loop ()
{// general code to look for button press, current state, etc etc
if (button == 3) buttonstate3;
}
int butttonstate3()
{
{for(int i = 0; i < 5; i++)
{
Serial.println(i);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
delay(400);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
delay(400);
}
button = 4;
}}
the for command works if used in a separate function instead of being inside the void loop().
when the if loop has i in the function reach 5 (no longer less than 5), it will return back to the void loop() and wait for another button press.
No, that is gibberish too. Are you deliberately trying to confuse people?