Need Help.

I am new to the coding world and have been trying to learn as much as I can but I have been working on a project on and off for over a year now and I need some help.

I have made a usbMidi note controller. It works very well but I would like to add banks to this pedal.. In example I would have 8 buttons, 7 buttons would have a midi note assigned to them (1,2,3,4,5,6,7). I would like for button number 8 to scroll up through the different banks so that the 7 buttons would have different notes in the different banks. Bank 1 (1,2,3,4,5,6,7). Bank 2 (11,22,33,44,55,66,77) ext.

I have spent hours looking into the different ways of doing this and have accomplished nothing but confusing myself. I have looked deeply into 2D arrays but don't really understand it. Below is a sample code of what I already have that does work. any and all help is very much appreciated.

#include <Bounce.h>
#include <SoftwareSerial.h>
#define txPin 1

const int channel = 1;

Bounce button14 = Bounce(14, 100);
Bounce button15 = Bounce(15, 100);

void setup(){

pinMode(14, INPUT_PULLUP);
pinMode(15, INPUT_PULLUP);
}

void loop(){

button14.update();
button15.update();

if (button14.fallingEdge()) {
usbMIDI.sendNoteOn(24, 99, channel); // 24 = C
}

if (button15.fallingEdge()) {
usbMIDI.sendNoteOn(29, 99, channel);
}

if (button14.risingEdge()) {
usbMIDI.sendNoteOff(24, 0, channel); // 24 = C
}

if (button15.risingEdge()) {
usbMIDI.sendNoteOff(25, 0, channel); // 25 = C#
}

What Arduino are you using?

What Bounce library are you using?

Add a closing bracket for loop(). Add code tags for your code.

I'm using a teensy 2.0.

I'm not sure what bounce library exactly.

I typed in that sample of the code so I just left that at. The code I sampled that from works like it should when uploaded to the controller.

Someone with 9 posts should know that using code tags is part of posting properly. So is reading the 3 locked topics and anything to which they point.

Oh, and most people here "Need Help", so a title of "Need Help" just wastes the time of people who might provide free help.

have a global variable holding the current bank number.
Pedal 8 increments that number.
The other pedals use the current bank number to work out what note event to send.

Oh - be careful about what happens if the bank changes while a note is still down. May happen if someone is using two feet.

JohnW: I read your reply last night while eating dinner with my wife and didn't realize you were needing the code tags and () brackets for you to be able to understand what was there so I apologize for my response if it came across wrong. I will get the whole code posted later when I get back to my computer.

PaulM: thank you for your reply, I will look into how to do that.

To use a two-dimensional array:

const byte Notes[7][] = {{1,2,3,4,5,6,7}, {11,22,33,44,55,66,77}};
void loop() {
    for (int button = 0; button < 7; button++) {
         Buttons[button].update();

        if (Buttons[button].fallingEdge()) {
            usbMIDI.sendNoteOn(Notes[button][Bank], 99, channel);
        }

        if (Buttons[button].risingEdge()) {
            usbMIDI.sendNoteOff(Notes[button][Bank], 99, channel);   
        }
    }
}

JohnW: Thank you for your code sample. It took me some poking around but it is starting to make some since.. Here is what i got so far and I added in all of the Code Tags from my original code in the same order as they are.

As of right now the code posted below works but I can't get it to switch to the 2nd "Bank".
Also the first usbMIDI.sendNoteOn line is sending note 22, not note 1.

/* Buttons to USB MIDI Example
You must select MIDI from the "Tools > USB Type" menu
To view the raw MIDI data on Linux: aseqdump -p "Teensy MIDI"
This example code is in the public domain.

You'll have to change the pin assignments below if you use different pins than I did.
*/

#include <Bounce.h>
const int channel = 1;

// Create Bounce objects for each button. The Bounce object
// automatically deals with contact chatter or "bounce", and
// it makes detecting changes very simple.

Bounce button2 = Bounce(2, 10); // which is appropriate for good
Bounce button3 = Bounce(3, 10); // quality mechanical pushbuttons

const byte Notes[2][7] = {{1,2,3,4,5,6,7}, {11,22,33,44,55,66,77}};

void setup() {
// Configure the pins for input mode with pullup resistors.
// The pushbuttons connect from each pin to ground. When
// the button is pressed, the pin reads LOW because the button
// shorts it to ground. When released, the pin reads HIGH
// because the pullup resistor connects to +5 volts inside
// the chip. LOW for "on", and HIGH for "off" may seem
// backwards, but using the on-chip pullup resistors is very
// convenient. The scheme is called "active low", and it's
// very commonly used in electronics... so much that the chip
// has built-in pullup resistors!

pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);

}
void loop() {
// Update all the buttons. There should not be any long
// delays in loop(), so this runs repetitively at a rate
// faster than the buttons could be pressed and released.

button2.update();
button3.update();

// Check each button for "falling" edge.
// Send a MIDI Note On message when each button presses
// Update the Joystick buttons only upon changes.
// falling = high (not pressed - voltage from pullup resistor)
// to low (pressed - button connects pin to ground)

for (int button = 0; button < 7; button++) {
button2.update();

if (button3.fallingEdge()) {
usbMIDI.sendNoteOn(Notes[1][1], 99, channel); // Is playing Midi note 22 - A#-1
}

if (button3.risingEdge()) {
usbMIDI.sendNoteOff(Notes[1][1], 99, channel);

if (button3.fallingEdge()) {
usbMIDI.sendNoteOn(Notes[11][2], 99, channel);
}

if (button3.risingEdge()) {
usbMIDI.sendNoteOff(Notes[11][2], 99, channel);
}
}
}
}

