MIDI Controller: More Buttons (Arrays & Bounce2-Library )

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");
  }
}

Hi Philip,

Look for arrays.
Regards, Franz-Peter

I would consider to start using : Control-Surface.h

Thanks @MicroBahner , I tried several ways but can’t really get this solved. Will keep trying.

Thanks @Deva_Rishi , I’ll try that. Can’t remember why I didn’t before

/*8 Push-Buttons connected to Teensy 4.0 (PIN 4-11),  Array*/
#include <Bounce2.h>

const byte buttonPins[] = { 4, 5, 6, 7, 8, 9, 10, 11 };
const byte buttonCnt = sizeof(buttonPins);
Bounce button[buttonCnt] ; 

void setup() {
  Serial.begin(9600);
  for ( byte i = 0; i < buttonCnt; i++ ) {
    button[i].attach( buttonPins[i], INPUT_PULLUP );
    button[i].interval(5);   // debounce time
  }
}

void loop() {
  for ( byte i = 0; i < buttonCnt; i++ )   button[i].update();

  for ( byte i = 0; i < buttonCnt; i++ ) {
    if (button[i].fallingEdge()) {
      Serial.print("Button "); Serial.print(i); Serial.println(" was pressed");
    }
    if (button[i].risingEdge()) {
      Serial.print("Button "); Serial.print(i); Serial.println(" was released");
    }
  }
}
2 Likes

Thanks @MicroBahner ! In the moment you were posting the code I found a solution in one of the examples. But yours is even better (shorter)!
This was my first post here and I am quite excited. Thanks again!

You’re welcome. And have fun. :sunglasses:

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