const int ledPin = 3;
const int buttonPin = 2;
int buttonState = 0;
int counterNumber = 0;
int lastButtonState = 0;
void setup() {
pinMode(buttonPin,INPUT);
pinMode(ledPin,OUTPUT);
Serial.begin(9600);
}
void loop() {
while (counterNumber < 9){
buttonSmash();
}
}
void buttonSmash(){
buttonState = digitalRead(buttonPin);
counterNumber = 0;
if (buttonState != lastButtonState){
if (buttonState == HIGH){
counterNumber++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(counterNumber);
}else {
Serial.println("off");
}
delay(50);
}
lastButtonState = buttonState;
if (counterNumber == 1){
analogWrite(ledPin, 255);
}
if (counterNumber == 2){
analogWrite(ledPin, 150);
}
if (counterNumber == 3){
analogWrite(ledPin, 76);
}
if (counterNumber == 4){
analogWrite(ledPin, 0);
}
if (counterNumber == 5){
analogWrite(ledPin, 255);
}
if (counterNumber == 6){
analogWrite(ledPin, 150);
}
if (counterNumber == 7){
analogWrite(ledPin, 76);
}
if (counterNumber == 8){
analogWrite(ledPin, 0);
}
}
I am trying to write a program that only uses one while statement in my void loop, to turn an LED on using PWM so for button press #1 LED = 255 (100%), second press is 60% and third is 30% the fourth press should be off, I then want this loop to repeat one more time and then cancel out.
I think I almost have it down, however in the code i provided, my counter only goes from 0 to 1 and does not increment further.
not sure where the issue is in my code but any pointers would be absolutely amazing, thank you in advance.