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