matrix of buttons to USBMIDI help

Status
Not open for further replies.

Nick1802

Well-known member
hey everyone,

i have been stuck on this for around a week now!!
im having a issue trying to send midi over usb

the code i am using is this



Code:
#include <Encoder.h>
#include <Bounce.h>

const byte sensor0 = 14;
const byte sensor1 = 15;
const byte fader0 = A8;
const byte touch0 = 0;
const byte transmitA = 2;
const byte transmitB = 3;
const byte transmitC = 4;
const byte reciverA = 5;
const byte reciverB = 6;
const byte reciverC = 7;
Encoder myEnc0 (8,9);
const int motorDown = 18;
const int motorUp = 19;
int led = 13;
int v = 0;
int w = 0;
int t[8] = {0};
int f[8] = {0};
double faderMax = 1023;
double faderMin = 1;
volatile bool touched = false;

void setup() {
  Serial.begin(115200);
  pinMode(reciverA,OUTPUT);
  pinMode(reciverB,OUTPUT);
  pinMode(reciverC,OUTPUT);
  pinMode(transmitA,OUTPUT);
  pinMode(transmitB,OUTPUT);
  pinMode(transmitC,OUTPUT);
  pinMode(sensor0,INPUT);
  pinMode(sensor1,INPUT);
  pinMode(fader0,INPUT);
  pinMode(touch0,INPUT);
  pinMode(led,OUTPUT);
  pinMode(motorUp,OUTPUT);
  pinMode(motorDown,OUTPUT);
  digitalWrite (led,HIGH);
}
long oldEnc0 = -999;

int readKeyboard() {
  int j,i,code;
  code=0;
  for(i=0;i<=7;i++) {
    for(j=0;j<=7;j++) {
      code++;
      //col
      {
        digitalWrite (transmitA, (i & 1) ? HIGH : LOW);
        digitalWrite (transmitB, (i & 2) ? HIGH : LOW);
        digitalWrite (transmitC, (i & 4) ? HIGH : LOW);
      }
      //row
      {
        digitalWrite (reciverA, (j & 1) ? HIGH : LOW);
        digitalWrite (reciverB, (j & 2) ? HIGH : LOW);
        digitalWrite (reciverC, (j & 4) ? HIGH : LOW);
        delayMicroseconds(20);
        digitalRead(sensor0);
        digitalRead(sensor1);
      }
      

      // Read the actual Value
      v = digitalRead(sensor0);
      w = digitalRead(sensor1);
      /*
Serial.print("Col: ");
       Serial.print(i);
       Serial.print(",Row: ");
       Serial.print(j);
       Serial.print("),IC1: ");
       Serial.print(v);
       Serial.print(" ,IC2: ");
       Serial.println(w);
       */
      if(v==HIGH)
      {return code;}
      if(w==HIGH) 
      {return (code+64);}
    }
  }
}

int readfader0 (const byte i)
{
{
  // select correct MUX channel
  digitalWrite (transmitA, (i & 1) ? HIGH : LOW);
  digitalWrite (transmitB, (i & 2) ? HIGH : LOW);
  digitalWrite (transmitC, (i & 4) ? HIGH : LOW);
  // now read the sensor
  return analogRead (fader0);
  }
  f[i] = analogRead (fader0);
}

int readtouch0 (const byte i)
{
{
  // select correct MUX channel
  digitalWrite (transmitA, (i & 1) ? HIGH : LOW);
  digitalWrite (transmitB, (i & 2) ? HIGH : LOW);
  digitalWrite (transmitC, (i & 4) ? HIGH : LOW);
  // now read the sensor
  return touchRead (touch0);
}
t[i] = touchRead (touch0);
}
void loop(){

  {
    v = readKeyboard();
    if(v >= 1 && v <= 128){
      Serial.println(v);
      if (v.fallingEdge()) {                      **********************************
    usbMIDI.sendNoteOn(v-1, 127, 1);
  }
  if (v.risingEdge()){                            **********************************
    usbMIDI.sendNoteOff(v-1, 0, 1);
  }
}
if (v >= 129 && v <= 254){
   Serial.println(v);
   if (v.fallingEdge()) {                         **********************************
    usbMIDI.sendNoteOn(v-1, 127, 2);
  }
  if (v.risingEdge()){                            **********************************
    usbMIDI.sendNoteOff(v-1, 0, 2);
  }}
}
  {
    long newEnc0;
    newEnc0= myEnc0.read();
    if (newEnc0 != oldEnc0) {
      Serial.print("Enc0 = ");
      Serial.println(newEnc0/4);
      oldEnc0 = newEnc0;
    }
  }
  {
    // show all 8 sensor readings
    for (byte i = 0; i <=7; i++)
    {
      if(readfader0(i)>2){
      Serial.print ("fader ");
      Serial.print (i);
      Serial.print (" reads: ");
      Serial.println (readfader0(i));
      }
      if (readtouch0(i) > 3600){
        Serial.print("touch0 ");
        Serial.print(i);
        Serial.println(" is touched");
}
}
}
}

