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):
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.