help to finish my code to see what i do wrong

int buttonPin = 8;
int ledPin = 4;
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;

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

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

if (buttonState != lastButtonState)
{
if (buttonState == HIGH)
{
buttonPushCounter++;
}
else
{

}
delay(300);
}

lastButtonState = buttonState;

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

could someone help me to create a good program , i stuck atm . my question is when i push 3 times the button the led goes on when i push again 3 times the led goes off. i need to do it in a while function

Hello
And why do you have to use the while statement inside the loop?

i just started programming and i have some tests to make i need to use a while function to see you can program more ways instead if else ...

Hello,
in this case study the while statement in detail using the Arduino reference.
Good luck

i did it whitout the button push like this but when i try to create the button in it , it dont work

var = 0;
while (var < 200) {
  // do something repetitive 200 times
  var++;
}

It is a bit pointless to use while in this program with that basic funcionality.
Loop is while cycle like while(1){ } so it is running forever as cycle.

While you should use at other situations such as...
While button is pressed, count time (how long is button held, how long until some action will do, such as: if button is held one second, turn on led) and so on...

The easiest way to do funcionality that you required:

//Author: martinius96
//Payment for solution of your PAID forum Thread can be sent to: https://paypal.me/chlebovec

const int buttonPin1 = 8;
int ledPin = 4;
int buttonPushCounter = 0;
int buttonState1 = HIGH;
int lastButtonState1 = HIGH;
boolean ledState = false;
unsigned long lastDebounceTime1 = 0;
unsigned long debounceInterval = 50;
void setup()
{
  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, ledState);
}

void loop() {
  int reading1 = digitalRead(buttonPin1);
  if (reading1 != lastButtonState1) {
    lastDebounceTime1 = millis();
  }

  if ((millis() - lastDebounceTime1) > debounceInterval) {
    if (reading1 != buttonState1) {
      buttonState1 = reading1;
      if (buttonState1 == HIGH) {
        buttonPushCounter++;
        if (buttonPushCounter == 3) {
          ledState = !ledState;
          digitalWrite(ledPin, ledState);
          buttonPushCounter = 0;
        }
      }
    }
  }
  lastButtonState1 = reading1;
}

martinius96

Thank you for te message , the if statement works i try to make it in a while loop while the button is push 3 times the led goes on . then i push again 3 times it would be 6 the led goes of and the counter reset to 0 .

Yes it is working exactly like that.
3 pushes, led on, next 3 pushes led off. Again 3 pushes, led on, again 3, led off :slight_smile:

but i dont see any while like a while loop i tried to make it now in a while loop where i use a while function

int buttonPushCounter = 0;
int lastButtonStatus = HIGH;
int buttonStatus = HIGH;
boolean ledStatus = false;
void setup()
{
  pinMode(8, INPUT_PULLUP);
  pinMode(4, OUTPUT);
  digitalWrite(4, ledStatus);

}

void loop() {
  delay(100);
  int count = digitalRead(8);
  while (count != lastButtonStatus) {

    if (count != buttonStatus) {
    buttonStatus = count;
    if (buttonStatus == HIGH) {
    buttonPushCounter++;
    if (buttonPushCounter == 3) {
    ledStatus = !ledStatus;
    digitalWrite(4, ledStatus);
    buttonPushCounter = 0;
    }
    } 
    lastButtonStatus = count;
    }
  }
}

You will only ever run that while loop once... as you set count to be equal to lastButtonStatus.

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