Kept receiving notes whenever I plug my MIDI device based on Leonardo to my PC

HI! I'm quite new to Arduino so please bear with me. PLease help me.

I only have Arduino Leonardo and I am planning to make a USB MIDI device, I succeeded using it in Android but I keep receiving ghost notes when I connect it to my Windows 10 pc.

ThIs is the code:

#include "MIDIUSB.h"

#define NUM_ROWS 7
#define NUM_COLS 8

#define NOTE_ON_CMD 0x90
#define NOTE_OFF_CMD 0x80
#define NOTE_VELOCITY 127


// Pin Definitions

void mnoteOn(byte channel, byte pitch, byte velocity) {
 midiEventPacket_t noteOn = {0x09, 0x90 | 0, pitch, velocity};
 MidiUSB.sendMIDI(noteOn);
}

void mnoteOff(byte channel, byte pitch, byte velocity) {
 midiEventPacket_t noteOff = {0x08, 0x80 | 0, pitch, velocity};
 MidiUSB.sendMIDI(noteOff);
}

void controlChange(byte channel, byte control, byte value) {
 midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value};
 MidiUSB.sendMIDI(event);
}  

void noteOn(int channel, int pitch, int velocity) {
 mnoteOn(channel, pitch, velocity);
 MidiUSB.flush();
}

void noteOff(byte channel, byte pitch, byte velocity) {
 mnoteOff(channel, pitch, velocity);
 MidiUSB.flush();
}

// Row input pins
const int row1Pin = 6;
const int row2Pin = 7;
const int row3Pin = 8;
const int row4Pin = 9;
const int row5Pin = 10;
const int row6Pin = 11;
const int row7Pin = 12;

// 74HC595 pins
const int dataPin = 4;
const int latchPin = 3;
const int clockPin = A5;

boolean keyPressed[NUM_ROWS][NUM_COLS];
uint8_t keyToMidiMap[NUM_ROWS][NUM_COLS];

// bitmasks for scanning columns
int bits[] =
{ 
 B01111111,
 B10111111,
 B11011111,
 B11101111,
 B11110111,
 B11111011,
 B11111101,
 B11111110,
};

void setup()
{
 int note = 36;

 for(int colCtr = 0; colCtr < NUM_COLS; ++colCtr)
 {
   for(int rowCtr = 0; rowCtr < NUM_ROWS; ++rowCtr)
   {
     keyPressed[rowCtr][colCtr] = false;
     keyToMidiMap[rowCtr][colCtr] = note;
     note++;
   }
 }

 // setup pins output/input mode
 pinMode(dataPin, OUTPUT);
 pinMode(clockPin, OUTPUT);
 pinMode(latchPin, OUTPUT);

 pinMode(row1Pin, INPUT_PULLUP);
 pinMode(row2Pin, INPUT_PULLUP);
 pinMode(row3Pin, INPUT_PULLUP);
 pinMode(row4Pin, INPUT_PULLUP);
 pinMode(row5Pin, INPUT_PULLUP);
 pinMode(row6Pin, INPUT_PULLUP);
 pinMode(row7Pin, INPUT_PULLUP);

 Serial.begin(115200);

}

void loop()
{
 for (int colCtr = 0; colCtr < NUM_COLS; ++colCtr)
 {
   //scan next column
   scanColumn(colCtr);

   //get row values at this column
   int rowValue[NUM_ROWS];
   rowValue[0] = !digitalRead(row1Pin);
   rowValue[1] = !digitalRead(row2Pin);
   rowValue[2] = !digitalRead(row3Pin);
   rowValue[3] = !digitalRead(row4Pin);
   rowValue[4] = !digitalRead(row5Pin);
   rowValue[5] = !digitalRead(row6Pin);
   rowValue[6] = !digitalRead(row7Pin);

   // process keys pressed
   for(int rowCtr=0; rowCtr<NUM_ROWS; ++rowCtr)
   {
     if(rowValue[rowCtr] != 0 && !keyPressed[rowCtr][colCtr])
     {
       keyPressed[rowCtr][colCtr] = true;
       noteOn(NOTE_ON_CMD, keyToMidiMap[rowCtr][colCtr], NOTE_VELOCITY);
       
     }
   }

   // process keys released
   for(int rowCtr=0; rowCtr<NUM_ROWS; ++rowCtr)
   {
     if(rowValue[rowCtr] == 0 && keyPressed[rowCtr][colCtr])
     {
       keyPressed[rowCtr][colCtr] = false;
       noteOff(NOTE_OFF_CMD, keyToMidiMap[rowCtr][colCtr], NOTE_VELOCITY);
       
     }
   }
 }
}

