Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 6 of 6

Thread: USB MIDI stops working after a few seconds

  1. #1
    Junior Member
    Join Date
    Aug 2017
    Posts
    13

    USB MIDI stops working after a few seconds

    First of all, I'd like to thank Paul and whoever else had a part in it for making and regularly improving the teensy. I loved it ever since I knew about it, and today I finally received my first 3.2.
    My first project will be a two-way MIDI controller (sending control changes and have LEDs on the device, possibly motorfaders as well, change according to MIDI data the teensy receives), and so today I started prototyping with a rotary encoder (the data sheet can be found here http://cdn-reichelt.de/documents/dat...650/401588.pdf ) . The two outer pins are connected to digital in 2+3 , while the middle pin is connected to GND. Device type is just USB Midi. It works, however only for less than a minute at which point it will no longer update, serial.print stays blank and no control change gets sent . I also seem to experience occasional mouse and keyboard freezes ( i think the teensy , the mouse and keyboard are connected on the same port) and had 3 blue screens which iirc seemed to happen while i had Ableton (the MIDI "receiver" in my case) open and tried to reprogram the board.
    Before posting , I did a little googling and found a possible solution of clearing the MIDI the teensy receives . Even tho Ableton doesnt have the Teensy MIDI Out set as an output, i am still clearing the buffer, but it wont help.

    I did not ground any inputs, so only pins GND, D2 and D3 are used at the moment as well as D4 for the rotary encoder's switch, however when it does work, the rotary encoder seems to produce "correct" outputs.


    This is my code (based on the Encoder example)

    Code:
    #include <MIDI.h>
    
    
    #include <Encoder.h>
    
    // Change these pin numbers to the pins connected to your encoder.
    //   Best Performance: both pins have interrupt capability
    //   Good Performance: only the first pin has interrupt capability
    //   Low Performance:  neither pin has interrupt capability
    Encoder knobLeft(2, 3);
    
    //   avoid using pins with LEDs attached
    
    void setup() {
      Serial.begin(9600);
      Serial.println("TwoKnobs Encoder Test:");
    pinMode(4,INPUT_PULLUP);
    }
    
    long positionLeft  = -999;
    
    
    void loop() {
      digitalWrite(13,digitalRead(4));
      
      long newLeft;
      newLeft = knobLeft.read();
    
    
      if (newLeft != positionLeft ) {
        Serial.print("Left = ");
        Serial.print(newLeft);  
    
     
        Serial.println();
        positionLeft = newLeft;
        
    
    usbMIDI.sendControlChange(22,positionLeft,1);
    positionLeft = 0;
    knobLeft.write(0);
       }
     
      // if a character is sent from the serial monitor,
      // reset both back to zero.
      if (Serial.available()) {
        Serial.read();
        Serial.println("Reset both knobs to zero");
        knobLeft.write(10);
    //value of 10 for troubleshooting (after the reset , it will always log a change) 
      }
    while (usbMIDI.read()) ;
    delay(100);
      
    }
    The other weird thing is (nothing to do with MIDI here), the LED never lights up (also I assume since the rotary encoder's switch is set as a pullup , the led should light when i am not pressing the button, correct?)
    Any ideas?

    Bonus question: Is there any way where i dont have to manually set knobLeft.write() ? Ideally id like the returned value to be the change since the last check / CC message ; im assuming the Encoder class does the "absolute" counting , so if theres a setting or a different library i can use, Im all ears.

    Apologies for asking a trivial question that gets asked too frequently and not being able to provide more troubleshooting details right now, i am just a little stuck on why it happens and how to solve it.

    Thanks for reading and best regards,
    Jazz

  2. #2
    Senior Member oddson's Avatar
    Join Date
    Feb 2013
    Location
    Isle in the Salish Sea
    Posts
    1,264
    I've not read all carefully but I wonder if this is the problem
    Code:
    while (usbMIDI.read()) ;
    delay(100);
      
    }
    As messages are recidved the delay causes the loop to fall behind the midi event queue???

  3. #3
    Junior Member
    Join Date
    Aug 2017
    Posts
    13
    Hey oddson, thanks for your reply - I believe I have to blame my sloppy formatting here (in my defense, it's just a quick testing code but the style police would lock me up regardless).
    This should read more clearly:
    Code:
     
    while ( usbMIDI.read() )
    ;
    
    delay (100);
    So normally, at least to my understanding, it should read and discard all MIDI messages in the receive queue and only then delay the loop.

    Luckily, I have all day to work on it , so I will probably do some experiments with simple pushbuttons and NoteOn to see if I run into similar issues. Will try to report results later.

    Thanks and best regards!

  4. #4
    Senior Member oddson's Avatar
    Join Date
    Feb 2013
    Location
    Isle in the Salish Sea
    Posts
    1,264
    Ok... lazy reading on my part... plus you said you added this after you had the problem

    It does sound like a queue is overflowing... does a lesser delay cause it to freeze sooner?

  5. #5
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    21,081
    The delay(100) line may be problematic, if you have software on the PC side sending lots of MIDI messages. A pretty incredible number of messages can arrive in a tenth of a second, if something is sending data quickly.

    Why have this delay at all?

  6. #6
    Junior Member
    Join Date
    Aug 2017
    Posts
    13
    Well , I feel rather stupid now - I initially attached headers by only soldering the corner pins, thinking it would be enough, so it turns out it was just a contact issue - classic noob mistake, my apologies.
    Both the pushbutton as well as the rotary encoder seem to work, however I get occasional jittery response from the rotary (for example slight CCW turns give the correct -1 output with the occasional +43, +100 or similar). Could this be related to ungrounded input pins? Possibly the library ? I have to reset the saved encoder value to 0 on each read as seen in my code, so possibly theres some latency and the value doesn't get changed in time?
    Also, if anybody knows a different library or a more robust way to have the rotary encoder return relative values ,I'd be very curious to know.
    As an example, with the encoder library , if i was to turn 10 "clicks" CW , the value would be 10, if i were then to turn 3 clicks CCW , the returned value will be 7 - if possible I'd prefer the readouts to be +10, then -3. Any ideas? I saw a bunch of different rotary libraries on the arduino site , but unless i'm mistaken only the "Encoder" one (that i'm currently using) comes pre-installed with teensyduino?

    Thanks for your help so far, hopefully I will be able to give a progress report soon and do a writeup on a LED ring for the encoders if i manage to get it working (plan would be to read CC data from MIDI in and send it out to a max7221 to drive the LED rings (made up of single LEDs) )


    ETA: I should have clarified, the delay was only added for troubleshooting, I wanted to rule out the possibility of too many MIDI messages being sent from the teensy causing the problems - oh and again thanks Paul for everything you do , I'm sure I'm not the only one who greatly appreciates it

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •