MIDI react LED using Arduino. (but im really stuck)

Grumpy_Mike:
I have a Couple of Teensy 3.6s, I think they are overkill for your project a Teensy 3.2 would be fine.

PieterP:
I only have a Teensy 3.2, and it works fine. I can't say anything about the newer versions, but they seem to be very powerful, and knowing PJRC's usual products and libraries, they will be well-supported.

Hi, Teensy 3.2 works perfectly. I tried lots of examples and works. Inputs, outputs are okay also WS2812-NeoPixel Libraries are okay. But I have trouble about using usbMIDI examples. When I try to usbMIDI examples. I got this "'usbMIDI' was not declared in this scope"

This is InputRead example(internal example.):

/* Receive Incoming USB MIDI by reading data.  This approach
   gives you access to incoming MIDI message data, but requires
   more work to use that data.  For the simpler function-based
   approach, see InputFunctionsBasic and InputFunctionsComplete.

   Use the Arduino Serial Monitor to view the messages
   as Teensy receives them by USB MIDI

   You must select MIDI from the "Tools > USB Type" menu

   This example code is in the public domain.
*/

void setup() {
  Serial.begin(115200);
}

void loop() {
  // usbMIDI.read() needs to be called rapidly from loop().  When
  // each MIDI messages arrives, it return true.  The message must
  // be fully processed before usbMIDI.read() is called again.
  if (usbMIDI.read()) {
    processMIDI();
  }
}

void processMIDI(void) {
  byte type, channel, data1, data2, cable;

  // fetch the MIDI message, defined by these 5 numbers (except SysEX)
  //
  type = usbMIDI.getType();       // which MIDI message, 128-255
  channel = usbMIDI.getChannel(); // which MIDI channel, 1-16
  data1 = usbMIDI.getData1();     // first data byte of message, 0-127
  data2 = usbMIDI.getData2();     // second data byte of message, 0-127
  cable = usbMIDI.getCable();     // which virtual cable with MIDIx8, 0-7

  // uncomment if using multiple virtual cables
  //Serial.print("cable ");
  //Serial.print(cable, DEC);
  //Serial.print(": ");

  // print info about the message
  //
  switch (type) {
    case usbMIDI.NoteOff: // 0x80
      Serial.print("Note Off, ch=");
      Serial.print(channel, DEC);
      Serial.print(", note=");
      Serial.print(data1, DEC);
      Serial.print(", velocity=");
      Serial.println(data2, DEC);
      break;

    case usbMIDI.NoteOn: // 0x90
      Serial.print("Note On, ch=");
      Serial.print(channel, DEC);
      Serial.print(", note=");
      Serial.print(data1, DEC);
      Serial.print(", velocity=");
      Serial.println(data2, DEC);
      break;

    case usbMIDI.AfterTouchPoly: // 0xA0
      Serial.print("AfterTouch Change, ch=");
      Serial.print(channel, DEC);
      Serial.print(", note=");
      Serial.print(data1, DEC);
      Serial.print(", velocity=");
      Serial.println(data2, DEC);
      break;

    case usbMIDI.ControlChange: // 0xB0
      Serial.print("Control Change, ch=");
      Serial.print(channel, DEC);
      Serial.print(", control=");
      Serial.print(data1, DEC);
      Serial.print(", value=");
      Serial.println(data2, DEC);
      break;

    case usbMIDI.ProgramChange: // 0xC0
      Serial.print("Program Change, ch=");
      Serial.print(channel, DEC);
      Serial.print(", program=");
      Serial.println(data1, DEC);
      break;

    case usbMIDI.AfterTouchChannel: // 0xD0
      Serial.print("After Touch, ch=");
      Serial.print(channel, DEC);
      Serial.print(", pressure=");
      Serial.println(data1, DEC);
      break;

    case usbMIDI.PitchBend: // 0xE0
      Serial.print("Pitch Change, ch=");
      Serial.print(channel, DEC);
      Serial.print(", pitch=");
      Serial.println(data1 + data2 * 128, DEC);
      break;

    case usbMIDI.SystemExclusive: // 0xF0
      // Messages larger than usbMIDI's internal buffer are truncated.
      // To receive large messages, you *must* use the 3-input function
      // handler.  See InputFunctionsComplete for details.
      Serial.print("SysEx Message: ");
      printBytes(usbMIDI.getSysExArray(), data1 + data2 * 256);
      Serial.println();
      break;

    case usbMIDI.TimeCodeQuarterFrame: // 0xF1
      Serial.print("TimeCode, index=");
      Serial.print(data1 >> 4, DEC);
      Serial.print(", digit=");
      Serial.println(data1 & 15, DEC);
      break;

    case usbMIDI.SongPosition: // 0xF2
      Serial.print("Song Position, beat=");
      Serial.println(data1 + data2 * 128);
      break;

    case usbMIDI.SongSelect: // 0xF3
      Serial.print("Sond Select, song=");
      Serial.println(data1, DEC);
      break;

    case usbMIDI.TuneRequest: // 0xF6
      Serial.println("Tune Request");
      break;

    case usbMIDI.Clock: // 0xF8
      Serial.println("Clock");
      break;

    case usbMIDI.Start: // 0xFA
      Serial.println("Start");
      break;

    case usbMIDI.Continue: // 0xFB
      Serial.println("Continue");
      break;

    case usbMIDI.Stop: // 0xFC
      Serial.println("Stop");
      break;

    case usbMIDI.ActiveSensing: // 0xFE
      Serial.println("Actvice Sensing");
      break;

    case usbMIDI.SystemReset: // 0xFF
      Serial.println("System Reset");
      break;

    default:
      Serial.println("Opps, an unknown MIDI message type!");
  }
}


