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.