while loop problem

hello i have a problem whit programming my code . i try to create a while loop that when i push a button my led will blink 3 times . the problem is when i push the button it blinks 4 times and can reset to do it over again

int i = 0;

void setup() {
  pinMode(4, OUTPUT);
  pinMode(8, INPUT_PULLUP);

  Serial.begin(9600);
}

void loop() {
  if (digitalRead(8) == LOW)
  {
    while (i <= 3) {
      digitalWrite(4, HIGH);
      delay(1000);
      digitalWrite(4, LOW);
      delay(1000);
      i++;
    }
  }
}

Walk through the code, line by line. Start with i=0 and count the number of blinks you'll see until the condition you have within the while(...) is false.

0, 1, 2, 3 = 4 blinks.

thnx SteveMann

i have a question now i try to make when i push the button 3 times the led goes on and when i push the button 3 times it goes off i need to make it in a while loop . i try to make a whil pushbutton is pushed 3 times led goes on put it dont work why

int i = 3;

void setup() {
  pinMode(4, OUTPUT);
  pinMode(4, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
  
  if(digitalRead(4) == i);
  while (i==3) 
  {
   
     
    
   digitalWrite(4, HIGH);
  }
}

What happens when you add the new line below to your code?

int i = 0;

void setup() {
  pinMode(4, OUTPUT);
  pinMode(8, INPUT_PULLUP);

  Serial.begin(9600);
}

void loop() {
  if (digitalRead(8) == LOW)
  {
    i = 0;                              // Add this line
    while (i <= 3) {
      digitalWrite(4, HIGH);
      delay(1000);
      digitalWrite(4, LOW);
      delay(1000);
      i++;
    }
  }
}

I would also use while (i < 4) instead of what you have. That’s just a personal preference.

Repo

Don't use while, especially in loop(). It just invites problems.

Just count the button presses then toggle the LED when the count ==2

[b]Pseudo code:[/b]
int i=0;

loop:{
  if (buttonPressed()) i=+1;
  if (i>=2) {
    toggle LED;
    i=0;
  }
}

Depending on the quality of the button, this may appear to not work. You have no debounce, so pressing the button may register as two, or ten button presses. This is why I show buttonPress() as a function where you can check and debounce button presses. Personally, I like to use one of the button libraries.

I may be just expressing my ignorance, but why not a for loop?
Of course, you’s still have to debounce the button…

if(digitalRead(buttonPin)==LOW)
{
for(int i = 0; i < 3: i++)
{
digitalWrite(LEDPin, HIGH);
delay(time);
digitalWrite(LEDPin, LOW);
delay(time);
}
}

JohnDeere630:
I may be just expressing my ignorance, but why not a for loop?
Of course, you’s still have to debounce the button…

if(digitalRead(buttonPin)==LOW)

{
for(int i = 0; i < 3: i++)
{
digitalWrite(LEDPin, HIGH);
delay(time);
digitalWrite(LEDPin, LOW);
delay(time);
}
}

The OP doesn’t want to blink three times, he wants to count button presses, then toggle the LED.

SteveMann:
The OP doesn’t want to blink three times, he wants to count button presses, then toggle the LED.

Oops…I guess I’ll mind my own business from now on, LOL.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.