Standalone midi controller using PC keyboard thru the USB host shield

hello,
I would like to use a computer keyboard to send midi notes. I use a arduino leonardo, a USB host shield in which I plug the keyboard and the midiUSB library.
here the usb host shield reference :
USBhostshield

Everything works fine as long as the IDE is open, but as soon as I close the Arduino IDE application, it stops working.
It seems that the problem comes from the setup and loop needed for the keyboard.

void setup()
{
  Serial.begin( 115200 );
#if !defined(__MIPSEL__)
  while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
#endif
  Serial.println("Start");

  if (Usb.Init() == -1)
    Serial.println("OSC did not start.");

  delay( 200 );

  HidKeyboard.SetReportParser(0, &Prs);
}

void loop()
{
  Usb.Task();
}

i find this piece of code in the example of the USB host shield library.
and i have a function to send midi like this

void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key)
{
  noteOn(0, scale[key], 64); //send midi
  MidiUSB.flush();
  delay(20);
}

Do you know if there is a conflict between the keyboard void setup and void loop and the way the midiUSB library is working ?
once again, everything is working as long as the IDE is open, but not as a standalone device.
any idea ?
thanks a lot

Hi @louis-dodo23

you will have to explain in more detail what you want to do.

You wrote

what is the exact opposite "configuration" to

As you write that this "configuration" with opened IDE is working.

Does this opposite configuration mean:

  • IDE is just closed but your microcontroller (which exact type of microcontroller??) and USB-hostshield is still connected to your computer?

  • Your microcontroller plus USB-host-shield is connected to what kind of MIDI-device?

you should post your complete sketch from the very first to the very last line of code and you should explicitly write that this is exact that code that shows the much more detailed described behaviour

hey @StefanL38
thanks for your reply
ok i will try to be more precise.

It is working when the microcontroller is connected to my computer with opened IDE.

  • when i close the IDE and reset the microcontroller, still connected to the computer, it doesn't work anymore.
  • it doesn't work neither when i try to plug it to my midi device as a host. the midi device is a Hapax squarp : hapax

i am using a arduino leonardo

and here is the complete sketch :

#include "MIDIUSB.h"
#include <hidboot.h>
#include <usbhub.h>

// Satisfy the IDE, which needs to see the include statment in the ino too.
#ifdef dobogusinclude
#include <spi4teensy3.h>
#endif
#include <SPI.h>

int scale[10] = {24,41,39,26,14,27,28,29,19,30}; //notes midi number i want to send

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

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

class KbdRptParser : public KeyboardReportParser
{
  protected:
    void OnKeyDown	(uint8_t mod, uint8_t key);
    void OnKeyUp	(uint8_t mod, uint8_t key);
};

void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key)
{
  noteOn(0, gamme[key], 64); //send midi note on
  MidiUSB.flush();
  delay(20);
}

void KbdRptParser::OnKeyUp(uint8_t mod, uint8_t key)
{
  noteOff(0, gamme[key], 64); // send midi note off
  MidiUSB.flush();
  delay(20);

}

USB     Usb;
//USBHub     Hub(&Usb);
HIDBoot<USB_HID_PROTOCOL_KEYBOARD>    HidKeyboard(&Usb);

KbdRptParser Prs;

void setup()
{
  Serial.begin( 115200 );
#if !defined(__MIPSEL__)
  while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
#endif
  Serial.println("Start");

  if (Usb.Init() == -1)
    Serial.println("OSC did not start.");

  delay( 200 );

  HidKeyboard.SetReportParser(0, &Prs);
}

void loop()
{
  Usb.Task();
}

:folded_hands:

I am not too familiar with USB. My 2 cents I know about it is that the two device that are connected to each other one is the host and one is the peripheral.

Now as the thing that you connected to the microcontroller is called host-shield I would think the host-shield is acting as the host. And then the other side would be the peripheral.

standard examples:
a computer (the USB-host) has connected a USB-mouse (a peripheral)
a computer has connected a USB-keyboard (a peripheral)

I do not know what the role ( the host or the peripheral) the hapax-midi-devic is having.

As for the IDE beeing closed and it does not work
I guess this line of code explains it

The comment says it:
As long as the serial connection is not established stay in the empty while-loop

You can check this by making a led blink fast inside the while (!serial)-loop
and make it blink slower after the while(!serial)-loop

The Hajax productpage says

Hapax can sequence and synchronize all your gear.

But what does this mean in relation to beeing a host or a peripheral? I don't know

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