The OP does not understand code tags and the OP did not use the autoformat (CTRL-T) feature of the IDE.

coryell291:
Also the first usbMIDI.sendNoteOn line is sending note 22, not note 1.

const byte Notes[2][7] = {{1,2,3,4,5,6,7}, {11,22,33,44,55,66,77}};

usbMIDI.sendNoteOn(Notes[1][1], 99, channel);  // Is playing Midi note 22 - A#-1

It makes perfect sense when you know that array indexes start at 0. The index [1][1] is the second element of the second bank: 22.

Hey JohnW, yes that does make sense.. thank you for reminding me of that. I knew that and had forgotten that arrays start at 0. I understand now how to get the note that I want on any given switch but I am having a hard time understanding A. what the

for (int button = 0; button < 7; button++) {
Buttons[button].update(); }

part of the code does and B. how to tie it into the code. here is what I have so far.

/* Buttons to USB MIDI Example
   You must select MIDI from the "Tools > USB Type" menu
   To view the raw MIDI data on Linux: aseqdump -p "Teensy MIDI"
   This example code is in the public domain.
   
   You'll have to change the pin assignments below if you use different pins than I did. 
*/


#include <Bounce.h>
const int channel = 1;

// Create Bounce objects for each button.  The Bounce object
// automatically deals with contact chatter or "bounce", and
// it makes detecting changes very simple.

Bounce button2 = Bounce(2, 10);  // which is appropriate for good
Bounce button3 = Bounce(3, 10);  // quality mechanical pushbuttons
Bounce button4 = Bounce(4, 10);

const byte Notes[][7] = {{1,2,3,4,5,6,7}, {11,22,33,44,55,66,77}};

