A lot of incoming midi messeges cause synth

i want to use both recieve and send midi msg ( arduino 2560 pro and my synth) .
i tried. when only (midi out from arduino to synth) is connected . working well.button are sending Prog change messeges.

but when both (midi in and out ) are connected. i am getting a flood of midi messeges (NoteOn/NoteOff/Prog Change etc. randomly) into my synthesizer.with pressing any key of synth.
my midi in connection are in this way

my first simple try code is below to see note messege data on oled display


#include <MIDI.h>
#include <Keypad.h>                   
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define NUMFLAKES     10 // Number of snowflakes in the animation example

const byte ROWS = 15; 
const byte COLS = 10; 
char keys[ROWS][COLS] = {
{1,2,3,4,5,6,7,8,9,10},
{11,12,13,14,15,16,17,18,19,20},
{21,22,23,24,25,26,27,28,29,30},
{31,32,33,34,35,36,37,38,39,40},
{41,42,43,44,45,46,47,48,49,50},
{51,52,53,54,55,56,57,58,59,60},
{61,62,63,64,65,66,67,68,69,70},
{71,72,73,74,75,76,77,78,79,80},
{81,82,83,84,85,86,87,88,89,90},
{91,92,93,94,95,96,97,98,99,100},
{101,102,103,104,105,106,107,108,109,110},
{111,112,113,114,115,116,117,118,119,120},
{121,122,123,124,125,126,127,128,129,130},
{131,132,133,134,135,136,137,138,139,140},
{141,142,143,144,145,146,147,148,149,150}
};
byte rowPins[ROWS] = {4,5,6,7,8,9,10,11,12,14,15,16,17,22,23}; 
byte colPins[COLS] = {32,33,34,35,36,37,38,39,40,41}; 

Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
String msg;
MIDI_CREATE_DEFAULT_INSTANCE();
 byte sysexArray[8] = {0xF0,0x7F,0x7F,0x04,0x04,0x00,50,0xF7}; 

 int Shift1 = 0;
in Note_In = 0;

void setup() {

    pinMode(LED_BUILTIN, OUTPUT);
    MIDI.begin(4);

    if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }
 display.display();
 display.clearDisplay();

    msg = "";

  
}

void SCN (int gg) {
  display.clearDisplay();
  display.setTextSize(1);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.setCursor(0,0);             // Start at top-left corner
  display.println(gg);
  display.setCursor(50,0);             // Start at top-left corner
  display.println(msg);
  display.setCursor(0,16);             // Start at top-left corner
  display.println(analogRead(A0));
  display.display();
  
}

void loop() {

    if (kpd.getKeys())
    {
        for (int i=0; i<LIST_MAX; i++)   
        {      int mykey = kpd.key[i].kchar;
            if (( kpd.key[i].stateChanged ) && (mykey < 128 )&& (mykey > 0 )) 
            {
                switch (kpd.key[i].kstate) {  
                    case PRESSED:
                    msg = " PRESSED.";  SCN (mykey);      MIDI.sendProgramChange(mykey, 1);
                break;
                    case RELEASED:
                    msg = " RELEASED.";   SCN (mykey);
                break;
                }
            }
            // ----------------------------------------------------------------------------------------------

        }
    }   

        if (MIDI.read())                // Is there a MIDI message incoming ?
    {
        switch(MIDI.getType())      // Get the type of the message we caught
        {           case midi::NoteOn:       // If it is a Note on,
             int Channel = (MIDI.getChannel());
              int D1 = ( MIDI.getData1()) ;
               int D2 = ( MIDI.getData2());                     

  display.clearDisplay();
  display.setTextSize(1);             
  display.setTextColor(SSD1306_WHITE);        
  display.setCursor(0,0);             
  display.println(Channel);
  display.setCursor(50,0);             
  display.println(D1);
  display.setCursor(0,16);           
  display.println(D2);
  display.display();
   Note_In = D1;
                break;
        }
    }
   
}  // End loop

please take a look . what mistake is being done by me in MIDI incoming code section???

