Different MIDI data from pot

I'm using 10k pot to send MIDI data. The issue that I'm having is that when I try to map that pot in my DJ software (Traktor Pro), it reads different values like:

Ch02.CC.032
Ch02.CC.021
Ch02.CC.000

Why is that? I've tried using 2 codes, first one I found on instructables (with some edits) and 2nd is mine. Same issues are appearing, with code 2 giving even more values:

Code 1:

//#define DEBUG

#define FILTER_AMOUNT 7

// Timeout is in microseconds
#define ANALOGUE_INPUT_CHANGE_TIMEOUT 250000


// Contains the current value of the analogue inputs.
int analogueInputs[6];

// Variable to hold temporary analogue values, used for analogue filtering logic.
int tempAnalogueInput;

// for loop
int i = 0;

// Variable to hold difference between current and new analogue input values.
int analogueDiff = 0;

// This is used as a flag to indicate that an analogue input is changing.
boolean analogueInputChanging[6];

// Time the analogue input was last moved
unsigned long analogueInputTimer[6];


void setup()
{
  
Serial.begin(31250);

  

  
  // Initialise each analogue input channel.
  for (i = 0; i < 1; i++)
  {
    // Set the pin direction to input.
    pinMode(i, INPUT);
    
    // Initialise the analogue value with a read to the input pin.
    analogueInputs[i] = analogRead(i);
    
    #ifdef DEBUG
    Serial.print("read in setup: ");
    Serial.println(analogueInputs[i]);
    #endif
    
    // Assume no analogue inputs are active
    analogueInputChanging[i] = false;
    analogueInputTimer[i] = 0;
  }
  
}


void loop()
{
  
  
  /*
   * Analogue input logic:
   * The Arduino uses a 10-bit (0-1023) analogue to digital converter (ADC) on each of its analogue inputs.
   * The ADC isn't very high resolution, so if a pot is in a position such that the output voltage is 'between'
   * what it can detect (say 2.505V or about 512.5 on a scale of 0-1023) then the value read will constantly
   * fluctuate between two integers (in this case 512 and 513).
   *
   * If we're simply looking for a change in the analogue input value like in the digital case above, then
   * there will be cases where the value is always changing, even though the physical input isn't being moved.
   * This will in turn send out a constant stream of MIDI messages to the connected software which may be problematic.
   *
   * To combat this, we require that the analogue input value must change by a certain threshold amount before
   * we register that it is actually changing. This is good in avoiding a constantly fluctuating value, but has
   * the negative effect of a reduced input resolution. For example if the threshold amount was 2 and we slowly moved
   * a slider through it's full range, we would only detect every second value as a change, in effect reducing the
   * already small 7-bit MIDI value to a 6-bit MIDI value.
   *
   * To get around this problem but still use the threshold logic, a timer is used. Initially the analogue input
   * must exceed the threshold to be detected as an input. Once this occurs, we then read every value coming from the
   * analogue input (not just those exceeding a threshold) giving us full 7-bit resolution. At the same time the
   * timer is started. This timer is used to keep track of whether an input hasn't been moved for a certain time
   * period. If it has been moved, the timer is restarted. If no movement occurs the timer is just left to run. When
   * the timer expires the analogue input is assumed to be no longer moving. Subsequent movements must exceed the
   * threshold amount.
   */
  
  for (i = 0; i < 1; i++)
  {
    // Read the analogue input pin, dividing it by 8 so the 10-bit ADC value (0-1023) is converted to a 7-bit MIDI value (0-127).
    tempAnalogueInput = analogRead(i);
    
    #ifdef DEBUG
    Serial.print("read in loop: ");
    Serial.println(tempAnalogueInput);
    #endif
    
    
    
   
    // Take the absolute value of the difference between the curent and new values 
    analogueDiff = abs(tempAnalogueInput - analogueInputs[i]);
    #ifdef DEBUG
    Serial.print("difference: ");
    Serial.println(analogueDiff);
    Serial.print("\n");
    delay(2000);
    #endif
    
    
    // Only continue if the threshold was exceeded, or the input was already changing
    
    if ((analogueDiff > 0 && analogueInputChanging[i] == true) || analogueDiff >= FILTER_AMOUNT)
    {
      // Only restart the timer if we're sure the input isn't 'between' a value
      // ie. It's moved more than FILTER_AMOUNT
      
      if (analogueInputChanging[i] == false || analogueDiff >= FILTER_AMOUNT)
      {
        // Reset the last time the input was moved
        analogueInputTimer[i] = micros();
        
        // The analogue input is moving
        analogueInputChanging[i] = true;
      }
      
      else if (micros() - analogueInputTimer[i] > ANALOGUE_INPUT_CHANGE_TIMEOUT)
      {
        analogueInputChanging[i] = false;
      }
      
      
      
      
      
      // Only send data if we know the analogue input is moving
      if (analogueInputChanging[i] == true)
      {
        // Record the new analogue value
        analogueInputs[i] = tempAnalogueInput;
        #ifdef DEBUG
        Serial.print("Value to send: ");
        Serial.println(analogueInputs[i]/8);
        #endif
        
      
        // Send the analogue value out on the general MIDI CC (see definitions at beginning of this file)
        controlChange(177, 21+i, analogueInputs[i]/8);
        
      }
    }
    
  }
  
}





