Go Down

Topic: Looping for a set amount of times. (Read 1 time) previous topic - next topic

CrossRoads

I wasn't sure about where to change button to 4 with a for:next, this way seemed clearer to me.
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

AWOL

No, the "while" loop is a classic "for"
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

CrossRoads

Ok, the long hand way of writing it out still works tho, yes?
4:15 on a Friday, feeling a little frazzled ...
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

PaulS

Code: [Select]
while (buttonMode == 3) {
// Do some stuff
button = 4;
}


How are you intending to exit this loop? Changing the value of button has no effect on buttonMode.

Giland

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.


Go Up