Unify more function buttons

hello, I would like to unify two buttons with a single command.
the code is:

//OCTAVE CONTROL
int octave = 0; //the octave as a global variable.
const int buttonUP = 52; //ButtonUp
const int buttonDOWN = 50; //ButtonDown

void CheckButtonDOWN() {

  static int buttonState;             // the current reading from the input pin
  static int lastButtonState = HIGH;   // the previous reading from the input pin
  static unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
  static unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers

  int reading = digitalRead(buttonDOWN); // read the state of the switch into a local variable:
  if (reading != lastButtonState) {   // If the switch changed, due to noise or pressing:
    lastDebounceTime = millis();  // reset the debouncing timer
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (reading != buttonState) {  // if the button state has changed:
      buttonState = reading;
      if (buttonState == LOW) {
        if (octave > -3) octave--;  // decrease octave to  minimum of -3
      }
      // change the variable that you want changed.
    }
  }

  lastButtonState = reading; // save the reading. Next time through the loop, it'll be the lastButtonState:
}

void CheckButtonUP() {

  static int buttonState;             // the current reading from the input pin
  static int lastButtonState = HIGH;   // the previous reading from the input pin
  static unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
  static unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers

  int reading = digitalRead(buttonUP); // read the state of the switch into a local variable:
  if (reading != lastButtonState) {   // If the switch changed, due to noise or pressing:
    lastDebounceTime = millis();  // reset the debouncing timer
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (reading != buttonState) {  // if the button state has changed:
      buttonState = reading;
      if (buttonState == LOW) {
        if (octave < 4) octave++;  // increase octave to  maximum of 4
      }
      // change the variable that you want changed.
    }
  }

  lastButtonState = reading; // save the reading. Next time through the loop, it'll be the lastButtonState:
}

so, instead typing 2 void functions and repeat 2 times same code for each button,
how can i do to have single code for 2 buttons?

You can make use of an array of structs or classes to store the relevant parameters for the button reading. Then you can write a function to read the button that takes e.g. the index in the array as an argument and works with the specific array element.

Now that you understand how buttons work and what variables are needed to memorize their state you are ready to go to a class or just use one of the button library that is available - there are many to choose from, and it’s also a good way to learn to look at their source code.
May be read a bit about OOP in C++ if you are not familiar with classes before tackling your own button implementation

I think OOP is the way to go.
Just put your function in a class and add a constructor with an initializer list to hand over the pin.
create another "member function" for the parts to be done in setup - and voila ... your class is ready.

Here is an example of a simple button class - roughly based on the debounce example - very similar to your function: button.ino - Wokwi ESP32, STM32, Arduino Simulator

1 Like

small copy/paste bug in the last line

  if (buttonC.wasPressed()) // <=== buttonC not buttonB
    Serial.println(F("ButtonC"));
2 Likes

Thanks so much

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.