Go Down

Topic: Teensy ++ 2.0 Problems With USB Extension Cable (Read 4389 times) previous topic - next topic

HFTobeason

Please excuse my lack of experience.  I have built a Teensy ++ 2.0 device that reads the output of a potentiometer, does some math on it, and sends the result as a MIDI controller value between 0 and 127.  This device works perfectly when connected directly to my computer with a one-foot USB cable.  However, when I connect the device using a Gefen USB MIDI Extender (Gefen EXT-USB-MIDI S <http://www.gefen.com/kvm/dproduct.jsp?prod_id=2218>) the MIDI controller values become totally unstable and randomly jump to values between 0 and 127.  Here is my code (note that the device reads two potentiometers as well as two on/off pushbuttons):

Code: [Select]
void setup()
{               
  pinMode(7, INPUT_PULLUP);    // Program Change INC button
  pinMode(8, INPUT_PULLUP);    // Program Change DEC button
}

// Global variables

int midiChannel = 1;
int decCCNumber = 21;
int incCCNumber = 22;
int volCCNumber = 7;
int wahCCNumber = 14;

// Inc/Dec switch variables

int incNew;
int decNew;
int incOld = digitalRead(7);  // Read startup status of INC button
int decOld = digitalRead(8);  // Read startup status of DEC button

// Vol/Wah variables

int volNew;
int wahNew;
int volDif;
int wahDif;
int volMax = 0;
int volMin = 1023;
int wahMax = 0;
int wahMin = 1023;
float volRange = 1023.00;
float wahRange = 1023.00;
int volOld = analogRead(0);  // Read startup position of VOL pedal
int wahOld = analogRead(1);  // Read startup position of WAH pedal
int volCC;
int wahCC;

void loop()                     
{

  // Decrement Program Number Code Block

  decNew  = digitalRead(8);
  if (decNew != decOld)   // DEC button has changed - DECREMENT
  {
    decOld = decNew;
    usbMIDI.sendControlChange(decCCNumber, 127, midiChannel);   // (control, value, channel)
  }

  // Increment Program Number Code Block

  incNew  = digitalRead(7);
  if (incNew != incOld)   // INC button has changed - INCREMENT
  {
    incOld = incNew;
    usbMIDI.sendControlChange(incCCNumber, 127, midiChannel);   // (control, value, channel)
  }

  // Volume Pedal Code Block

  volNew = (analogRead(0));
  if (volNew > volMax)  // Check for new Max
  {
    volMax = volNew;
  }
  if (volNew < volMin)  // Check for new Min
  {
    volMin = volNew;
  }
  volRange = volMax - volMin;
  volDif = (volNew - volOld);
  if (abs(volDif) > 8)   // VOL pedal has changed more than x/1024ths
  {
    volOld = volNew;
    int volCC = 127 / float(volRange) * (volOld - volMin + 4);   // add 4 to insure CC127
    usbMIDI.sendControlChange(volCCNumber, volCC, midiChannel);   // (control, value, channel)
  }

  // Wah Pedal Code Block

  wahNew = (analogRead(1));
  if (wahNew > wahMax)  // Check for new Max
  {
    wahMax = wahNew;
  }
  if (wahNew < wahMin)  // Check for new Min
  {
    wahMin = wahNew;
  }
  wahRange = wahMax - wahMin;
  wahDif = (wahNew - wahOld);
  if (abs(wahDif) > 8)   // WAH pedal has changed more than x/1024ths
  {
    wahOld = wahNew;
    int wahCC = 127 / float(wahRange) * (wahOld - wahMin + 4);   // add 4 to insure CC127
    usbMIDI.sendControlChange(wahCCNumber, wahCC, midiChannel);    // (control, value, channel)
  }

  delay(25);

}


I am absolutely stumped.  And advice would be very much appreciated.  Thank you.

Coding Badly

What happens when you use a short cable with the Gefen?

Measure the power supply voltage at the Teensy.  Is it close to 5 volts?

HFTobeason

The voltage at the Teensy is 4.89 when using the long cable between the Gefen units.  When connected directly to the Mac, the voltage is 5.00.  From my reading of the Teensy documentation, even a varying voltage supplied to the Teensy should still result in consistent analog input readings.  In any event, I discovered that I can "smooth" out my funky potentiometer readings by increasing my fudge factor (the volDif and wahDif numbers).  The negative side effect is that my MIDI CC values now jump by 2s or 3s.  Frustrating.

Coding Badly

Does this help...
http://www.thebox.myzen.co.uk/Tutorial/De-coupling.html

HFTobeason

That seems to be exactly the problem!!!  Thank you so much!  It looks like it's just the low frequency component that's at issue - I'm currently testing various uF values to see what works best.  Again - thank you for your help.  It is very much appreciated.

Go Up