Dear folks,
I am starting to build a Arduino- (or Teensy-)based MIDI-Controller. My first prototype with a few buttons, LEDs and a motorfader was already big fun. Now I am trying to expand it a little bit.
So currently I am struggeling with building an array for more buttons: I connected 8 pushbuttons and would like to simplify the attached code, while using the "Bounce2"-Library
The code below works perfectly but can anyone give me a hint how to shorten it?
Thanks and best wishes
Philip
/*8 Push-Buttons connected to Teensy 4.0 (PIN 4-11), Array*/
#include <Bounce2.h>
Bounce button1 = Bounce(4, 5); // Pin 11, 5 = 5 ms debounce time
Bounce button2 = Bounce(5, 5); // Pin 12, 5 = 5 ms debounce time
Bounce button3 = Bounce(6, 5); // Pin 12, 5 = 5 ms debounce time
Bounce button4 = Bounce(7, 5); // Pin 12, 5 = 5 ms debounce time
Bounce button5 = Bounce(8, 5); // Pin 12, 5 = 5 ms debounce time
Bounce button6 = Bounce(9, 5); // Pin 12, 5 = 5 ms debounce time
Bounce button7 = Bounce(10, 5); // Pin 12, 5 = 5 ms debounce time
Bounce button8 = Bounce(11, 5); // Pin 12, 5 = 5 ms debounce time
void setup() {
Serial.begin(9600);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
pinMode(11, INPUT_PULLUP);
}
void loop() {
button1.update();
button2.update();
button3.update();
button4.update();
button5.update();
button6.update();
button7.update();
button8.update();
if (button1.fallingEdge()) {
Serial.println("Button 1 was pressed");
}
if (button1.risingEdge()) {
Serial.println("Button 1 was released");
}
if (button2.fallingEdge()) {
Serial.println("Button 2 was pressed");
}
if (button2.risingEdge()) {
Serial.println("Button 2 was released");
}
if (button3.fallingEdge()) {
Serial.println("Button 3 was pressed");
}
if (button3.risingEdge()) {
Serial.println("Button 3 was released");
}
if (button4.fallingEdge()) {
Serial.println("Button 4 was pressed");
}
if (button4.risingEdge()) {
Serial.println("Button 4 was released");
}
if (button5.fallingEdge()) {
Serial.println("Button 5 was pressed");
}
if (button5.risingEdge()) {
Serial.println("Button 5 was released");
}
if (button6.fallingEdge()) {
Serial.println("Button 6 was pressed");
}
if (button6.risingEdge()) {
Serial.println("Button 6 was released");
}
if (button7.fallingEdge()) {
Serial.println("Button 7 was pressed");
}
if (button7.risingEdge()) {
Serial.println("Button 7 was released");
}
if (button8.fallingEdge()) {
Serial.println("Button 8 was pressed");
}
if (button8.risingEdge()) {
Serial.println("Button 8 was released");
}
}