Arduino midi data stream confusing ableton

Hey guys im having a problem with my code, im building a midi controller ,i have 20 buttons and 4 pots, i recently coded the pots but there is a constant stream of midi data which is confusing ableton live.
It is basically changing my midi mapping . is there any way i can only send data to ableton if i physically turn the pot?
Any information or alterations to the sketch (which is included) would be greatly appreciated
many thanks
Gary

v2sketch_NesArduino.pde (6.24 KB)

void loop() {
   // put your main code here, to run repeatedly:
   sensor0Value = analogRead(sensor0Pin);

...   
   controlChange(0xB0, sensor0Value, 0x07); // VOLUME

...
}

You basically want to check for a change. For example:

if (sensor0Value != oldSensor0Value)
   controlChange(0xB0, sensor0Value, 0x07); // VOLUME
oldSensor0Value = sensor0Value;  // remember for next time through

Ditto for the others. Of course, you need to set up the oldSensor0Value variable.

is there any way i can only send data to ableton if i physically turn the pot?

The potentiometer is connected all the time. You read it all the time. There is no way of determining whether the potentiometer is being moved. You can, though, keep track of the last reading that resulted in a message send, and only send another message if the reading changed significantly. (You get to define what significantly means; don't make it too small, though.)

I would agree with PaulS here. Instead of what I showed, add in a "fudge" amount, so you don't send very minor changes.

Something like:

if (abs (sensor0Value - oldSensor0Value) > 5)
   controlChange(0xB0, sensor0Value, 0x07); // VOLUME

That only sends changes which are more, or less, than 5 from the previous value.

thanks very much guys where hod how will i implement this into the code? i aint the best at coding :frowning:

also im using an arduino mega i hear these have problems with live

can some one tell me where im going wrong what do i need to declare in each scope ?

v3sketch_NesArduino.pde (6.65 KB)

int switchAState, switchBState, switchCState, switchDState = LOW;

Only one of the 4 variables in this line is initialed to LOW. The rest get default values (which just happen to be the same value as LOW).

Some arrays would be real useful...

In readSwitch(), you pass in the previous state, read the new state, and do something if the current state is not the same as the previous state.

You need to do the same with controlChange. Don't read the values in loop(). Read them in controlChange(), instead. Compare the new reading with the old reading, and do something if the absolute value (abs()) of the difference is large enough.

im still not really getting it :frowning: .. fancy doing it for me lol

Change the function to:

int controlChange(byte channel, byte pin, byte prevVal, byte controller)
{
  int currVal = analogRead(pin);

  if(abs(currVal - prevVal) > 5)
  {
    Serial.print(channel, BYTE); // MIDI control change; channel
    Serial.print(controller, BYTE); // MIDI controller
    Serial.print(val, BYTE); // MIDI controller value from potentiometer
    delay(5);
  }
  return currVal;
}

Change the calls to:

  sonsor0Value =  controlChange(0xB0, sensor0Pin, sensor0Value, 0x07); // VOLUME

It really isn't magic. It's simple logic.

v2sketch_NesArduino.cpp: In function 'int controlChange(byte, byte, byte, byte)':
v2sketch_NesArduino:199: error: 'val' was not declared in this scope

Change val to currVal.

sweet man thats sorted it, but the pots are all over the place in ableton i cant seem to get them to start at 0 and go up the full range of the pot, any idea why
sorry to be a pain in the neck