while loop button

hello

i try to create for school a while loop when i push the button 3 times the led goes on , when i push it again 3 times it goes off . Now i have created the code but when i push 3 timesa again it dont go off , could some explain me why it dont work

int i = 0;
void setup() {
  pinMode(4, OUTPUT);
  pinMode(8, INPUT_PULLUP);

  Serial.begin(9600);
}

void loop() {
  while (digitalRead(8) == LOW && digitalRead (4) == LOW)
  {
    if (digitalRead(8) == HIGH)
    {
      if (i == 3);
      digitalWrite(4, HIGH);
    }
   
  while (digitalRead(8) == LOW && digitalRead (4) == HIGH)
  {
    if (digitalRead(8) == LOW)
    {
      if (i == 3);
      digitalWrite(4, LOW);
  }
  }
  }
}
while (digitalRead(8) == LOW && digitalRead (4) == LOW)
  {
    if (digitalRead(8) == HIGH)

How could this be true unless your timing was perfect to the microsecond?

  while (digitalRead(8) == LOW && digitalRead (4) == HIGH)
  {
    if (digitalRead(8) == LOW)

How could this NOT be true unless your timing is perfect to the microsecond?

if (i == 3); Oops

dear TheMemberFormerlyKnownAsAWOL

to what do i have to change it to let it work

The variable i starts off with a value of 0. How is the value of the variable i ever changed? How can i ever equal 3?

The state change detection tutorialhttps://www.arduino.cc/en/Tutorial/BuiltInExamples/StateChangeDetection shows a way to count button presses.

i tried this i cant get it worked toghter in a while loop

opelvectra:
i tried this i cant get it worked toghter in a while loop

Post your modified code so we can look at that.

const int  buttonPin = 8;    // the pin that the pushbutton is attached to
const int ledPin = 4;       // the pin that the LED is attached to


int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}


void loop() {

  buttonState = digitalRead(buttonPin);


  if (buttonState != lastButtonState) {

    if (buttonState == LOW) {

      

    } else {


    }

    delay(200);
  }

  lastButtonState = buttonState;

  while (buttonPushCounter % 3 == 0) {
    digitalWrite(ledPin, HIGH);
  }  {

  }
  while (buttonPushCounter % 3 == 0) {
    digitalWrite(ledPin, LOW);
  }  {

  }

Your code is incomplete

what part do i miss ?

The stuff at the very end.

The stuff that does things to buttonPushCounter

You never increment buttonPushCounter
You have 2 while loops that don't make any sense

dear ToddL1962

how could i create one while loop to have 2 function fot the button

like this
void loop() {

while (buttonPushCounter == 3 )
{
digitalWrite(ledPin, HIGH);

Well, given that buttonPushCounter is currently always zero, the body of such a while loop will never be executed.

  1. Increment buttonPushCounter when the button is pressed
  2. Check to see if buttonPushCounter == 3. If it is, change the state of the LED and set buttonPushCounter = 0.

You do not need any while loops for this.

i understand my teacher want i create a code whit a while loop when i push button 3x led goes on and when 3x it goes off . i know there is a other solutions i need to make it in a while loop .

opelvectra:
i understand my teacher want i create a code whit a while loop when i push button 3x led goes on and when 3x it goes off . i know there is a other solutions i need to make it in a while loop .

You can do it the same way just put a while loop around it. Or you could use 2 while loops. Unless your instructor wants something different. I can't read minds.

Generally, on the Arduino since the loop() function executes over and over again until the Arduino is powered down most people utilize that rather than create separate loops.

If you MUST use while loop(s) you will need to read the button each iteration through the while loop(s) in order to count the pushes.

I can think of multiple ways to implement it using one or more while loops. However, it is your assignment and we should not be doing it for you.

I wil try to make a while loop around it and post it tommorow for advice

int ledPin = 4;           
int buttonPin = 8;        
int buttonState = 0;
int lastbuttonState = 0;

void setup() {

  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {
  
  buttonState = digitalRead(buttonPin);

  //if (buttonState !=lastbuttonState){
    while ( buttonState == 3){
    }
    digitalWrite(ledPin , HIGH);
    }

I tried to make it but i stuck at the while