Using a for loop to increment counter to 8 and cancel

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.

What makes you think it should?

A for loop need three conditions.
You only have two.
Leo..

No, only one.
Three expressions, maybe?

(The OP's third expression is contained in the function)

the conditional in the for statement counterNumber < 9

I am very new to programming and arduino.

I am trying to figure out how to make the for loop stop at counterNumber 9

When that condition is met, loop() will return, get called again, and counterNumber set to zero, and so on.

I see.

would a second for loop fix this issue?

The great thing about Arduino is how easy it is to find out.

that's what I liked about the forums.

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

thank you.

Replace

With

  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
  }
1 Like

I didn't write "Google it", I wrote "try it" after you suggested a further loop.

If you can't see the difference, then you can foxtrot oscar.

again, no meaningful input.

I said what I said.

How did you get on with the extra loop?

thank you, so much for helping me. It is greatly appreciated.
I had no idea about the switch cases, and probably wouldn't have without your help.

It's good to learn new constructs, but you could have achieved the same result with the construct you already had

ok, elaborate.

Seriously?

Or a simple while(1) after your for loop.

Like I wrote, very easy to try stuff on the Arduino.


 for (counterNumber = 0; counterNumber < 9;){
  buttonSmash();
 }


for (initialization; condition; increment) {
  // statement(s);
}

They're all optional, though I've never quite understood why

for (;;)

Is equivalent to

while(1)

I think for ( ; ; ) is much better looking. :sparkling_heart: