Go Down

Topic: MIDI in on Leonardo via MidiUSB (Read 748 times) previous topic - next topic

ledsfortony

Hello there!
I'm trying to read MIDI from an electronic keyboard and would appreciate if you could tell me if I'm properly reading MIDI over a USB connection. I'm going to control some LEDs, but I need to get this part down before I can move forward to LED manipulation.

The code I'm using is as such
Code: [Select]

#include <MIDIUSB.h>
#include <MIDIUSB_Defs.h>
#include <frequencyToNote.h>
#include <pitchToFrequency.h>
#include <pitchToNote.h>

void noteOn(byte channel, byte pitch, byte velocity) {
  midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
  // The notes on a keyboard go from 21 to 108. To get the first note to correspond to the first LED, I'm going to subtract 21 from the pitch number and store it.
  short R = pitch-21;
}

void noteOff(byte channel, byte pitch, byte velocity) {
  midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
  //Here, we will a. correlate a MIDI note to a key b. turn off that key's associated light
  short R = pitch-21;
}

void setup() {
}

void loop() {
    midiEventPacket_t rx;
    rx = MidiUSB.read();
}



The code here is based off the MIDIUSB github's "read" example.

My main question is: I believe "pitch" is the numeric representation of the midi note. Is this correct? If using MIDIUSB, do I need to specify noteOn and noteOFF like with the standard serial MIDI library? Once I know how to move forward, I already know how to play with the lights.
Thank you for your time.

Grumpy_Mike

#1
Sep 08, 2019, 12:42 am Last Edit: Sep 08, 2019, 12:50 am by Grumpy_Mike
Quote
I'm trying to read MIDI from an electronic keyboard
You can't do that.
A keyboard is a USB client device and so is a Leonardo. You can only connect a client to a host. So you would need a USB host shield to do this.

Something like this:-
http://www.hobbytronics.co.uk/usb-host-midi

Go Up