Need help with Midi banks

Status
Not open for further replies.

Balam

Active member
HI
( searched the forum and google it ) need help with midi banks

I have the basic code working as I like and like to add 3 buttons to change midi notes ( 3 midi banks of 8 notes each bank )

any help is very welcome

Code:
/*digital read base from
 * https://raw.githubusercontent.com/langsound/Digital-Electronics/master/Teensy_MIDI_code_lesson/ManyButtonsManyNotes_Functions_D_NC/ManyButtonsManyNotes_Functions_D_NC.ino
 */

 
int myLED = 13;;
int myButtons[8];//16
int myOldButtons[8];//16
                 
int myNotes[8] = {60, 62, 64, 65, 67, 69, 71, 72} ;//16
int myVelocity = 127;
int myChannel= 2;


/*****analog area****/
/* base from http://djtechtools.com/2015/08/25/how-to-make-your-own-diy-midi-controller/
 *  */
 

// define how many pots are active up to number of available analog inputs
#define analogInputs 2
// make arrays for input values and lagged input values
int inputAnalog[analogInputs];
int iAlag[analogInputs];
// make array of cc values
int ccValue[analogInputs];
// index variable for loop
int a;

/////



/*******void analog******/

void readanalog(){
  
// loop trough active inputs for knobs
for (a=0;a<analogInputs;a++)
{ 
  // read current value at i-th input inputAnalog[i] = analogRead(i); 
 // if magnitude of difference is 8 or more… if (abs(inputAnalog[i] – iAlag[i]) > 7){
// calc the CC value based on the raw value
ccValue[a] = inputAnalog[a]/8;
// send the MIDI
usbMIDI.sendControlChange(a, ccValue[a], 3);
// set raw reading to lagged array for next comparison
iAlag[a] = inputAnalog[a];
}
delay(10); // limits MIDI messages to reasonable number
}


  /*******end void analog******/////

  

void readMyButtons(){
 for(int i; i<8; i++){
   myButtons[i] = digitalRead(i);
 }  
}

void playMyNotes(){
 for(int i; i<8; i++){
   if(myButtons[i] != myOldButtons[i]){
        myOldButtons[i] = myButtons[i];
        if(myButtons[i]){
          usbMIDI.sendNoteOn(myNotes[i], myVelocity, myChannel);
          digitalWrite(myLED, HIGH);
          delay(5); //// try 1 
          
        }
        else{
          usbMIDI.sendNoteOn(myNotes[i], 0, myChannel);
          digitalWrite(myLED, LOW);
        }
    }
  }
}

void setup(){
  pinMode(myLED, OUTPUT);
  for(int i; i<8; i++){
  pinMode(i, INPUT);
  }
}


/*
 * void Looopppppppppppppplooop
 */

 
void loop(){
 readMyButtons();
 playMyNotes();
 readanalog();


  // MIDI Controllers should discard incoming MIDI messages.
 
  while (usbMIDI.read()) {
    // ignore incoming messages
  }



 
}
 
Last edited:
I don't see that you are using internal pullups... are you using external ones?
Also not too sure about your contact bounce management

You should consider the Teensy-way outlined on Paul's example pages:
http://www.pjrc.com/teensy/td_digital.html
http://www.pjrc.com/teensy/td_libs_Bounce.html

But assuming your code is good and works well for you adding a 'bank select' feature with three buttons and three banks should be fairly simple.


You just need a variable to track the bank and the buttons need to write 0,1,2 to that variable and then you can read from a two-dimensional note value array.
Code:
int myNotes[3][8] = { {60, 62, 64, 65, 67, 69, 71, 72},
            {61, 63, 65, 66, 68, 70, 72, 73},
            {62, 64, 66, 67, 69, 71, 73, 74}};

...

             usbMIDI.sendNoteOn(myNotes[bank][i], myVelocity, myChannel);

Warning... I've not used 2D arrays in Teensyduino or Arduino coding so I'm not 100% sure I've got the initialization correct (the one Arduino reference I looked at omits the number of rows in the declaration).
 
Thanks oddson
I don't see that you are using internal pullups... are you using external ones?
Also not too sure about your contact bounce management

I am using touch sensors :)

I consider myself a Newbie regarding programming ( but willing to take the task to learn more ) I want to use a 3 pole Rotary Switch to select the Banks ( for easy interface ) and each pole is a digital pin ( total of 3 pins )
how can I get the variable, any hint is really welcome.
 
...don't have the IDE handy to test but if you have a three item array similar to your myButtons[] array (I leave it to you to sort out pins etc.) then it should be VERY simple:

Code:
for(int i; i<3; i++){
  if myRotary[i] {
    bank = i;
  }
}
Whichever of the PINs is active; bank should contain its index and therefore should work in reading the 2D array.