i keep getting errors with the lines that are ******************************

error that i keep getting are
error: request for member 'fallingEdge' in 'v', which is of non-class type 'int'
error: request for member 'risingEdge' in 'v', which is of non-class type 'int'
error: request for member 'fallingEdge' in 'v', which is of non-class type 'int'
error: request for member 'risingEdge' in 'v', which is of non-class type 'int'


when i changed this bit of code to the one below i can get it to output midi but keeps sending it every loop
Code:
void loop(){

  {
    v = readKeyboard();
    if(v >= 1 && v <= 128){
      Serial.println(v);
      if (v.fallingEdge()) {
    usbMIDI.sendNoteOn(v-1, 127, 1);
  }
  if (v.risingEdge()){
    usbMIDI.sendNoteOff(v-1, 0, 1);
  }
}
if (v >= 129 && v <= 254){
   Serial.println(v);
   if (v.fallingEdge()) {
    usbMIDI.sendNoteOn(v-1, 127, 2);
  }
  if (v.risingEdge()){
    usbMIDI.sendNoteOff(v-1, 0, 2);
  }}
}


to be like this

Code:
void loop(){

  {
    v = readKeyboard();
    if(v >= 1 && v <= 128){
      Serial.println(v);
    usbMIDI.sendNoteOn(v-1, 127, 1);
  }
if (v >= 129 && v <= 254){
   Serial.println(v);
    usbMIDI.sendNoteOn(v-1, 127, 2);
}
}


it works but with out the note off when i let go. but i cant work out how to add it with out errors

i had also tryed useing the bounce lib but couldnt work it out ether but im not sure if i even need it when useing mux

if any one can point me where to look or even show me how to fix this would be much liked!!!

sorry i no this is posted eles where on this forum but no 1 has replyed to it so im asking again fresh

thanks
nick
 
You have declared a variable called v, which is an int
Code:
int v = 0;

and later, you are trying to call v's fallingEdge method. Its an integer, it doesn't have methods.
Code:
if (v.fallingEdge()) {                      **********************************
    usbMIDI.sendNoteOn(v-1, 127, 1);

You need to declare a bounce object for each switch that you want to be debounced, like this

Code:
// Instantiate a Bounce object with a 5 millisecond debounce time
Bounce bouncer1 = Bounce(BUTTON1, 5);

Have a look at Bounce/examples/edge/ for some code that does this.

Its not clear to me what your code is trying to do with v. Part of the code is doing matrix scanning and part of it is calling bounce (but without any Bounce objects).
 
Last edited:
thanks nantonos,
when a button is pushed v prints out the number of the button that is being pushed. so with this i was hoping that where it is would say the button number and the program would work. but it dont :( i did even try
((v==1).fallingEdge())
still didnt work :(


looking at all the examples in bounce. i cant find anything that shows me how to use this with a matrix of buttons.

i think the part that is stumping me the most is with the bounce lib is the part at the start

Code:
Bounce bouncer = Bounce (pin-name,5)

but couse im using a array i need pin-name to put to there
how do i do that??
 
As I said, v is just a number. Its not a bounce object, so it doesn't have methods (code that you can call).

And yes, bounce is for individual buttons not a scanned matrix. You seem to be trying to use both at once on the same buttons.

Wait, with all this fallingEdge risingEdge stuff you are just trying to find the current state of a button (up or down)?

It would also help if you worked on smaller examples. Don't try to do everything at once. yes, you want faders and etc eventually but for now, just work on being able to scan a matrix of keys and reliably output to Serial.print what key was actually pressed.

Once you have that working, change it so it generates the appropriate midi message.

Once that is working, make a new sketch and learn how to read an encoder. And so on. Currently you have bits of all of this jumbled up which makes it hard for others to help you on a specific problem.
 
Wait, with all this fallingEdge risingEdge stuff you are just trying to find the current state of a button (up or down)?

yes, then have it send midi note on/off

It would also help if you worked on smaller examples. Don't try to do everything at once. yes, you want faders and etc eventually but for now, just work on being able to scan a matrix of keys and reliably output to Serial.print what key was actually pressed.

Once you have that working, change it so it generates the appropriate midi message.

ok so this code i have here is just the button matrix with only serial print and its working fine ever sense i got it to work as digital buttons instead of analog buttons.

Code:
const byte sensor0 = 14;       //4051(1) input
const byte sensor1 = 15;      //4051(2) input
const byte transmitA = 2;      //control pins for col 4051
const byte transmitB = 3;
const byte transmitC = 4;
const byte reciverA = 5;      //control pins for rows 4051
const byte reciverB = 6;
const byte reciverC = 7;
int v = 0;                          //array to store 4051(1) reads
int w = 0;                         //array to store 4051(2) reads
int led = 13;                      //to use as a power light

void setup() {
  Serial.begin(115200);
  pinMode(reciverA,OUTPUT);     
  pinMode(reciverB,OUTPUT);
  pinMode(reciverC,OUTPUT);
  pinMode(transmitA,OUTPUT);
  pinMode(transmitB,OUTPUT);
  pinMode(transmitC,OUTPUT);
  pinMode(sensor0,INPUT);
  pinMode(sensor1,INPUT);
  pinMode(led, OUTPUT);
  digitalWrite (led, HIGH);
}

int readKeyboard() {
  int j,i,code;
  code=0;
  for(i=0;i<=7;i++) {
    for(j=0;j<=7;j++) {
      code++;
      //col
      {
        digitalWrite (transmitA, (i & 1) ? HIGH : LOW);
        digitalWrite (transmitB, (i & 2) ? HIGH : LOW);
        digitalWrite (transmitC, (i & 4) ? HIGH : LOW);
      }
      //row
      {
        digitalWrite (reciverA, (j & 1) ? HIGH : LOW);
        digitalWrite (reciverB, (j & 2) ? HIGH : LOW);
        digitalWrite (reciverC, (j & 4) ? HIGH : LOW);
        delayMicroseconds(20);
        digitalRead(sensor0);
        digitalRead(sensor1);
      }
      //delay(0);

      // Read the actual Value
      v = digitalRead(sensor0);
      w = digitalRead(sensor1);

      if(v==HIGH)
      {
        return code;                                 
      }
      if(w==HIGH) 
      {
        return (code+64);
      }
    }
  }
}

void loop(){
  {
    v = readKeyboard();
    if(v>=1)                                              //so it doesn't show the 0's
      Serial.println(v);
    //delay(100);
  }
}
 
Last edited:
so now i have gone all the way back to very basic!!!!

this code is only 6/128 done as i only did it up to where i had buttons contected

and it works

Code:
#include <Bounce.h>

const byte sensor0 = 14;
const byte sensor1 = 15;
const byte transmitA = 2;
const byte transmitB = 3;
const byte transmitC = 4;
const byte reciverA = 5;
const byte reciverB = 6;
const byte reciverC = 7;
int led = 13;

Bounce bp14r1c1 = Bounce(sensor0,5);
Bounce bp14r4c1 = Bounce(sensor0,5);
Bounce bp14r8c1 = Bounce(sensor0,5);
Bounce bp15r1c8 = Bounce(sensor1,5);
Bounce bp15r4c8 = Bounce(sensor1,5);
Bounce bp15r8c8 = Bounce(sensor1,5);

void setup() {
  Serial.begin(115200);
  pinMode(reciverA,OUTPUT);
  pinMode(reciverB,OUTPUT);
  pinMode(reciverC,OUTPUT);
  pinMode(transmitA,OUTPUT);
  pinMode(transmitB,OUTPUT);
  pinMode(transmitC,OUTPUT);
  pinMode(sensor0,INPUT);
  pinMode(sensor1,INPUT);
  pinMode(led, OUTPUT);
  digitalWrite (led, HIGH);
}


void loop(){
  //row 1 col 1  
  digitalWrite(transmitA, LOW);
  digitalWrite(transmitB, LOW);
  digitalWrite(transmitC, LOW);
  digitalWrite(reciverA, LOW);
  digitalWrite(reciverB, LOW);
  digitalWrite(reciverC, LOW);
  
  delayMicroseconds(20);
  
  bp14r1c1.update();
   
  //row 4 col 1
  digitalWrite(transmitA, HIGH);
  digitalWrite(transmitB, HIGH);
  digitalWrite(transmitC, LOW);
  digitalWrite(reciverA, LOW);
  digitalWrite(reciverB, LOW);
  digitalWrite(reciverC, LOW);
  
  delayMicroseconds(20);
  
  bp14r4c1.update();
  
  //row 8 col 1
  digitalWrite(transmitA, HIGH);
  digitalWrite(transmitB, HIGH);
  digitalWrite(transmitC, HIGH);
  digitalWrite(reciverA, LOW);
  digitalWrite(reciverB, LOW);
  digitalWrite(reciverC, LOW);
  
  delayMicroseconds(20);
  
  bp14r8c1.update();

  //row 1 col 8  
  digitalWrite(transmitA, LOW);
  digitalWrite(transmitB, LOW);
  digitalWrite(transmitC, LOW);
  digitalWrite(reciverA, HIGH);
  digitalWrite(reciverB, HIGH);
  digitalWrite(reciverC, HIGH);
  
  delayMicroseconds(20);
  
  bp15r1c8.update();
   
  //row 4 col 8
  digitalWrite(transmitA, HIGH);
  digitalWrite(transmitB, HIGH);
  digitalWrite(transmitC, LOW);
  digitalWrite(reciverA, HIGH);
  digitalWrite(reciverB, HIGH);
  digitalWrite(reciverC, HIGH);
  
  delayMicroseconds(20);
  
  bp15r4c8.update();
  
  //row 8 col 8
  digitalWrite(transmitA, HIGH);
  digitalWrite(transmitB, HIGH);
  digitalWrite(transmitC, HIGH);
  digitalWrite(reciverA, HIGH);
  digitalWrite(reciverB, HIGH);
  digitalWrite(reciverC, HIGH);
  
  delayMicroseconds(20);
  
  bp15r8c8.update();

 
  
  
  
  
  
  
  
  
  
  if (bp14r1c1.risingEdge()) {
    usbMIDI.sendNoteOn(0, 127, 1);  // 60 = C4
  }
  if (bp14r4c1.risingEdge()) {
    usbMIDI.sendNoteOn(1, 127, 1);  // 61 = C#4
  }
  if (bp14r8c1.risingEdge()) {
    usbMIDI.sendNoteOn(2, 127, 1);  // 62 = D4
  }
  if (bp15r1c8.risingEdge()) {
    usbMIDI.sendNoteOn(125, 127, 1);  // 60 = C4
  }
  if (bp15r4c8.risingEdge()) {
    usbMIDI.sendNoteOn(126, 127, 1);  // 61 = C#4
  }
  if (bp15r8c8.risingEdge()) {
    usbMIDI.sendNoteOn(127, 127, 1);  // 62 = D4
  }
  
  
  
  
  
  
  
  
  // Note Off messages when each button is released
  if (bp14r1c1.fallingEdge()) {
    usbMIDI.sendNoteOff(0, 0, 1);  // 60 = C4
  }
  if (bp14r4c1.fallingEdge()) {
    usbMIDI.sendNoteOff(1, 0, 1);  // 61 = C#4
  }
  if (bp14r8c1.fallingEdge()) {
    usbMIDI.sendNoteOff(2, 0, 1);  // 62 = D4
  }
  if (bp15r1c8.fallingEdge()) {
    usbMIDI.sendNoteOff(125, 0, 1);  // 60 = C4
  }
  if (bp15r4c8.fallingEdge()) {
    usbMIDI.sendNoteOff(126, 0, 1);  // 61 = C#4
  }
  if (bp15r8c8.fallingEdge()) {
    usbMIDI.sendNoteOff(127, 0, 1);  // 62 = D4
  }








  // MIDI Controllers should discard incoming MIDI messages.
  while (usbMIDI.read()) {
  }
}
 
now i have condensed it a bit. i thought i was doing it right but in my MIDI window its showing the note on and note off when they should but 64 times.
i changed for
Code:
(i=0;i<=63;i++)
to
Code:
(i=0;i<=6;i++)
and then my MIDI window showed it 7 times (but of course it's not reading though all the buttons now)
if any 1 can see y its doing that that would be cool :)



Code:
#include <Bounce.h>

const byte sensor0 = 14;
const byte sensor1 = 15;
const byte transmitA = 2;
const byte transmitB = 3;
const byte transmitC = 4;
const byte reciverA = 5;
const byte reciverB = 6;
const byte reciverC = 7;
int i = 0;
int led = 13;
Bounce bp14r1c1 = Bounce(sensor0,20);
Bounce bp14r4c1 = Bounce(sensor0,20);
Bounce bp14r8c1 = Bounce(sensor0,20);
Bounce bp15r1c8 = Bounce(sensor1,20);
Bounce bp15r4c8 = Bounce(sensor1,20);
Bounce bp15r8c8 = Bounce(sensor1,20);

void setup() {
Serial.begin(115200);
pinMode(reciverA,OUTPUT);
pinMode(reciverB,OUTPUT);
pinMode(reciverC,OUTPUT);
pinMode(transmitA,OUTPUT);
pinMode(transmitB,OUTPUT);
pinMode(transmitC,OUTPUT);
pinMode(sensor0,INPUT);
pinMode(sensor1,INPUT);
pinMode(led, OUTPUT);
digitalWrite (led, HIGH);
}



void loop(){
for(i=0;i<=6;i++) {
//col
  {
  digitalWrite (transmitA, (i & 1) ? HIGH : LOW);
  digitalWrite (transmitB, (i & 2) ? HIGH : LOW);
  digitalWrite (transmitC, (i & 4) ? HIGH : LOW);
  digitalWrite (reciverA, (i & 8) ? HIGH : LOW);
  digitalWrite (reciverB, (i & 16) ? HIGH : LOW);
  digitalWrite (reciverC, (i & 32) ? HIGH : LOW);
  delayMicroseconds(20);
  if (i==0){
    bp14r1c1.update();
  }
  if (i==3){
    bp14r4c1.update();
  }
  if (i==7){
    bp14r8c1.update();
}
  if (i==56){
    bp15r1c8.update();
  }
  if (i==59){
    bp15r4c8.update();
  }
  if (i==63){
    bp15r8c8.update();
  }


  if (bp14r1c1.risingEdge()) {
    usbMIDI.sendNoteOn(0, 127, 1); 
  }
  if (bp14r4c1.risingEdge()) {
    usbMIDI.sendNoteOn(1, 127, 1); 
  }
  if (bp14r8c1.risingEdge()) {
    usbMIDI.sendNoteOn(2, 127, 1); 
  }
  if (bp15r1c8.risingEdge()) {
    usbMIDI.sendNoteOn(125, 127, 1); 
  }
  if (bp15r4c8.risingEdge()) {
    usbMIDI.sendNoteOn(126, 127, 1); 
  }
  if (bp15r8c8.risingEdge()) {
    usbMIDI.sendNoteOn(127, 127, 1);
  }
  
  
  
  
  
  
  
  
  // Note Off messages when each button is released
  if (bp14r1c1.fallingEdge()) {
    usbMIDI.sendNoteOff(0, 0, 1);  
  }
  if (bp14r4c1.fallingEdge()) {
    usbMIDI.sendNoteOff(1, 0, 1);  
  }
  if (bp14r8c1.fallingEdge()) {
    usbMIDI.sendNoteOff(2, 0, 1);  
  }
  if (bp15r1c8.fallingEdge()) {
    usbMIDI.sendNoteOff(125, 0, 1);  
  }
  if (bp15r4c8.fallingEdge()) {
    usbMIDI.sendNoteOff(126, 0, 1); 
  }
  if (bp15r8c8.fallingEdge()) {
    usbMIDI.sendNoteOff(127, 0, 1); 
  }








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

thanks :)
 
never mind found the problem!! i had a } not in the right spot!! had it at the end when it should have been b4 the midi messages
 
Status
Not open for further replies.
Back
Top