Stepper motor, jumping...

i am trying to get a stepper motor to use the value of a pot to move forward and backwards. i have the motor working, i just can't figure out what is wrong with my code.

i want the motor to move forward x steps and stop, where x is the value of the pot. then when the pot value goes down move x backward and stop. here is what i have so far.

/*
  Motor knob using Adafruit MotorShield
  
  Reads the analog input of a pot and divdes it by 10 to a range between
  0 to 101. The motor use the value to turn the stepper forward or backward.
  
  The circuit:
  * pot is connected to analog pin 0.
    Center pin to A0, side pins to +5V and GND.
  * Stepper is connected to port 1 of the shield.
  
  created 22 Dec. 2010
  by Brandon Honeycutt
  
  This example code is public domain.
  
*/

#include <AFMotor.h>

AF_Stepper motor(200, 1);

// the previous value from analog input
int current = 0;
int diff = 0;

void setup() {
  // initialize serial comms
  Serial.begin(9600);
  
  // set motor speed in rpm
  motor.setSpeed(30);
}

void loop() {
  // get the sensor value
  int val = analogRead(0)/10;
  diff = abs(current - val);
  
  //move forward or backward
  if(diff > current){
    motor.step(diff, FORWARD, SINGLE);
    //motor.release();
  }
  
  else if (diff < current){
    motor.step(diff, BACKWARD, SINGLE);
    //motor.release();
  }
  
  // set previous value for next turn
  current = diff;
  
  // print value of the pot to serial
  Serial.print("pot = ");
  Serial.print(val);
  Serial.print("\n");
  
  // delay 10 millisecs
  delay(10);
  
}

thanks in advance.

the motor works fine, i have been using it for other projects. the value seems to stay constant and is not jumping. i did notice that if i send the value of diff to serial output it is jumpy. don't know why though, is it something in my code?

  // set previous value for next turn
  current = diff;

Shouldn't this be current = val;?

If the first reading (val) was 38, and current was 0, then diff was 38, and current gets set to 38. So far, so good.

On the next pass through loop, suppose val is still 38. current is 38, so diff is 0. Then, you set current to 0.

On the next pass, suppose val is STILL 38. current is 0, so diff is 38, and off the motor goes.

God Member has an All Seeing Eye

tried adding a cap to the pot and still the same thing. i did change current = val also.

i am looking to use a pot to turn a stepper motor x forward and x-y backward. so if the pot reads 38 then the motor steps 38 forward. if the pot is turned down and reads 18, the the motor steps backwards 20.

with my code the motor will step forward but not backwards and i don't see the mistake.

found my problem. is was in my conditionals, i was supposed to be comparing val with current not diff.

if (val > current)

not

if (diff > current)

thanks for the help.