const int buttonPin = 2;
const int ledPin = 3;
int counterNumber = 0;
int buttonState = 0;
int lastButtonState = 0;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
for (counterNumber = 0; counterNumber < 9;){
buttonSmash();
}
}
void buttonSmash(){
buttonState = digitalRead(buttonPin);
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);
}
}
this is the code I have currently. It will increment counter and go through the LED changes but it does not stop after 8 button presses. I need to trap the program in an infinite loop after counter reaches 9.
if I was struggling to find the answers on my own, then collaborating and asking experienced individuals to provide me with tips, pointers, tricks etc was the next best option.
as with anything like this, it's always the community that can provide the best help.
I understand learning as an individual is very important.
but so is asking questions.
If I was able to find the answers on my own I wouldn't be in the forums
If you can't provide any meaningful help and just want to say "google it"
then you can leave
switch (counterNumber) {
case 1:
analogWrite( ledPin, 255);
break;
case 2:
analogWrite( ledPin, 150);
break;
case 3:
analogWrite( ledPin, 76);
break;
case 4:
analogWrite( ledPin, 0);
break;
case 5:
analogWrite( ledPin, 255);
break;
case 6:
analogWrite( ledPin, 150);
break;
case 7:
analogWrite( ledPin, 76);
break;
case 8:
analogWrite( ledPin, 0);
break;
case 9:
while (1); // program loops here ad infinitum
}