void setup() {
// Configure the pins for input mode with pullup resistors.
  // The pushbuttons connect from each pin to ground.  When
  // the button is pressed, the pin reads LOW because the button
  // shorts it to ground.  When released, the pin reads HIGH
  // because the pullup resistor connects to +5 volts inside
  // the chip.  LOW for "on", and HIGH for "off" may seem
  // backwards, but using the on-chip pullup resistors is very
  // convenient.  The scheme is called "active low", and it's
  // very commonly used in electronics... so much that the chip
  // has built-in pullup resistors!
  
pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);  
}
void loop() {
// Update all the buttons.  There should not be any long
  // delays in loop(), so this runs repetitively at a rate
  // faster than the buttons could be pressed and released.
  
button2.update();
  button3.update();
  button4.update();
  
// Check each button for "falling" edge.
  // Send a MIDI Note On message when each button presses
  // Update the Joystick buttons only upon changes.
  // falling = high (not pressed - voltage from pullup resistor)
  //           to low (pressed - button connects pin to ground)





    for (int button = 0; button < 7; button++) {
         Buttons[button].update();  }

        if (button3.fallingEdge()) {
            usbMIDI.sendNoteOn(Notes[1][1], 99, channel);  // Is playing Midi note 46 - A#1
        }

        if (button3.risingEdge()) {
            usbMIDI.sendNoteOff(Notes[1][1], 99, channel);
        }
               if (button4.fallingEdge()) {
            usbMIDI.sendNoteOn(Notes[0][0], 99, channel);  
        }

        if (button4.risingEdge()) {
            usbMIDI.sendNoteOff(Notes[0][0], 99, channel);
        }
    }
/code]

To use an array of Bounce objects you have to create an array of Bounce objects:

Bounce Buttons[] = {Bounce(2, 10), Bounce(3, 10), Bounce(4, 10)};

To use seven buttons you will need to define all seven objects.

Hey John, thank you for your help. I think I understand the for loop now for Bounce. This works when I upload it to my pedal. Correct me if I'm wrong but the for loop for Bounce is to clean up and shorten the over all code correct?

/* Buttons to USB MIDI Example
   You must select MIDI from the "Tools > USB Type" menu
   To view the raw MIDI data on Linux: aseqdump -p "Teensy MIDI"
   This example code is in the public domain.
   
   You'll have to change the pin assignments below if you use different pins than I did. 
*/


#include <Bounce.h>
const int channel = 1;

// Create Bounce objects for each button.  The Bounce object
// automatically deals with contact chatter or "bounce", and
// it makes detecting changes very simple.

Bounce button5 = Bounce(5, 10);  // which is appropriate for good
Bounce button3 = Bounce(3, 10);  // quality mechanical pushbuttons
Bounce button4 = Bounce(4, 10);

const byte Notes[][7] = {{1,2,3,4,5,6,7}, {11,22,33,44,55,66,77}};
Bounce Buttons[] = {Bounce(5, 10), Bounce(3, 10), Bounce(4, 10)};
void setup() {
// Configure the pins for input mode with pullup resistors.
  // The pushbuttons connect from each pin to ground.  When
  // the button is pressed, the pin reads LOW because the button
  // shorts it to ground.  When released, the pin reads HIGH
  // because the pullup resistor connects to +5 volts inside
  // the chip.  LOW for "on", and HIGH for "off" may seem
  // backwards, but using the on-chip pullup resistors is very
  // convenient.  The scheme is called "active low", and it's
  // very commonly used in electronics... so much that the chip
  // has built-in pullup resistors!
  
pinMode(5, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);  
}
void loop() {
// Update all the buttons.  There should not be any long
  // delays in loop(), so this runs repetitively at a rate
  // faster than the buttons could be pressed and released.
  


  
// Check each button for "falling" edge.
  // Send a MIDI Note On message when each button presses
  // Update the Joystick buttons only upon changes.
  // falling = high (not pressed - voltage from pullup resistor)
  //           to low (pressed - button connects pin to ground)

  for (int Bounce = 0; Bounce < 7; Bounce++) {  // <-- this is for Bounce array
         Buttons[Bounce].update();  }

         if (button5.fallingEdge()){
      usbMIDI.sendNoteOn(Notes[1][1], 99, channel); 
         }
         if (button4.fallingEdge()){
          usbMIDI.sendNoteOn(Notes[0][1], 99, channel);
         }
         }
    

/code]