Midi Overshoot???

Status
Not open for further replies.
Okay, I'm having a strange problem, presumably in my code, as it's happened with different teensies connected to different pots.

When I move the pot all the way down, I get a non-zero value. If I then move it slightly in the opposite direction, I get a proper zero. Then if I move to max, I get a value of around 109. If I move slightly back from max, I get a 127.

Here's my code:

Code:
/* Pedal Interface.  10 footswitches and 4 control pedals

 */

#include <Bounce.h>

const int COUNT_D = 10;
const int COUNT_A = 3;

const int PINS_D[] = {0,1,2,3,4,5,6,7,8,9};
const int CCS_D[] = {65,66,67,68,69,70,71,72,73,74};

const int sus = 0; 
const int vol = 1;
const int expr = 2;
const int aux=3;

const int PINS_A[] = {sus,vol,expr,aux,};
const int CCS_A[] = { 64,11,12,13};
int VALUES_A[COUNT_A];

int midiChan = 3;
// range of ctls is 0-1023, divide by 8 for midi value
const int SENSITIVITY = 64;
const int SCALE_A = 8;

//ccVal = map(sensorValue, 0, 1023, 0, 127);  //more precise, I suppose

int sens = 4;

//button debounce time
// Debounce
long debounceDelay = 20;
 
Bounce buttons[] = {
  Bounce(PINS_D[0], debounceDelay),
  Bounce(PINS_D[1], debounceDelay),
  Bounce(PINS_D[2], debounceDelay),
  Bounce(PINS_D[3], debounceDelay),
  Bounce(PINS_D[4], debounceDelay),
  Bounce(PINS_D[5], debounceDelay),
  Bounce(PINS_D[6], debounceDelay),
  Bounce(PINS_D[7], debounceDelay),
  Bounce(PINS_D[8], debounceDelay),
  Bounce(PINS_D[9], debounceDelay),
  Bounce(PINS_D[10], debounceDelay),
  Bounce(PINS_D[11], debounceDelay),
  Bounce(PINS_D[12], debounceDelay)
};

void setup() {
  for (int ctl = 0; ctl < COUNT_A; ctl++)
  {
    VALUES_A[ctl] = 0;
  }  
  for (int btn = 0; btn < COUNT_D; btn++) //int btn = 0;
  {
    pinMode (btn, INPUT_PULLUP);
  }  
}

void loop() {
  //BUTTONS
  for (int btn = 0; btn < COUNT_D; btn++)  //int btn = 0;
  {
    buttons[btn].update();
    if (buttons[btn].risingEdge())
    { 
         usbMIDI.sendControlChange(CCS_D[btn], 0, midiChan);
    }
    else if (buttons[btn].fallingEdge()) {        
        usbMIDI.sendControlChange(CCS_D[btn], 127, midiChan);
    }
  }
  //POTS
  for (int ctl = 0; ctl < COUNT_A; ctl++)  //int ctl = 0;
  {
    int value = analogRead(PINS_A[ctl]);
    if (0 < ctl < COUNT_A) {sens = SENSITIVITY;} else {sens = SENSITIVITY;} //default sensitivity
    if (abs(VALUES_A[ctl] - value) > sens)
    {
      usbMIDI.sendControlChange(CCS_A[ctl], VALUES_A[ctl] / SCALE_A, midiChan);  //CC,val,CH
      VALUES_A[ctl] = value;
      delay(50);
    }
  }
  //pin test 
  /*
  int pin = bCtl;
  int value = analogRead(PINS_A[pin]);
  if (abs(VALUES_A[pin] - value) > sens)  {
    usbMIDI.sendControlChange(CCS_A[pin],VALUES_A[pin] / SCALE_A, midiChan);
    VALUES_A[bCtl] = value;
    delay(50);
  }*/
  
  // MIDI Controllers should discard incoming MIDI messages.
  // http://forum.pjrc.com/threads/24179-Teensy-3-Ableton-Analog-CC-causes-midi-crash
  while (usbMIDI.read()) {
    // ignore incoming messages
  }
}

Thanks in advance!!!
 
Two things. This code
Code:
    if (0 < ctl < COUNT_A) {
      sens = SENSITIVITY; //default sensitivity
    } else {
      sens = SENSITIVITY;
    }
Has a problem because although the condition (0 < ctl < COUNT_A) is valid C, it is not doing what you probably expect. It should be written ((0 < ctl) && (ctl < COUNT_A)). But once that is fixed, it will set "sens" to SENSITIVITY whether the condition is true or false. So why not just set sens = SENSITIVITY.

The other problem is in this statement and is probably what is causing the problem you observe:
Code:
    usbMIDI.sendControlChange(CCS_A[pin],VALUES_A[pin] / SCALE_A, midiChan);
If the new 'value' from the ADC is significant, you send the old value as a CC message. I think this should be:
Code:
    usbMIDI.sendControlChange(CCS_A[pin],value / SCALE_A, midiChan);

Pete
 
Wow, is this ever a responsive forum, yikes!!! Thanks so much!!

Yeah, #1 is left over from code for the other teensy, which actually does have two different sensitivities.... And I'll fix the ternary issue in the other code, where it actually matters.... Good to be reminded of the proper syntax for that in C--it's been quite a while....

But I see the problem with #2. I think that would explain it quite well, I'll go fix that.

Thanks again!
 
Status
Not open for further replies.
Back
Top