This line creates a new, different, variable called reading2, which only exists within the curly braces of the loop.
The while test will look at the original, reading2 variable, which not been changed after you read it the first time. Therefore, it will never become false and the loop will not exit.
dear PaulS,
I thought it was sufficently simple to understand.
as I said 'one button, one led'.
when button is pressed, the led blink until button is pressed again.
that's all.
saying 'it doesn't work' means the 2nd press on the button doesn't stop the led.
any idea?
Well your second version solves one problem. What exactly does it do now ?
If you press the button and the first digitalRead( ) is HIGH. [ You are assuming that is equal to 1 ]
then the do...while statement will run at least once. When the blink3( ) has been done the first time, then the loop will continue for as long as it sees that the button is NOT pressed ( toto==0 ).
This seems to be the OPPOSITE logic of what you were trying to do in the original post. Is that what you intended ?
Anyway, the construct
if ( some condition ) do { something } while ( some condition )
is exactly the same as
while ( some condition ) { do something }
except it is more verbose and requires the code to detect and compare the condition, twice.
You need to detect when the button becomes pressed rather than when it is pressed. Look at the StateChangeDetection example in the IDE to see how to do it.
I am glad it works. Now improve it by writing it without any delay()s that block reading the input for a fifth of a second each time loop() repeats. Learn how to do it now before the use of delay() is ingrained in your programming.
UKHeliBob:
I am glad it works. Now improve it by writing it without any delay()s that block reading the input for a fifth of a second each time loop() repeats. Learn how to do it now before the use of delay() is ingrained in your programming.
See the 'Blink without delay' example in the examples page of this site, or in the IDE.
Just to be clear, in the do...while loop construct, if the condition at the end evaluates to true, then the loop is repeated again. If the condition is false, the loop exits.