Jump to content
43oh

MSP430 Analog read - potentiometer value floating


Recommended Posts

Thanks Rob, but i am using this simple code:

 

    unsigned int sensor4Valor;
     
    void setup() {
      Serial.begin(9600);
    }
     
    void loop() {
      sensor4Valor = analogRead(A7);
     
      Serial.print("Pot 1 value = ");
      Serial.print(sensor4Valor);
      Serial.println("  |  ");
     
      delay(1);
    }
 

 

Even removing the potentiometer and using a wire between P1.7 and Vcc the value floats from 1022 to 1023.

Link to post
Share on other sites

If you really want to (relatively) stabilize your readings you need to switch your ADC reference source away from the general supply.  Keep in mind your VCC will itself be waivering according to load. The ADC itself is also consuming power and changing the reference voltage itself.  That said, I don't really see a problem with a single least significant bit...keep in mind the actual value falls between the 2 numbers and is being rounded + multiple sources of noise.

 

Keep in mind also that your potentiometer is very, very likely to be more inaccurate from true than the ADC.  Many pots are accurate to 5% or worse and deviate from linearity as standard. 

Link to post
Share on other sites
Then all you need to do is divide ADCMEM10 by 8 and you will get 0-127 and pretty much stable value. 
value = ADCMEM10 >> 3;

 

You can also implement some sort of hysteresis that will take care of situations like 0x0F3 and 0x0F4, and stabilize your value even more.

 

For example, you can have 16 steps window. When your pot is in the middle, ADCMEM10 value is 512. Any changes from 504 to 520 will not change your actual value.

As soon as you change to 503 for example, you move center of your window to 503 and change your actual value. Then your new window will be 495 to 511, and so on.

You will loose some precision, but gain stability. You can try making the window smaller and see how it works. 

 

//untested, 4 steps window

adc = ADCMEM10;

if(adc > adcHigh || adc < adcLow) {
adcHigh = adc + 2;
adcLow = adc - 2;
midiValue = adc >>3;
} 

 

 

 

Link to post
Share on other sites
    unsigned int sensor4Valor;
     
    void setup() {
      Serial.begin(9600);
    }
     
    void loop() {
      sensor4Valor = analogRead(A7) >> 3;
     
      Serial.print("Pot 1 value = ");
      Serial.print(sensor4Valor);
      Serial.println("  |  ");
     
      delay(1);
    }

or

 

    unsigned int sensor4Valor = 0;
    unsigned int adc = 0;
    unsigned int adcMin = 0;
    unsigned int adcMax = 0;
     
    void setup() {
      Serial.begin(9600);
    }
     
    void loop() {
      adc = analogRead(A7);
      if( adc > adcHigh || adc < adcLow) {
           adcHigh = adc + 2;
           adcLow = adc - 2;
           sensor4Valor = adc >> 3; // divide 10 bit to get 7bit, MIDI uses 0-127
       }
      Serial.print("Pot 1 value = ");
      Serial.print(sensor4Valor);
      Serial.println("  |  ");
     
      delay(1);
    }

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...