How to make the led blink 3x when pressing button using a while loop?

So I made it with a for loop, but now I want to make a while loop as well. What do I have to change to make it work?

My for loop:

void loop (){
int i=0;

b = digitalRead(BUTTON1);

if (b == LOW)
{
for (i = 1; i <= 3; i=i+1)
{
digitalWrite(LED1, HIGH);
delay(1000);
digitalWrite(LED1, LOW);
delay(1000);

Oh I tried this btw, but it doesnt work :stuck_out_tongue:

My attempt while loop:

if (b == LOW)
{
do {
digitalWrite(LED1, HIGH);
delay(1000);
digitalWrite(LED1, LOW);
delay(1000);
} while (i<3);

I just found out while loop and do while loop are 2 different things...
I want it to be a while loop though.

Oh I tried this btw, but it doesnt work :stuck_out_tongue:

My attempt while loop:

if (b == LOW)
{
do {
digitalWrite(LED1, HIGH);
delay(1000);
digitalWrite(LED1, LOW);
delay(1000);
} while (i<3);

Because i will always be 0, which is less than 3, it will loop forever.

Try:

 do { ... } while (++i<3);

I just found out while loop and do while loop are 2 different things…
I want it to be a while loop though.

They are almost the same. With while, the test is done before the loop starts, so the loop may not run even once. With do…while, the test is done at the end of the loop, so the loop always runs at least once.

PaulRB:
Because i will always be 0, which is less than 3, it will loop forever.

Try:

 do { ... } while (++i<3);

They are almost the same. With while, the test is done before the loop starts, so the loop may not run even once. With do…while, the test is done at the end of the loop, so the loop always runs at least once.

Oh i can’t thank you enough! :slight_smile:
Do you perhaps also know how i can: press the button 3x so the led turns on (and also turn it off by pressing it 3x)? I dont know whats easier for this one, a for or while loop. And I’m not sure how to write the code. I think I have to change the first line, thats for sure, but how? :
if (b == LOW) {
do {
digitalWrite(LED1, HIGH);
delay(1000);
digitalWrite(LED1, LOW);
delay(1000);
}

while (++i<3);

When you know in advance, or can calculate in advance, how many times the loop should run, it's more normal to use a for-loop. When you don't know how many times, a while-loop is more usual. To be honest, using do-while loops is quite rare, I can't remember last time I used one.

To do what you want now, the code will need to change quite a lot. You will need to use both for-loops and while-loops. You know how many presses you want, so use a for-loop for that. What you don't know is how long it will be before the button is pressed, or how long it will be pressed for, for each of the 3 presses. So use while-loops for that.

You will also face, maybe for the first time, the problem of buttons bouncing. If you don't know about buttons bouncing, it's like dropping a tennis ball onto a hard surface. It takes several bounces before the ball stops moving. Inside a button, you have two contacts held apart by a spring. When your finger pushes the button, the contacts close, but bounce apart again, then close again, and bounce again several times before they settle. You don't want those bounces to count as your 3 presses. The tennis ball will take several seconds to settle, but the button will only take a few hundredths of a second. So you can ignore those switch bounces by using, say, delay(20). By the time this 20ms have passed, the switch should have settled.

uwuxxx:
I want it to be a while loop though.

Well, no you actually don't! :astonished:

Two structures in code need to be avoided, because the "block" or prevent other necessary things happening. One is "delay"; the other is "while".

Study (at least) this reference and this.

PaulRB:
When you know in advance, or can calculate in advance, how many times the loop should run, it's more normal to use a for-loop. When you don't know how many times, a while-loop is more usual. To be honest, using do-while loops is quite rare, I can't remember last time I used one.

To do what you want now, the code will need to change quite a lot. You will need to use both for-loops and while-loops. You know how many presses you want, so use a for-loop for that. What you don't know is how long it will be before the button is pressed, or how long it will be pressed for, for each of the 3 presses. So use while-loops for that.

You will also face, maybe for the first time, the problem of buttons bouncing. If you don't know about buttons bouncing, it's like dropping a tennis ball onto a hard surface. It takes several bounces before the ball stops moving. Inside a button, you have two contacts held apart by a spring. When your finger pushes the button, the contacts close, but bounce apart again, then close again, and bounce again several times before they settle. You don't want those bounces to count as your 3 presses. The tennis ball will take several seconds to settle, but the button will only take a few hundredths of a second. So you can ignore those switch bounces by using, say, delay(20). By the time this 20ms have passed, the switch should have settled.

Thanks for your help! :smiley: I tried it but unfortunately I'm not skilled enough so I failed ): Maybe another time hopefully!

Paul__B:
Well, no you actually don't! :astonished:

Two structures in code need to be avoided, because the "block" or prevent other necessary things happening. One is "delay"; the other is "while".

Study (at least) this reference and this.

Oh! Good to know! Thank you :smiley:

@uwuxxx

Other post/duplicate DELETED
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

If you want something to happen after a certain number of button presses, you should look at the state change detect tutorial. The example there counts to 4 and turns an led on every 4 clicks, otherwise off.

So for you case where it seems you want to turn blinking on and off every 3 clicks, I would use that approach to toggle a boolean variable every 3 clicks. Then have the blink without delay code in a function, and if the boolean is set, call the blink function; if the boolean is clear, turn the led off.

ballscrewbob:
@uwuxxx

Other post/duplicate DELETED
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

Excuse me but what do you mean? My other question had nothing to do with this one. This one is mainly about how to make a while loop. The other one had 0 to do with while loops, so may been easier/harder to answer for some experts and thats why I decided to make a new forum instead of posting it here on my while loop forum. + Some other not experienced programmers might have the same question but will not look for the answer here, because again, it has nothing to do with this topic on this forum. The only similarities between those questions were the words/number '3', 'led' and 'button', but that doesnt make it the same. Thank you for your understanding.

Paul__B:
Two structures in code need to be avoided, because the "block" or prevent other necessary things happening. One is "delay"; the other is "while".

Short delays (like for debouncing a button) and short while loops (like searching through an array) are generally fine. It's longer delays and while loops that wait on external events or contain delays that cause blocking.

void-basil:
So for you case where it seems you want to turn blinking on and off every 3 clicks...

The OP did not say that, only to turn the led on and off after 3x presses. Maybe that's not what they meant, we will see. But for what they said, turning the led on after 3 presses and off after 3 presses, using blocking code will not be a problem.

Right now, the OP is at a very early stage of learning to code, getting to grips with different kinds of loops etc, and as you can see from reply #6, their confidence is fragile. So let's help them to cross one bridge at a time, not scare or confuse them with advice about bridges they haven't even got to get.

PaulRB:
The OP did not say that, only to turn the led on and off after 3x presses.

That’s what I thought originally too, but the code here still has the on and off code to blink the led in there, which made me think the 3x clicks were actually meant to turn the blinking on and off, not just toggle the led.

Do you perhaps also know how i can: press the button 3x so the led turns on (and also turn it off by pressing it 3x)? I dont know whats easier for this one, a for or while loop. And I’m not sure how to write the code. I think I have to change the first line, thats for sure, but how? :
if (b == LOW) {
do {
digitalWrite(LED1, HIGH);
delay(1000);
digitalWrite(LED1, LOW);
delay(1000);
}

while (++i<3);

But you’re right, it’s not clear, and that’s actually why I said “it seems”…

(And anyway, even if OP's intention is simply to toggle the led, not toggle its blinking, after 3x presses, state change detection using % ala the example is still the way to go, even for a beginner. It's delay()-less (including for()-less and while()-less, apart from the ersatz de-bounce), and that's undoubtedly a good habit to start with. )

uwuxxx:
I tried it but unfortunately I'm not skilled enough so I failed

It's hard to help you if you don't show us your attempt, or even describe what went wrong.

Let's break down what you are trying to do. You want to wait for 3 presses before switching on the led. So you need a loop that will repeat 3 times, a for-loop. Inside that loop, you need to wait until the button is pressed. So another loop, checking the button over and over until it is pressed. You don't know how many repeats will be required for this, so use a while-loop. After the button is pressed and the while-loop stops repeating, you then need to wait until the button stops bouncing, so put a short delay. Next you need to wait until the button is released, another while-loop, checking the button until it is no longer pressed. After that, another short delay in case of bouncing. These steps are inside the for-loop, that loop will continue until the 3rd press and release have completed. Then you can turn the led on.

As long as the led is on after 3 presses I'm happy :smiley: So whatever is easier is what I will try and maybe later I can try the other one as well.

So the thing is I have absolutely no idea how to make a for loop for the button. I can't find it anywhere. The only thing I can think of is:

for (b = 3)
{
while (i=

But I feel like that's super wrong. (I didn't finish the while loop sentence because I'm still thinking what to write there. I hope I can figure that one out myself)

I did a quick sketch to toggle the led every 3 presses using state change detect, will post it if you want it (but you might want to try a bit longer yourself and not be able to "peek" at a working example, your choice.)

The problem with using fors and whiles and wotnot, is that if you then ever want to expand that to 2 buttons/leds, it will be impossible to make it responsive. When it's in one of those blocking parts it won't be looking at the rest of the code. The state change detect example lets loop() do all the looping, and does no blocking. (Apart from the very short debounce delay()).

void-basil:
I did a quick sketch to toggle the led every 3 presses using state change detect, will post it if you want it (but you might want to try a bit longer yourself and not be able to "peek" at a working example, your choice.)

The problem with using fors and whiles and wotnot, is that if you then ever want to expand that to 2 buttons/leds, it will be impossible to make it responsive. When it's in one of those blocking parts it won't be looking at the rest of the code. The state change detect example lets loop() do all the looping, and does no blocking. (Apart from the very short debounce delay()).

To be honest the most effective way of learning for me is to see an example code and then figure out myself what it all means and does and then try it myself. So I'd really appreciate it if you shared it :slight_smile:
That's very good to know. Thank you for letting me know!

uwuxxx:
I have absolutely no idea how to make a for loop for the button

But you do know! From your original post:

for (i = 1; i <= 3; i=i+1) {....

PaulRB:
But you do know. From your original post:

for (i = 1; i <= 3; i=i+1) {....

Yes but that was for the leds, wasnt it? Now I have to make it for the button which seems way more difficult