Go Down

Topic: Three buttons one function (Read 213 times) previous topic - next topic

JohnRandalls

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?


Code: [Select]
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;
}

Danois90

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 :)
Instead of mocking what's wrong, teach what's right! ;)
When you get help, remember to thank the helper and give some karma!
Please, do NOT send me any private messages!!

PieterP

#2
Jan 22, 2019, 02:24 pm Last Edit: Jan 22, 2019, 02:28 pm by PieterP
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)
      : pin(pin) {} // remember the push button pin

    /** Enable the internal pull-up resistor */
    void begin() const { pinMode(pin, INPUT_PULLUP); }
   
    /** Read the button state check if the button has been pressed, debounce the button */
    bool isPressed() {
      bool pressed = false;
      bool state = digitalRead(pin);               // 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 (millis() - 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 = millis();                       // 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 setup() {
  Serial.begin(115200);
  for (auto &button : buttons)
    button.begin();
}

void loop() {
  for (auto &button : buttons)
    if (button.isPressed()) {
      Serial.print("Button on pin #");
      Serial.print(button.pin);
      Serial.println(" pressed!");
    }
}
Pieter

JohnRandalls

Thanks a lot guys. 

Pieter, I sure do appreciate that example.


John

JohnRandalls

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.

PieterP


Go Up