Defining Variable Within A Loop

Hello,

I am working on a project where I control 4 LEDs using a pushbutton. The button cycles the 4 LEDs through different patterns of flashing. This is the code for the variable, which I call counter (which counts the number of times the button has been pressed up to 4, then resets to 1)

int switchPin =2; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status
int counter = 0;
int currentState = 0;
int previousState = 0;

void setup() {
pinMode(switchPin, INPUT); // declare pushbutton as input
Serial.begin(9600);
}

void loop(){
val = digitalRead(switchPin); // read input value
if (val == HIGH) { // check if the input is HIGH (button released)
currentState = 1;
}
else {
currentState = 0;
}
if(currentState != previousState){
  if(currentState == 1){
    if(counter > 3){
      counter =1;
    }
    else{
      counter ++;
    }
Serial.println(counter);
}
previousState = currentState;
}
}

This works fine, and serial prints as it should. My issue is when I need to call this variable, I am using switch case:

switch (counter) {
  case 1;
    do
      {
        //check if counter still equals 1
        //mode 1
      } while (counter = 1);
  break;

  case 2;
    do
      {
        //check if counter still equals 2
        //mode 2
      } while (counter = 2);
  break;
  
  case 3;
    do
      {
        //check if counter still equals 3
        //mode 3
      } while (counter = 3);
  break;
  
  case 4;
    do
      {
        //check if counter still equals 4
        //mode 4
      } while (counter = 4);

I need a way to check if the counter is still at the correct value in the "do" loop. But, my variable is not defined by reading a pin, so I cannot reset the variable at the start of each loop. Any idea how I can check to the counter value at the beginning of the "do" loop in each case?

Thanks!

while (counter = 1);

Assignment, not comparison.

C 101 fail.

This is the code for the variable, which I call counter (which counts the number of times the button has been pressed up to 4, then resets to 1)

instead of calling it something meaningful, like switchPressCount?

My issue is when I need to call this variable

You don’t “call” variables. You call functions.

I need a way to check if the counter is still at the correct value in the “do” loop.

What is going to change the value of the poorly named variable, while the do loop is running?

Any idea how I can check to the counter value at the beginning of the “do” loop in each case?

Don’t use the stupid do/while statement. A while statement performs the check first, not last.

if you are counting button presses then once counter >=4 it should stay at 4 not reset to 1

understand that a switch statement is not a loop. every program pass will go though the complete loop until it hits the switch statement then will jump to the case and run until it hits the break; then bypass any other cases until it gets to the next line after the switch.

So 4 presses will get you to case 4: run what ever code you like then simply make the last pass when everything is complete (maybe use a millis timer here) a if timer>??? minus one from counter and reset timer. now on the next pass it will run what ever is in case 3: repeat the timer and minus and after 3 has completed it will go to case2:

write some garbage in case:0 to shut it down until the button is pressed again.

This is not good code but it will get you working with the switch statement and you should also learn how to make a millis timer (search for millis timer examples)

if you are learning and interested you can change a little code and remove the switch and try while and if instead.

What is going to change the value of the poorly named variable, while the do loop is running?

This is massively important. The answer lay in having a sketch that is entirely non-blocking. This means using timers to control your four flashing patterns. That way you can flash your leds and still read the button. Once you figure this out you will likely find that the loop() function is all the looping you need.