Push button going through states with different actions

Hello all, I’m new to Arduino and don’t have a lot of programming experience from before, so this is probably a simple one, but here goes:
I want a push button to go through 3 phases doing 3 different things. When it’s first pressed it would make a LED fade from 0 to 85, then from 85 to 170 and lastly 170 to 255.

What I got so far (bits and pieces from here and there and my own code) is:

// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 9;       // the pin that the LED is attached to
const int fadeAmount = 1;    // how many points to fade the LED by

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button
int brightness = 0;    // how bright the LED is

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed
    if (buttonState == HIGH) {
      //if the counter is 0, increment and fade to 85
      if (buttonPushCounter == 0) {
        buttonPushCounter++;
          while (brightness < 85) {
            analogWrite(ledPin, brightness);
            brightness = brightness + fadeAmount;
            Serial.println("Brightness: ");
            Serial.println(brightness);
          }
        Serial.println("on");
        Serial.print("number of button pushes: ");
        Serial.println(buttonPushCounter);
      }
      //if the counter is 1, increment and fade to 170
      if (buttonPushCounter == 1) {
        buttonPushCounter++;
        while (brightness < 170) {
            analogWrite(ledPin, brightness);
            brightness = brightness + fadeAmount;
            Serial.println("Brightness: ");
            Serial.println(brightness);
        }
    } 
    else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off"); 
    }
  }
  // save the current state as the last state, 
  //for next time through the loop
  lastButtonState = buttonState;
}

Now the problem is that of course the if (buttonPushCounter == 1) { becomes useless since it’s being set to 1 in the if (buttonPushCounter == 0)-statement. I can’t get my head around how to do this so, anyone got any ideas?

Hello and welcome :slight_smile:

Increase buttonPushCounter anytime the button is pushed, and use the modulo operator to reset it when you want, only then, compare it's value with whatever you want (I prefer using switch instead of if)

buttonPushCounter ++;
buttonPushCounter %= 3;

switch (buttonPushCounter)
{
    case 0:
    {
        //whatever
        break;
    }
    case 1:
    {
        //whatever
        break;
    }
    //etc
}

Edit: I forgot to say that you need to intitialize buttonPushCounter to -1..or move the increment/modulo operations AFTER the switch.

Edit2: Actually your problem is because you use if .. if, instead of if .. else if.

guix:
Edit2: Actually your problem is because you use if .. if, instead of if .. else if.

Wow, I feel stupid now. Of course it is! Thank you.