smoothing out potentiometer values.

I have a potentiometer attached to arduino and I mapped the values from 0 to 270, the problem that the values are always changing by 1. for example

180
181
181
181
180
181
180
180
181

how should i go about fixing this?

int rightShoulderX;
int rightShoulderY;
int rightElbowX;
int rightElbowY;

int rightShoulderXt;
int rightShoulderYt;
int rightElbowXt;
int rightElbowYt;



void setup() {
  // put your setup code here, to run once:

Serial.begin(9600);


}

void loop() {
  // put your main code here, to run repeatedly:

  rightShoulderXt=analogRead(0);
rightShoulderYt=analogRead(1);
rightElbowXt=analogRead(2);
rightElbowYt=analogRead(3);

rightShoulderXt=map(rightShoulderXt,0,1028,0,270);
rightShoulderYt=map(rightShoulderYt,0,1028,0,270);
rightElbowXt=map(rightElbowXt,0,1028,0,270);
rightElbowYt=map(rightElbowYt,0,1028,0,270);
delay(8);
rightShoulderX=analogRead(0);
rightShoulderY=analogRead(1);
rightElbowX=analogRead(2);
rightElbowY=analogRead(3);

rightShoulderX=map(rightShoulderX,0,1028,0,270);
rightShoulderY=map(rightShoulderY,0,1028,0,270);
rightElbowX=map(rightElbowX,0,1028,0,270);
rightElbowY=map(rightElbowY,0,1028,0,270);

if(rightShoulderX-rightShoulderXt==2||rightShoulderX-rightShoulderXt==-2);
{
  rightShoulderX=rightShoulderXt;
}
if(rightShoulderY-rightShoulderYt==2||rightShoulderY-rightShoulderYt==-2);
{
  rightShoulderY=rightShoulderYt;
}
if(rightElbowX-rightElbowXt==2||rightElbowX-rightElbowXt==-2);
{
  rightElbowX=rightElbowXt;
}
if(rightElbowY-rightElbowYt==2||rightElbowY-rightElbowYt==-2);
{
  rightElbowY=rightElbowYt;
}


Serial.print("<");
Serial.print(rightShoulderX);
Serial.print(",");
Serial.print(rightShoulderY);
Serial.print(",");
Serial.print(rightElbowX);
Serial.print(",");
Serial.print(rightElbowY);

Serial.println(">");
delay(20);

}

nvm i figured it out

You cant. Slight electrical noise on the signal will result in a different value read from the ADC. A change of just 1 is very reasonable.

Take a bunch of samples (32 or 64 is normally enough - this are nice round numbers for easy division by bit shift) and take the average.

So common, the newer AVR processors even have that option built into their hardware!

Without a schematic etc knowing how the pot is connected and the lead dress I would say you are doing great. The reference for the A/D is the output of the 5V regulator. Any load on that can can change the A/D reference voltage. Nick Gammon posted a great tutorial on the A/D it is at: Gammon Forum : Electronics : Microprocessors : ADC conversion on the Arduino (analogRead)

daudhtm:
I have a potentiometer attached to arduino and I mapped the values from 0 to 270, the problem that the values are always changing by 1. for example

180
181
181
181
180
181
180
180
181

how should i go about fixing this?

Inevitable nature of analog to digital conversion, you accept its going to always happen
and code defensively. [ google "hysteresis" perhaps? ]

Yes ... if your input value was equivalent to say 180.5, then maybe sometimes you’ll see 180 , sometimes 181 .

Also in mapping the values you have further reduced the resolution, your values only now go from 0-270 and not 0-1023, and the values don’t map across as whole numbers so you’ll get some rounding .

Not even going to 270 as the high limit of the map input is 1028 not 1023 which is the highest an analog reading can ever be…

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.