Push-Button mit mehreren Funktionen

Das IDE Beispiel zeigt auch das entprellen.

hier adaptiert mit Buttons gegen GND

/*
  State change detection (edge detection)

  https://forum.arduino.cc/t/push-button-mit-mehreren-funktionen/927103/6
  
  based on 
  http://www.arduino.cc/en/Tutorial/ButtonStateChange

  by noaisca
*/

// this constant won't change:
constexpr byte buttonPin = A0;    // the pin that the pushbutton is attached to
constexpr byte ledPin = 13;       // the pin that the LED is attached to
constexpr byte noOfStates = 3; 
constexpr byte ON = LOW;          // button closes to GND, ON is LOW
constexpr byte OFF = HIGH;        // 

// Variables will change:
uint8_t buttonPushCounter = 0;   // counter for the number of button presses
bool buttonState = 0;         // current state of the button
bool lastButtonState = 0;     // previous state of the button

void doFunctionA()
{
  Serial.println(F("this is Function A"));
}

void doFunctionB()
{
  Serial.println(F("this is Function B"));
}

void doFunctionC()
{
  Serial.println(F("this is Function C"));
}

void setup() {
  if (ON == HIGH)
      pinMode(buttonPin, INPUT);  // initialize the button pin as without pullup
  else
      pinMode(buttonPin, INPUT_PULLUP);  // initialize the button pin as a input with pullup
  pinMode(ledPin, OUTPUT);           // initialize the LED as an output:
  Serial.begin(115200);              // initialize serial communication:
}

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, increment the counter
    if (buttonState == ON) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
      if (buttonPushCounter >= noOfStates) buttonPushCounter = 0; // rollover
      Serial.println(F("on"));
      Serial.print(F("number of button pushes: "));
      Serial.println(buttonPushCounter);
      switch (buttonPushCounter)
      {
        case 0: doFunctionA(); break;
        case 1: doFunctionB(); break;
        case 2: doFunctionC(); break;
      }
    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println(F("off"));
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;

  // turns on the LED every n button pushes by checking the modulo 
  if (buttonPushCounter % noOfStates == 0) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }
}

warum man zum Forum-How To lesen mehr als eine Stunde braucht verstehe ich zwar nicht, aber seis drumm.