Make sure you test initializing the note array and pulling items from it before getting bogged down in details.
 
thanks Addson
I have listen your advice about possible 2d arrays
after a login search session I came across to a sparkfun project that has a similar idea of changing notes with pushbuttons

https://www.sparkfun.com/news/2222

so here is my version ( have not try the code with hardware yet. ) but if you have improvements feel free to edit
2 files ***.ino and scales.h



//This file contains the various Scales used
//Each number corresponds to a position in the note_frequency table
//Many of these scales were imported using this site as a reference:
//https://hapidrum.co/hapi-drum-scale.aspx

///CHANGE WHEN ADDING NEW SCALES
int numOfScales = 6;//7-1=6

int scale[][8] =
{
//G Major (G3, A3, B3, D4, E4, G4, A4, B4) scale [0][x] x=0-7
{
55,//196.00,//G3
57,//220,//A3
59,//246.94,//B3
62,//293.66,//D4
64,//329.63,//E4
67,//392,//G4
69,//440,//A4
71//493.88,//B4
},
/////////////////////////////////////////////////////
//G Minor (G3, Bb3, C4, D4, F4, G4, Bb4, C5)
{
55,//196.00,//G3
58,//(233.88);//Bb3
60,//(261.63);//C4 (Middle C)
62,//293.66,//D4
65,//(174.61);//F4
67,//392,//G4
70,//(233.88);//Bb4
72//(523.25);//C5
},
/////////////////////////////////////////////////////
//C Major (C4, D4, E4, G4, A4, C5, D5, E5)
{
60,//(261.63);//C4 (Middle C)
62,//293.66,//D4
64,//329.63,//E4
67,//392,//G4
69,//440,//A4
72,//(523.25);//C5
74,//(587.32);//D5
76//(659.26);//E5
},
/////////////////////////////////////////////////////
//D Akebono (D4, E4, F4, A4, Bb4, D5, E5, F5)
{
62,//(146.83);//D4
64,//(164.81);//E4
65,//(174.61);//F4
69,//220,//A4
70,//(233.88);//Bb4
74,//293.66,//D5
76,//329.63,//E5
77//(349.23);//F5
/*
50,//(146.83);//D3
52,//(164.81);//E3
53,//(174.61);//F3
57,//220,//A3
58,//(233.88);//Bb3
62,//293.66,//D4
64,//329.63,//E4
65//(349.23);//F4
*/
},
/////////////////////////////////////////////////////
//D Major (D4, E4, F#4, A4, B4, D5, E5, F#5)
{
62,//(146.83);//D4
64,//(164.81);//E4
66,//F#4
69,//220,//A4
71,//B4
74,//293.66,//D5
76,//329.63,//E5
78//F#5
},
/////////////////////////////////////////////////////
//A Minor (A3, C4, D4, E4, G4, A4, C5, D5)
{
57,//A3
60,//C4
62,//(146.83);//D4
64,//(164.81);//E4
67,//392,//G4
69,//220,//A4
72,//C5
74//293.66,//D5
},
/////////////////////////////////////////////////////
//A Akebono (A3, B3, C4, E4, F4, A4, B4, C5)
{
57,//A3
59,//246.94,//B3
60,//C4
64,//(164.81);//E4
65,//(174.61);//F4
69,//A4
71,//B4
72//C5
}
/////////////////////////////////////////////////////
};



arduino / teensy code here


#include <Bounce.h>

#include "scales.h"


// the MIDI channel number to send messages
const int channel = 1;

int scale_index = 0;//var to keep track fo which scale is being used

// Create Bounce objects for each button. The Bounce object
// automatically deals with contact chatter or "bounce", and
// it makes detecting changes very simple.
Bounce button0 = Bounce(0, 5);
Bounce button1 = Bounce(1, 5); // 5 = 5 ms debounce time
Bounce button2 = Bounce(2, 5); // which is appropriate for good
Bounce button3 = Bounce(3, 5); // quality mechanical pushbuttons
Bounce button4 = Bounce(4, 5);
Bounce button5 = Bounce(5, 5); // if a button is too "sensitive"
Bounce button6 = Bounce(6, 5); // to rapid touch, you can
Bounce button7 = Bounce(7, 5); // increase this time.
Bounce button8 = Bounce(8, 5);
Bounce button9 = Bounce(9, 5);
Bounce button10 = Bounce(10, 5);
Bounce button11 = Bounce(11, 5);

/////buttons for change scale

Bounce chbt14 = Bounce(14, 5);
Bounce chbt15 = Bounce(15, 5);


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

pinMode(0, INPUT_PULLUP);
pinMode(1, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP); // Teensy++ 2.0 LED, may need 1k resistor pullup
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
pinMode(11, INPUT_PULLUP); // Teensy 2.0 LED, may need 1k resistor pullup