Do you see the flood playing out on the display?

I don't use the keypad library, but I can ask why you don't use it in the simple way the example code does, viz:

void loop(){
  char key = keypad.getKey();
  
  if (key){
    Serial.println(key);
  }
}

Where obvsly I hope you'd want to handle the pressed key differently to just serial printing it.

Looping over all the keys just to find one you pressed makes sense if you are trying to see multiple keys get pressed at the same instant.

a7

@charnjit - I have had an opportunity to run the keypad exactly how you are using it, and there is no sign of repeated pressed events.

As I had to strip away all the display and MIDI stuff, it is possible I damaged something (fixed, that is), but I am quite good at hacking, my confidence is high.

I suggest you write a simpler sketch that uses only the serial monitor. No display, no MIDI and see if you can get one prssed/released pair per actual keystroke.

I see your code runs through only the active key list filled in by the getKetys() method. I'll file this under I learned something today.

Hacked down sketch
type or paste code here# include <Keypad.h>                   

const uint8_t ROWS = 4;
const uint8_t COLS = 4;

char keys[ROWS][COLS] = {
  { '1', '2', '3', 'A' },
  { '4', '5', '6', 'B' },
  { '7', '8', '9', 'C' },
  { '*', '0', '#', 'D' }
};

uint8_t colPins[COLS] = { 5, 4, 3, 2 }; // Pins connected to C1, C2, C3, C4
uint8_t rowPins[ROWS] = { 9, 8, 7, 6 }; // Pins connected to R1, R2, R3, R4

Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup() {
  Serial.begin(115200);
  Serial.println("Hello Keypad Advanced World!\n");
}

void loop() {
  if (kpd.getKeys()) {
    for (int ij = 0; ij < LIST_MAX; ij++) {      
      int mykey = kpd.key[ij].kchar;
      if (( kpd.key[ij].stateChanged ) && (mykey < 128 )&& (mykey > 0 )) 
      {
          switch (kpd.key[ij].kstate) {  
          case PRESSED:
            Serial.print(ij); Serial.print(" pressed "); Serial.println(mykey);
            break;
          
          case RELEASED:
            Serial.print(ij); Serial.print(" released "); Serial.println(mykey);
            break;
          }
      }  
    }
  }   
}

Dunno where else to look, what is your keyboard and how is it wired?

a7

Is this connected via a plug board? The reason I ask is it appears to be a bad connection.

thank you for reply , but it did not solve yet
there is no any issue with keypad code. when i disconnect midi (from synth to Arduino) or i removed code line below , it works fine. every key press from arduino change prog of my synth & also show key number on oled display.

        if (MIDI.read())                // Is there a MIDI message incoming ?
    {
        switch(MIDI.getType())      // Get the type of the message we caught
        {           case midi::NoteOn:       // If it is a Note on,
             int Channel = (MIDI.getChannel());
              int D1 = ( MIDI.getData1()) ;
               int D2 = ( MIDI.getData2());                     

  display.clearDisplay();
  display.setTextSize(1);             
  display.setTextColor(SSD1306_WHITE);        
  display.setCursor(0,0);             
  display.println(Channel);
  display.setCursor(50,0);             
  display.println(D1);
  display.setCursor(0,16);           
  display.println(D2);
  display.display();
   Note_In = D1;
                break;
        }
    }

i seemed any problem in Midi library. when above code is added to sketch, it behave flood of midi messeges , hanged out my synth.No any result is shown on Display.only midi out from Arduino is working . before adding midi & display code , i have check every key press in serial monitor. before adding midi code , i see every key pressed & released on display. so no issue with keypad code or wiring. problem is in -:

  if (MIDI.read()) 

please help me. whererfrom these a lot midi messsege are coming,
is there another midi library availble for 5pin midi to test again.???
or any other solution ???

By default midi thru is turned on using MIDI.h, try adding

MIDI.turnThruOff();

to setup() after MIDI.begin();

1 Like

thank you Deva Rishi ,

   MIDI.turnThruOff();

solved my issue.
thank you all of you .

2 Likes

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