void printBytes(const byte *data, unsigned int size) {
  while (size > 0) {
    byte b = *data++;
    if (b < 16) Serial.print('0');
    Serial.print(b, HEX);
    if (size > 1) Serial.print(' ');
    size = size - 1;
  }
}

This is my code and I also get same trouble mesagge here.

#include <Adafruit_NeoPixel.h> // Add Led Library

//define NeoPixel Pin and Number of LEDs
#define PIN 6
#define NUM_LEDS 88

//    strip.setPixelColor(4, 120, 255, 40);
//    strip.setPixelColor(Number of LED, RedDensity, GreenDensity, BlueDensity);
#define RedDensity    150
#define GreenDensity  199
#define BlueDensity   88

//create a NeoPixel strip
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  
  strip.begin(); // start the strip and blank it out
  strip.show();
  
  Serial.begin(115200);  // Hairless MIDI speed

  usbMIDI.setHandleNoteOn(myNoteOn);
  usbMIDI.setHandleNoteOff(myNoteOff);
}


void loop() {
  // The handler functions are called when usbMIDI reads data.  They
  // will not be called automatically.  You must call usbMIDI.read()
  // regularly from loop() for usbMIDI to actually read incoming
  // data and run the handler functions as messages arrive.
  usbMIDI.read();
}

void myNoteOn(byte channel, byte note, byte velocity) {
  // When using MIDIx4 or MIDIx16, usbMIDI.getCable() can be used
  // to read which of the virtual MIDI cables received this message.
    strip.setPixelColor(pitch-20, RedDensity, GreenDensity, BlueDensity);
    strip.show();
}

void myNoteOff(byte channel, byte note, byte velocity) {
      strip.setPixelColor(pitch-20, 0, 0, 0);
      strip.show();
}

If I want to use usbMIDI do I write the #include usbMIDI or something like that?

You have to select the correct USB mode from the Tools menu.

Also, your program will crash/behave unpredictably if pitch is less than 20 or greater than 107.

PieterP:
You have to select the correct USB mode from the Tools menu.

Also, your program will crash/behave unpredictably if pitch is less than 20 or greater than 107.

Do you mean this?

My piano pitch scale is 21 to 108. Its 88 key piano. I cannot send MIDI mesagge out of that scale.

Yes.

Your logic is still flawed. The LEDs are zero-based, so you have to subtract 21, not 20. Even if your piano doesn't send out notes outside of that range, you still have to account for it. What happens if you use MIDI software, for example? Or if you transpose your piano? It takes one extra line of code. You can use the Arduino constrain macro.

PieterP:
Yes.

Your logic is still flawed. The LEDs are zero-based, so you have to subtract 21, not 20. Even if your piano doesn't send out notes outside of that range, you still have to account for it. What happens if you use MIDI software, for example? Or if you transpose your piano? It takes one extra line of code. You can use the Arduino constrain macro.

Thanks for suggestion. I'll consider your suggestion.

Still I can't use Teensy's own MIDI library. My Teensy is not responding. Almost I have tried all things to do. I get stuck again. Teensy can not see my Piano. I think I need some help

Teensy can not see my Piano

Why should it?

A piano needs to be connected to a USB host if it is a USB connector. If it is a 5 Pin DIN connector it can.

What connection have you got?

Grumpy_Mike:
Why should it?

A piano needs to be connected to a USB host if it is a USB connector. If it is a 5 Pin DIN connector it can.

What connection have you got?

USB connection. Pieter said "You didn't have to use HairlessMIDI" and I thought the Teensy can see MIDI signal over USB.

You misunderstood what he said. He said you didn’t need hairless for the PC to see the Teensy as a USB device.

He said nothing about a Teensy acting as a USB host.

sigh

https://www.pjrc.com/teensy/td_midi.html

This is for what? PC to Teensy or Teensy to PC?

Even bigger sigh - that link doesn’t work.

This is for what? PC to Teensy or Teensy to PC?

Yes.

You don’t get it do you. A Teensy can send MIDI to a PC, and receive MIDI from a PC.

But it can not act like a PC for a MIDI device that is expecting to see a PC.
If you would answer

What connection have you got?

It would be good, but if it is a USB cable then you need the Teensy to act as a host. Then you can’t have that Teensy act as a client to the PC.

