Three buttons one function

I have tried this and failed, but I was wondering if I am not doing it correctly. If you have a few pushbuttons, can you use the same function so as not to have duplicated code?

Since each button is doing something different does the code have to be seperated like this or is there a way of combining them and how to know which button runs what function?

void checkPushButton1() 
{
  int reading = digitalRead(button1);
  if (reading != lastButtonState) {
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (reading != buttonState) {
      buttonState = reading;

      if (buttonState == HIGH) 
      {
        // code to turn on some lights
      }
    }
  }

  digitalWrite(ledPin, ledState);

  lastButtonState = reading;
}

void checkPushButton2() 
{
  int reading = digitalRead(button2);
  if (reading != lastButtonState) {
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (reading != buttonState) {
      buttonState = reading;

      if (buttonState == HIGH) 
      {
        // code to run a mator
      }
    }
  }

  digitalWrite(ledPin, ledState);

  lastButtonState = reading;
}

Use a const array (byte) for pin numbers to where the buttons are connected. Use a global array (byte) to store lastButtonState. If you want individual debounce, use a global array (unsigned long) for lastDebounceTime. Pass an index (byte) to "checkPushButton" which is the index in the arrays for which values to use and let the function return a bool which is true if the button is pressed and false otherwise. Should be pretty easy :slight_smile:

This is a great moment to learn about classes:

Code:

---



```
class PushButton {
public:
/** Constructor (executes when a PushButton object is created) */
PushButton(uint8_t pin)
: pincolor=#000000[/color] {} // remember the push button pin

/** Enable the internal pull-up resistor */
    void begincolor=#000000[/color] const { pinMode(pin, INPUT_PULLUP); }
   
    /** Read the button state check if the button has been pressed, debounce the button */
    bool isPressedcolor=#000000[/color] {
      bool pressed = false;
      bool state = digitalReadcolor=#000000[/color];              // read the button's state
      int8_t stateChange = state - previousState;  // calculate the state change since last time

if (stateChange == falling)                          // If the button is pressed (went from high to low)
        if (milliscolor=#000000[/color] - previousBounceTime > debounceTime)    // check if time since last bounce is longer than threshold
          pressed = true;                                      // the button is pressed
      if (stateChange == rising)                          // if the button is released or bounces
        previousBounceTime = milliscolor=#000000[/color];                      // remember when this happened

previousState = state; // remember the current state
      return pressed;        // return true if the button was pressed and didn't bounce
    }

const uint8_t pin;
   
  private:
    bool previousState = HIGH;
    unsigned long previousBounceTime = 0;

const static unsigned long debounceTime = 25;
    const static int8_t rising = HIGH - LOW;
    const static int8_t falling = LOW - HIGH;
};

// ---------------------------------------------------------------------------------------------------------------------- //

PushButton buttons[] = { 2, 3, 4 };        // Create PushButton objects on pins 2, 3 and 4

void setupcolor=#000000[/color] {
  Serial.begincolor=#000000[/color];
  for (auto &button : buttons)
    button.begincolor=#000000[/color];
}

void loopcolor=#000000[/color] {
  for (auto &button : buttons)
    if color=#000000[/color] {
      Serial.print("Button on pin #");
      Serial.printcolor=#000000[/color];
      Serial.println(" pressed!");
    }
}
```

|

Pieter

Thanks a lot guys.

Pieter, I sure do appreciate that example.

John

PieterP:
This is a great moment to learn about classes:

Pieter,

I just wanted to say thank you very much for that class, it works GREAT!!

John.

Glad to hear!