Adafruit NeoKey 1x4 and MIDI problem in Arduino

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
trumpetto
 
Posts: 3
Joined: Wed Jun 12, 2019 1:23 pm

Adafruit NeoKey 1x4 and MIDI problem in Arduino

Post by trumpetto »

I use a QT Py with an Adafruit NeoKey 1x4 QT I2C Breakout to create a small MIDI controller.
Using different MIDI over USB libraries my code seems to fail on reading the neokey.
Below the code of a standard example that works without the seesaw library and NeoKey stuff..
Adding the library is OK but as soon as I add a "neokey.read()" my code stops.
The QT Py with Adafruit NeoKey 1x4 QT I2C Breakout works OK with other code (no MIDI).

Lines I added are tagged "// ADDED FOR NeoKey TEST"

Any tips are welcome :-)

The code:

Code: Select all

/*********************************************************************
 Adafruit invests time and resources providing this open source code,
 please support Adafruit and open-source hardware by purchasing
 products from Adafruit!

 MIT license, check LICENSE for more information
 Copyright (c) 2019 Ha Thach for Adafruit Industries
 All text above, and the splash screen below must be included in
 any redistribution
*********************************************************************/

/* This sketch is enumerated as USB MIDI device. 
 * Following library is required
 * - MIDI Library by Forty Seven Effects
 *   https://github.com/FortySevenEffects/arduino_midi_library
 */

#include <Arduino.h>
#include <Adafruit_TinyUSB.h>
#include <MIDI.h>


#include <Adafruit_NeoKey_1x4.h>          // ADDED FOR NeoKey TEST
#include <seesaw_neopixel.h>              // ADDED FOR NeoKey TEST
Adafruit_NeoKey_1x4 neokey;               // ADDED FOR NeoKey TEST


// USB MIDI object
Adafruit_USBD_MIDI usb_midi;

// Create a new instance of the Arduino MIDI Library,
// and attach usb_midi as the transport.
MIDI_CREATE_INSTANCE(Adafruit_USBD_MIDI, usb_midi, MIDI);

// Variable that holds the current position in the sequence.
uint32_t position = 0;

// Store example melody as an array of note values
byte note_sequence[] = {
  74,78,81,86,90,93,98,102,57,61,66,69,73,78,81,85,88,92,97,100,97,92,88,85,81,78,
  74,69,66,62,57,62,66,69,74,78,81,86,90,93,97,102,97,93,90,85,81,78,73,68,64,61,
  56,61,64,68,74,78,81,86,90,93,98,102
};

void setup()
{
#if defined(ARDUINO_ARCH_MBED) && defined(ARDUINO_ARCH_RP2040)
  // Manual begin() is required on core without built-in support for TinyUSB such as mbed rp2040
  TinyUSB_Device_Init(0);
#endif

  pinMode(LED_BUILTIN, OUTPUT);
  
  usb_midi.setStringDescriptor("TinyUSB MIDI"); 

  // Initialize MIDI, and listen to all MIDI channels
  // This will also call usb_midi's begin()
  MIDI.begin(MIDI_CHANNEL_OMNI);

  // Attach the handleNoteOn function to the MIDI Library. It will
  // be called whenever the Bluefruit receives MIDI Note On messages.
  MIDI.setHandleNoteOn(handleNoteOn);

  // Do the same for MIDI Note Off messages.
  MIDI.setHandleNoteOff(handleNoteOff);

  Serial.begin(115200);

  // wait until device mounted
  while( !TinyUSBDevice.mounted() ) delay(1);
}

void loop()
{



/* If the line below is active (just reading the neokey 1x4) my code is not executed  */

//  uint8_t buttons = neokey.read();    // ADDED FOR NeoKey TEST

  
  static uint32_t start_ms = 0;
  if ( millis() - start_ms > 266 )
  {
    start_ms += 266;
    
    // Setup variables for the current and previous
    // positions in the note sequence.
    int previous = position - 1;
  
    // If we currently are at position 0, set the
    // previous position to the last note in the sequence.
    if (previous < 0) {
      previous = sizeof(note_sequence) - 1;
    }
  
    // Send Note On for current position at full velocity (127) on channel 1.
    MIDI.sendNoteOn(note_sequence[position], 127, 1);
  
    // Send Note Off for previous note.
    MIDI.sendNoteOff(note_sequence[previous], 0, 1);
  
    // Increment position
    position++;
  
    // If we are at the end of the sequence, start over.
    if (position >= sizeof(note_sequence)) {
      position = 0;
    }
  }

  // read any new MIDI messages
  MIDI.read();  
}

