State Management with Button

I'm trying to do a simple state management using Attiny85 and Arduino Uno, my scenario is as follows.

-Counter starts from 0.
-Counter can go up to 3 at most.
-Certain functions are called according to the state of the counter.

void loop() {
  btnState = digitalRead(button);

  if (btnState != lastState) {
    if (btnState == 1 && counter == 3) {
      counter = 0;
      lastState = btnState;
    }
    else {
      counter++;
      lastState = btnState;
    }
    delay(50);
  }

  if (btnState == 0) {
    if (counter == 1) {
      xyz();
    }
    if (counter == 2) {
      abc();
    }
    if (counter == 3) {
      asd();
    }
  }
}

With this code, once the button is pressed, the counter becomes 1, the function in the first state is called, but then the button becomes dysfunctional.

Welcome to the forum

Please post your complete sketch

How is the button input wired ?
What is the pinMode() of the button input ?

What does your code do when the button is released?
Hint: print the value of "counter" to the serial monitor

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.