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?