void handleNoteOn(byte channel, byte pitch, byte velocity)
{
  // Log when a note is pressed.
  Serial.print("Note on: channel = ");
  Serial.print(channel);

  Serial.print(" pitch = ");
  Serial.print(pitch);

  Serial.print(" velocity = ");
  Serial.println(velocity);
}

void handleNoteOff(byte channel, byte pitch, byte velocity)
{
  // Log when a note is released.
  Serial.print("Note off: channel = ");
  Serial.print(channel);

  Serial.print(" pitch = ");
  Serial.print(pitch);

  Serial.print(" velocity = ");
  Serial.println(velocity);
}

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: Adafruit NeoKey 1x4 and MIDI problem in Arduino

Post by mikeysklar »

@trumpetto,

Can you narrow it down to which library is causing the conflict that causes the fail at neokey.read()? If so, it would be a good idea to open an issue on the Adafruit_Seesaw github repo.

https://github.com/adafruit/Adafruit_Seesaw/issues

User avatar
trumpetto
 
Posts: 3
Joined: Wed Jun 12, 2019 1:23 pm

Re: Adafruit NeoKey 1x4 and MIDI problem in Arduino

Post by trumpetto »

mikeysklar wrote:@trumpetto,

Can you narrow it down to which library is causing the conflict that causes the fail at neokey.read()? If so, it would be a good idea to open an issue on the Adafruit_Seesaw github repo.

https://github.com/adafruit/Adafruit_Seesaw/issues
Thanks for the reply,

A good conflict has two sides :-)
It is hard (for me) to find out what library should be blamed/fixed or what mistake I make.
I narrowed it down to a smaller piece of code and will open an issue at the Adafruit_Seesaw github repo.
I cannot remove more libraries because the code needs it.

For completeness: I have exactly the same problem with #include "MIDIUSB.h" which uses the Arduino USB Stack.


Activating the first line of the loop() causes the crash.

Code: Select all

#include <Arduino.h>
#include <Adafruit_TinyUSB.h>
#include <MIDI.h>
#include <Adafruit_NeoKey_1x4.h>
#include <seesaw_neopixel.h>

Adafruit_NeoKey_1x4 neokey;
Adafruit_USBD_MIDI usb_midi;

MIDI_CREATE_INSTANCE(Adafruit_USBD_MIDI, usb_midi, MIDI);

void setup()
{
  #if defined(ARDUINO_ARCH_MBED) && defined(ARDUINO_ARCH_RP2040)
    TinyUSB_Device_Init(0);
  #endif

  pinMode(LED_BUILTIN, OUTPUT);
  usb_midi.setStringDescriptor("TinyUSB MIDI"); 
  MIDI.begin(MIDI_CHANNEL_OMNI);
  Serial.begin(115200);
  while( !TinyUSBDevice.mounted() ) delay(1);   // wait until device mounted
}

void loop()
{
  //uint8_t buttons = neokey.read();
  MIDI.sendNoteOn(60, 80, 1);
  delay(500); 
  MIDI.sendNoteOff(60, 0, 1);
  delay(1500); 
}

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: Adafruit NeoKey 1x4 and MIDI problem in Arduino

Post by mikeysklar »

Thanks for shrinking the example and opening an issue with the github repo.

That will ensure that others see it and acknowledge it.

In the meantime if you want to experiment with what might be happening when neokey.read() is called it looks like a very small function. You could try to comment out individual lines to locate the culprit.

Code: Select all

  uint8_t read(void);

  seesaw_NeoPixel pixels; ///< the onboard neopixel matrix

  friend class Adafruit_MultiNeoKey1x4; ///< for allowing use of protected
                                        ///< methods by aggregate class

protected:
  uint8_t last_buttons = 0; ///< The last reading for the buttons
  uint8_t _addr;            ///< the I2C address of this board
  NeoKey1x4Callback (*_callbacks[NEOKEY_1X4_KEYS])(
      keyEvent); ///< the array of callback functions
};
https://github.com/adafruit/Adafruit_Se ... _1x4.h#L50

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”