So where are you plugging the keyboard into. If it is the PC then you need to route that MIDI to your Teensy, this will not happen automatically.

PieterP:
Yes.

Your logic is still flawed. The LEDs are zero-based, so you have to subtract 21, not 20. Even if your piano doesn't send out notes outside of that range, you still have to account for it. What happens if you use MIDI software, for example? Or if you transpose your piano? It takes one extra line of code. You can use the Arduino constrain macro.

I still can't send MIDI signal to Teensy over PC. I try Serial.begin, USB Type : MIDI & usbMIDI codes, MIDI.h library. How can I send MIDI signal to Teensy? over HairlessMIDI or Teensy can take MIDI signal on PC? How can I route them to Teensy. Because Mike says its not happen automatically.

  1. You have a piano with a MIDI interface using a USB connection, right?
  2. It doesn't have 5-pin MIDI connectors, correct?
  3. You want to connect the Teensy directly to the piano to display the playing notes.
  4. Up 'till now, you used the computer in between, but it would be nice to just leave the computer out entirely.

Correct, or not?

PieterP:

  1. You have a piano with a MIDI interface using a USB connection, right?
  2. It doesn't have 5-pin MIDI connectors, correct?
  3. You want to connect the Teensy directly to the piano to display the playing notes.
  4. Up 'till now, you used the computer in between, but it would be nice to just leave the computer out entirely.

Correct, or not?

  1. Correct this is my piano.
  2. Correct, I'm using this.
  3. Correct, over PC connection. Because I'll record my piano pieces too so then I need piano connect.
  4. If I leave computer I can not record my pieces, covers etc. So I need piano connection to my PC. If there is a way to split MIDI to the 2 same signal(1 for Teensy via 5 pin MIDI connector and 1 for directly PC for recording my MIDIs) It will be OK for me. But I surely need record my MIDIs to PC.

That keyboard has a connection to a “USB host” according to that link. That means it is not very easy to connect it directly to an Arduino of any type. You need some sort of host shield, or host to serial converter.

This is the lowest cost one I have come across:-

Alright. You'll need to route the MIDI output from your piano to the MIDI input of the Teensy. This has to be done on your computer in software. On Linux, I use QJackCtl, I haven't tried any Windows equivalents.
Some DAWs may support it, or you can use a software tool.

Grumpy_Mike:
That keyboard has a connection to a “USB host” according to that link. That means it is not very easy to connect it directly to an Arduino of any type. You need some sort of host shield, or host to serial converter.

This is the lowest cost one I have come across:-
USB Host Board – MIDI Software – HobbyTronics

The HairlessMIDI converts USB's MIDI signal to Serial.

PieterP:
Alright. You'll need to route the MIDI output from your piano to the MIDI input of the Teensy. This has to be done on your computer in software. On Linux, I use QJackCtl, I haven't tried any Windows equivalents.
Some DAWs may support it, or you can use a software tool.

When I was using Arduino I was loosing some datas but I was sending Piano's MIDI signal to Arduino perfectly. But I cant even run this code on Teensy

void OnNoteOn(byte channel, byte note, byte velocity) {
  digitalWrite(LED_BUILTIN, HIGH); // Any Note-On turns on LED
}

void OnNoteOff(byte channel, byte note, byte velocity) {
  digitalWrite(LED_BUILTIN, LOW);  // Any Note-Off turns off LED
}

void setup() {
  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);
  usbMIDI.setHandleNoteOff(OnNoteOff);
  usbMIDI.setHandleNoteOn(OnNoteOn) ;
  digitalWrite(LED_BUILTIN, HIGH);
  delay(400);                 // Blink LED once at startup
  digitalWrite(LED_BUILTIN, LOW);
}

void loop() {
  usbMIDI.read();
}

I was sending Midi signal over Serial port but its not giving any reaction. (I tried any USB Type Serial, Serial-MIDI, Serial-MIDI4x, Serial-MIDI16x) but my Teensy's Led not responding. Also MIDI.h library doesnt work.

ozguney:
But I cant even run this code on Teensy
...
I was sending Midi signal over Serial port but its not giving any reaction. (I tried any USB Type Serial, Serial-MIDI, Serial-MIDI4x, Serial-MIDI16x) but my Teensy's Led not responding.

The Teensy is a USB MIDI device. Sending Serial data to it will not magically turn it into MIDI messages.

Option one, use a MIDI router, as explained earlier.

Option two, don't use USB MIDI, but use Serial instead, and use Hairless to do the routing.

ozguney:
Also MIDI.h library doesnt work.

I would be surprised, as the last time I used it, it worked perfectly fine, so you'll have to elaborate.

ozguney:
The HairlessMIDI converts USB's MIDI signal to Serial.

But is does not convert a USB host MIDI signal, especially when you have no PC in the mix.

The trouble is you keep moveing the goal posts. That board is needed if you want your keyboard to talk to the Teensy without a PC in the mix, which was one of your configurations you want to use. That board or one like it is needed to connect your keyboard to an Arduino without going through a computer.