while loop button

HIGH has the value 1, LOW has the value zero.

Neither will ever equal 3.

if i take a do option i read

int ledPin = 4;           
int buttonPin = 8;        
int i=1;
void setup() {
  
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
}

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

  {
    while ( i == 3){
    }
    do
    {
      digitalWrite(ledPin , HIGH);
    }
    }

Again, you gave i the value 1, which will never be 3 either

i dont understand one push is 1 so 3 pushes is 3

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);
  }

Here is your code after auto formatting (in the IDE, ctrl-t or Tools, Auto Format) and the extra white space removed. Is it not easier to follow?

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);
   }
}

This code:

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

If the counter is a multiple of 3 write the LED HIGH, then right away write the LED LOW. The LED will not be HIGH long enough to notice. An if, else would be better there, I think.

groundFungus

i know i haave to use for my program a while function
so like when i push 3 times the led goes on and if i push again 3 times it goes off

I did make the program in a while function

[ltr][color=#222222][code]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;
    }
  }
}

[/color][/ltr]
[/code]

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