void scanColumn(int colNum)
{
 digitalWrite(latchPin, LOW);

 if(0 <= colNum && colNum <= 7)
 {
   shiftOut(dataPin, clockPin, MSBFIRST, bits[colNum]); //left sr
 }
 else
 {
   shiftOut(dataPin, clockPin, MSBFIRST, bits[colNum-8]); //right sr
 }
 digitalWrite(latchPin, HIGH);
}

Perhaps you could help by explaining what you mean by 'ghost notes' and exactly when you receive them.

In my musical world ghost notes are dead/percussive notes usually played just before the main note, very common in funky bass lines. But I bet that's not what you mean.

Steve

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html .
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :slight_smile:

slipstick:
Perhaps you could help by explaining what you mean by 'ghost notes' and exactly when you receive them.

In my musical world ghost notes are dead/percussive notes usually played just before the main note, very common in funky bass lines. But I bet that's not what you mean.

Steve

Sorry if I used the wrong term. When I plug my arduino midiusb device to my pc(leonardo), all of the notes suddenly plays. I mean ALL OF THEM, but when I connect it to my Android device, it's fine... Thank You slipstick for your help.

So let's get this clear. You're running that one program on a Leonardo and as soon as you plug the USB cable in and without pressing any keys/buttons all 42 notes play at once? And that only happens when you plug it into a Windows 10 PC? If you plug it into an Android device (what is this device?) individual notes play normally only when you press the key/button? And nothing else changes at the Arduino end, same Leonardo, same program, same USB cable just plugged in to something different. Sorry that makes no sense to me.

What are the programs/apps in the Android device and in the PC that are playing the notes?

Steve

Corrected the code posting. Well done!

slipstick:
So let's get this clear. You're running that one program on a Leonardo and as soon as you plug the USB cable in and without pressing any keys/buttons all 42 notes play at once? And that only happens when you plug it into a Windows 10 PC? If you plug it into an Android device (what is this device?) individual notes play normally only when you press the key/button? And nothing else changes at the Arduino end, same Leonardo, same program, same USB cable just plugged in to something different. Sorry that makes no sense to me.

What are the programs/apps in the Android device and in the PC that are playing the notes?

Steve

I am using Perfect Piano for Android. It works perfectly. I am using Synthesia for my pc. I tried other Piano apps for pc but I keep receiving random notes whichever I use. I am using the same program for my Android and PC same Leonardo and same USB cable. I don't know why it fails to do it's purpose.

Sounds like Windows is toggling DTR when the serial port opens, but Anne Droid isn’t.
Easy solution if that’s the case, break the jumper link on the Arduino to stop serial DTR resetting the board.

lastchancename:
Sounds like Windows is toggling DTR when the serial port opens, but Anne Droid isn’t.
Easy solution if that’s the case, break the jumper link on the Arduino to stop serial DTR resetting the board.

That can't be it, he's using a Leonardo.

You keep changing the story. Are you getting ALL of the notes i.e. a complete matrix full, all together as soon as you plug in, like you originally said. Or are you getting, as in your latest post, odd RANDOM notes playing? They're not the same.

Can you run a MIDI logging program on your PC, something like MIDIOX , and post examples of the MIDI messages received a) when you first plug in WITHOUT touching any key/button and then b) when you play a few notes.

Steve

slipstick:
You keep changing the story. Are you getting ALL of the notes i.e. a complete matrix full, all together as soon as you plug in, like you originally said. Or are you getting, as in your latest post, odd RANDOM notes playing? They're not the same.

Can you run a MIDI logging program on your PC, something like MIDIOX , and post examples of the MIDI messages received a) when you first plug in WITHOUT touching any key/button and then b) when you play a few notes.

Steve

Yes I keep receiving all the notes but each note appears randomly.

So you don't get all the notes at once as soon as you plug in. I give up!

Steve