// Send a MIDI control change message
void controlChange(int channel, int control, int value)
{
  

      Serial.print(channel, BYTE);
      Serial.print(control, BYTE);
      Serial.print(value, BYTE);

}

Code 2:

//current reading from pot
int sendval[2] = {0, 0};

//previous reading from pot
int last[2] = {0, 0};

//difference between current and previous reading
int diff[2] = {0, 0};

//for loop variable
int i = 0;

void setup() {

  Serial.begin(31250); 
}


void loop() { 
  
  
for (i=0; i<1; i++) {
  sendval[i]=analogRead(i); 
  
  
  diff[i]=abs(sendval[i]-last[i]);
  



  if ( (sendval[i]/8 != last[i]/8 ) && (diff[i]>7) )


  {

    //Serial.println(sendval[i]/8, DEC);
    midiCC(177, 21, sendval[i]/8);

    // update last variable
    last[i] = sendval[i];

  }
 } }



// this function sends a Midi CC. 
void midiCC(char CC_data, char c_num, char c_val){
  Serial.print(CC_data, BYTE);
  Serial.print(c_num, BYTE);
  Serial.print(c_val, BYTE);

}

Oh and for the record, I'm using this scheme also: shiftmore: Quick and Dirty Arduino Midi Over USB

Any help is appreciated.

it reads different values like

What do you mean by this? The values look fine to me what are you expecting to see?

It should be constant, ie. when I map pot to selected function (we are talking about ONE pot whole time by the way) in Traktor it should read it as a specific command. The command changes in my case which is making it unusable.

It's like you want to map key on your keyboard to function "A" and your software tries to map it to functions "A" and "B".

Funny thing is, If I set the baud rate to 9600 and use Serial-to-MIDI converter it works just fine. But with the setup I'm using it doesn't.

we are talking about ONE pot whole time by the way

But the code is reading two analogue inputs if there is nothing connected to one of them then you have a floating input and you will get random numbers.

You're right, but that was the issue only in 2nd code. I fixed it and I'm getting same results again.

You see people helping here are rather like compilers, they stop at the first error they find. That is why it is important to post the code you are having the problem with and not a version you have fixed some time ago.

If you are 100% sure that at the lower baud rate it is working fine then the problem must be in the software that is receiving the code. However I suspect that that it is not working fine at the lower baud rate, can you post the code you have and what the output is at the lower rate.

I'm not quite sure what do you mean.

If I set the baud rate to 9600 and run application called Serial2Midi, my DJ software can map the pot just fine (it reads what it should read: Ch02.CC.21) with Ch02 being first number my MIDI function is sending (177), CC being the continous controller nr. 21 and last number which my function is sending is the read value (0-127). The board is connected to PC via USB directly so I have to use that Serial2Midi converter.

However, if I use the setup which is in my first post (I've bought this from ebay http://goo.gl/9oO9u , removed the MIDI cables from it and connected it to TX pin on the board) I have to set the baud rate to 31250 (otherwise it doesn't really work) and then those problems occur (DJ software reads multiple commands).

I have to say that I'm also using 2 rotary encoders connected using the same sheme (that converter from ebay), they can spin much faster compared to speed of rotating the pot, also running on 31250 rate and they work perfectly well.

Here is a picture of setup:
http://dl.dropbox.com/u/2777613/23062011041.jpg

removed the MIDI cables from it and connected it to TX pin on the board)

You can't do this it will not work. MIDI is a current loop interface and the TX pin is a TTL voltage interface. You need some hardware to convert the two, something like his:-
http://www.thebox.myzen.co.uk/Hardware/MIDI_Shield.html

Grumpy_Mike:

removed the MIDI cables from it and connected it to TX pin on the board)

You can't do this it will not work. MIDI is a current loop interface and the TX pin is a TTL voltage interface. You need some hardware to convert the two, something like his:-
http://www.thebox.myzen.co.uk/Hardware/MIDI_Shield.html

That's strange because it DOES work (with rotary encoders).

This is the scheme: http://2.bp.blogspot.com/_KLJp9VskMzs/S0tTnt2GlbI/AAAAAAAAAJg/LSEawVLNDSg/s320/cheapo_midi-usb.png