problem with analog Read

hey...
actually the thing is i turned my dc motor into a servo using a potentiometer..and that is working perfectly..but the thing is while i am using that servo in project and i am writing same angle again and again the arduino is reading the values wrong and making the motor move backward and forward...and when i checked why is it happening the arduino is reading the pot value every timedifferently

here is the case

servo function

int pot=A1;
int minang=18;
int maxang=620;

int  servo(int mval)
{
  if(angle<180&&angle>0){

val=analogRead(pot);


 cangle=map(val,minang,maxang,0,180);

while (cangle!=mval)
 {
  val=analogRead(pot);
 cangle=map(val,minang,maxang,0,180);
   if(cangle<mval)
   
   {
    Serial.print("fw");
   
    frwrd();
 
    }
    
if(cangle==mval)  {

    Serial.print("stp");
    stp();
    
  }
  if(cangle>mval)
  {
 
    Serial.print("bw");
    bckwrd();
       

    
  }
   
  }

 
}

}

when i am trying to move the servo from 0 to 180 degrees using for loop it is working awesome

but when i am writing same angle in servo in void loop lets assume of 90 degree...

void loop(){
servo(90);
}

it is not stable the motor is moving little forward and backward
and when i checked the code on serial monitor what i got was every time the while loop executes it takes the servo to 90 degree on the reading of around 320(on pot) but when the while loop executes again and reads the value of pot(which should be same where we left as 320) it reads the value around 344(angle 96) so turns backward and stops on again 320 and when it reads the value again it reads the value as around 280 so turns forward and stops on 320(angle 90).....

it is kinda giving me headache because if the value is stopped on 320 then,while reading the value again, why is it reading the different value... please help

Something to try is reading the analog point twice like below. Others have said that a more stable reading is sometimes obtained on the second reading of the analog input line.

val=analogRead(pot);
val=analogRead(pot);
cangle=map(val,minang,maxang,0,180);

Zerocoolz1:
but when i am writing same angle in servo in void loop lets assume of 90 degree...

Perhaps you need a dead-band to allow for minor errors in the analogRead() value.

Or it may be that you need to use PID to control the motor so it slows down as it nears the setpoint and can deal properly with minor over-shoots.

This link has some simple PID code that you could adapt to your system.

...R

There are several possible causes of your problem.

  • Your power source isn't stable enough.

  • You get electrical noise onto the pot signal. This is a big problem in the environment where you drive motors.

  • You don't have a stable reference voltage.

You might solve this by improving your hardware or by making the software more tolerant.

One way to make the software more tolerant is by simply using a running average of the measured pot value (for example the last 6 values averaged). This often stabilizes the setup. The other method is to move the motor until you reach the calculates target. Once you reached it you don't move the motor anymore except the difference between the target value and the measured value is greater than a configured limit.

If the values you posted (320, 344, 280) all represent the same pot position you have a bigger hardware problem though.

Make sure the pot is correctly installed with the wiper going to the analog in pin. If you try the "knob" example code, does the servo operate as expected?