Sending MIDI CC with Potentiometer issues

OK,
So I have a 10k Linear pot hooked up correctly. But, of course I don't want to send the CC unless it has changed (the pot has been moved).
Here is my lame attempt a doing that.

void setup()
{                
  Serial.begin(31250); 
}
int CCTH=0;
int OLDCC=1;

void loop() {
 // read the pot position
  CCTH = analogRead(0) / 8;
 if (CCTH != OLDCC)
 {

   Serial.println (CCTH);
 }
OLDCC=CCTH;
}

It works but it seems a little clunky. Any better way to do it???
Also, every once in a while when I stop turning the pot it will kind of be stuck sending, say 100 then 101 over and over until I tweak the pot a bit. Any ideas on what that is????
Thanks,
Todd

I have the same problem as yours, although you may replace the last line into the if statement ~

  for(int loope = 0;loope < 4; loope++){
    
 readadc[loope] = analogRead(loope)/8; //reads value from pot
 
 if(readadc[loope] != readadctemp[loope]){
 MIDI_TX(176,loope,readadc[loope]); //sends cc
 readadctemp[loope] = readadc[loope]; //old value
}

So it won't update the old value after you changed the pot, it might also speed up your code abit.
Although I think this problem exist since the ADC cant decide on what value it wants to be, for as example
2.5v = 511
2.51v = 512
due 10 bit resolution?
(Im using more inputs using a for loop for that, you can leave that away if you only using one.)

Also, not trying to hijacking your topic... but do you experience glitches when sending cc or notes to your midi device?
It appears at mines that theres alot of garbage sended along with the CC messages, such as "System reset" (FF) random notes. or random hex stuff ~.
This by using MIDI OX on a windows PC and a usb midi cable. (brandless) The cable works fine on normal midi devices, except the arduino.

(Sorry if my english grammar is crappy ^_)

Thanks.
Dnstje.

Thanks for responding. :slight_smile:
No, I don't seem to have any MIDI junk or other problems. I am also using MIDI OX to test.

every once in a while when I stop turning the pot it will kind of be stuck sending, say 100 then 101 over and over until I tweak the pot a bit. Any ideas on what that is????

Yes it is the normal one bit uncertainty of any A/D converter. The input voltage is in such a state that the small amount of noise is enough to trigger a change in one in the reading. All A/D's do this and there is nothing you can do to stop it.

What you have to do is to take the difference between your current reading and the last reading and if this is greater than 2 (or more) send your MIDI. Use the ABS() function to ensure you account for positive and negative differences. There is no need to average a lot of readings.

 if (CCTH != OLDCC)
 {

   Serial.println (CCTH);
   OLDCC=CCTH;
 }
// OLDCC=CCTH;

Thank you Grumpy Mike. I figured it was something like that. :slight_smile:

Thanks Vanyamba- I fixed that part already. :slight_smile:

PROBLEM SOLVED

This is what I came up with. It uses pin 0.

////////// Knob function:  /////////////
void setup()
{                
  //Serial.begin(31250); 
}

int THRESHOLD=2;
int maxPin;
int knobFunction(const int pin, int divisor)
{
  static int prevRead[6] = {0, 0, 0, 0, 0, 0};   
  int output = analogRead(pin)/4;	   
  if (abs(output - prevRead[0])  >= THRESHOLD)
    {prevRead[pin] = output;}
  else
    {output = prevRead[pin];}
  return output/2;
}

///// in void LOOP: ///////////////
void loop () {
  static int lastmaxVal = 0;
  int maxVal = knobFunction(0, 4);
  if(maxVal != lastmaxVal)
  {
    Serial.println(maxVal);
    lastmaxVal = maxVal;
  }
}

There is some unnecessary stuff in there but you can get the idea. :slight_smile:
Thanks to all and nym on the Reaper forums.