Button press and hold/ double press and hold

hi i want to setup a multi function button. for example i would like it to work like this, if i pres the button then let go and pres it again fast i want it to write pin 1 high, then if i double press the button and then hold it i want it to set pin 2 high, then triple press then hold would set pin 3 high. ive been looking at examples but i have not found anything thats showing what im trying to do. i already have the button setup to pull pin A5 low when pressed/pulled high when released

single press then hold activate pin 1
double press then hold activate pin 2
triple press and hold activate pin 3.

you need to write a state machine (and consider debouncing)

J-M-L:
you need to write a state machine (and consider debouncing)

im already using state machine for long/short button presses but now im trying to figure out my first question.

  if (button.currentState != button.prevState) {
    button.currentState = digitalRead(button.pin);
    if (button.currentState == PRESSED) {
      button.counter = millis();
    }

    if (button.currentState == NOT_PRESSED) {

      unsigned long currentMillis = millis();
      if ((currentMillis - button.counter >= shortPress) && !(currentMillis - button.counter >= longPress) ) {

        handleShortPress();
      }
      if ((currentMillis - button.counter >= longPress)) {

        handleLongPress();
      }
      if ((currentMillis - button.counter >= UltraPress))

        handleUltraPress();
    }

    button.prevState = button.currentState;
  }

you can have a look at this oneButton library to see how the state machine was implemented


(This content is part of the http://www.mathertel.de/ web site)