//////// change buttons

pinMode(14, INPUT_PULLUP);
pinMode(15, 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.
button0.update();
button1.update();
button2.update();
button3.update();
button4.update();
button5.update();
button6.update();
button7.update();
button8.update();
button9.update();
button10.update();
button11.update();

////change scale btn
chbt14.update();
chbt15.update();

// Check each button for touch = risingEdge"" edge.
// Send a MIDI Note On message when each button presses


if (button0.risingEdge()) {
usbMIDI.sendNoteOn(scale[scale_index][0], 99, channel); // 60 = C4
}
if (button1.risingEdge()) {
usbMIDI.sendNoteOn(scale[scale_index][1], 99, channel); // 61 = C#4
}
if (button2.risingEdge()) {
usbMIDI.sendNoteOn(scale[scale_index][2], 99, channel); // 62 = D4
}
if (button3.risingEdge()) {
usbMIDI.sendNoteOn(scale[scale_index][3], 99, channel); // 63 = D#4
}
if (button4.risingEdge()) {
usbMIDI.sendNoteOn(scale[scale_index][4], 99, channel); // 64 = E4
}
if (button5.risingEdge()) {
usbMIDI.sendNoteOn(scale[scale_index][5], 99, channel); // 65 = F4
}
if (button6.risingEdge()) {
usbMIDI.sendNoteOn(scale[scale_index][6], 99, channel); // 66 = F#4
}
if (button7.risingEdge()) {
usbMIDI.sendNoteOn(scale[scale_index][7], 99, channel); // 67 = G4
}

delay(10);
/*
if (button8.risingEdge()) {
usbMIDI.sendNoteOn(68, 99, channel); // 68 = G#4
}
if (button9.risingEdge()) {
usbMIDI.sendNoteOn(69, 99, channel); // 69 = A5
}
if (button10.risingEdge()) {
usbMIDI.sendNoteOn(70, 99, channel); // 70 = A#5
}
if (button11.risingEdge()) {
usbMIDI.sendNoteOn(71, 99, channel); // 71 = B5
}
*/



// Check each button for Not touch = "fallingEdge" edge
// Send a MIDI Note Off message when each button releases



if (button0.fallingEdge()) {
usbMIDI.sendNoteOff(scale[scale_index][0], 0, channel); // 60 = C4
}
if (button1.fallingEdge()) {
usbMIDI.sendNoteOff(scale[scale_index][1], 0, channel); // 61 = C#4
}
if (button2.fallingEdge()) {
usbMIDI.sendNoteOff(scale[scale_index][2], 0, channel); // 62 = D4
}
if (button3.fallingEdge()) {
usbMIDI.sendNoteOff(scale[scale_index][3], 0, channel); // 63 = D#4
}
if (button4.fallingEdge()) {
usbMIDI.sendNoteOff(scale[scale_index][4], 0, channel); // 64 = E4
}
if (button5.fallingEdge()) {
usbMIDI.sendNoteOff(scale[scale_index][5], 0, channel); // 65 = F4
}
if (button6.fallingEdge()) {
usbMIDI.sendNoteOff(scale[scale_index][6], 0, channel); // 66 = F#4
}
if (button7.fallingEdge()) {
usbMIDI.sendNoteOff(scale[scale_index][7], 0, channel); // 67 = G4
}

delay(10);
/*
if (button8.fallingEdge()) {
usbMIDI.sendNoteOff(68, 0, channel); // 68 = G#4
}
if (button9.fallingEdge()) {
usbMIDI.sendNoteOff(69, 0, channel); // 69 = A5
}
if (button10.fallingEdge()) {
usbMIDI.sendNoteOff(70, 0, channel); // 70 = A#5
}
if (button11.fallingEdge()) {
usbMIDI.sendNoteOff(71, 0, channel); // 71 = B5
}
*/

///////////

buttonCheck();//check for button presses to change the scale

///////////

// MIDI Controllers should discard incoming MIDI messages.
// http://forum.pjrc.com/threads/24179-Teensy-3-Ableton-Analog-CC-causes-midi-crash
while (usbMIDI.read()) {
// ignore incoming messages
}
}





//////////////////////////////////////////////////
void buttonCheck()
{
chbt14.update();
chbt15.update();

//if button 0 is pressed, increment the scale being used
if (chbt14.risingEdge())
{
scale_index++;
//check for overflow
if(scale_index > numOfScales)//numOfScales variable found in the scales.h file
scale_index = 0;
}

//if button 1 is pressed, decrement the scale being used
if (chbt15.risingEdge())
{
scale_index--;
//check for negative numbers
if(scale_index < 0)
scale_index = numOfScales;//numOfScales variable found in the scales.h file
}


}
 
Status
Not open for further replies.
Back
Top