stepper motor control

I’m trying to control my stepper motor using a pot so that when i turn the pot clockwise my motor steps clockwise. if i don’t turn the pot the stepper motor doesn’t move and if i turn the potentiometer counter clockwise the the motor should step counter clockwise. here is my code.

int motorPin1 = 8;
int motorPin2 = 9;
int motorPin3 = 10;
int motorPin4 = 11;
int delayTime = 250;
int val = 0;
int count;

void setup() {
Serial.begin(19200);
Serial.println(“I’m online dude!”);
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
}

void loop() {
val = analogRead(0);
Serial.println(val);

if ((val > (count+5)) && (val < 256))
{
moveForward();
count = val;
}
else if(val < (count-5) && (val <=256))
{
moveBackward();
count = val;
}
else {
Dontmove();
}
}

void moveForward() {

digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
delay(delayTime);
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
delay(delayTime);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
delay(delayTime);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
delay(delayTime);
return;
}

void moveBackward() {

digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
delay(delayTime);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
delay(delayTime);
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
delay(delayTime);
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
delay(delayTime);
return ;
}

void Dontmove() {

digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(delayTime);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(delayTime);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(delayTime);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(delayTime);
}

oh i added + 5 to count because even without touching the pot it varies back and forth by a few bit so it throws the motor off making it move back and forward. My problem is if it turn the POT fast clockwise or counter clockwise the motor doesn’t move at all. PLEASE HELP i just want it to move up to 90 degrees :frowning:

I don't think this will affect your code, but you have read the value of "count" before you have assigned it a value, or initialised it.

"I'm trying to control my stepper motor using a pot so that when i turn the pot clockwise my motor steps clockwise. if i don't turn the pot the stepper motor doesn't move and if i turn the potentiometer counter clockwise the the motor should step counter clockwise"

Isn't this what the tutorial Here does? http://www.arduino.cc/en/Tutorial/StepperUnipolar

At the end of your post you said that all you want to do is move it 90degrees... If this is the case would it be better to use a servo? From what little I've read on steppers, it is possible for pulses to get missed and without a feedback loop of an encoder you may or may not make it to your desired 90 degree rotation.

Warning, this is the blind leading the blind... I just got my hands on my first stepper so I'm learning what you are learning too.

Good luck!

Are you trying to drive the motor directly from the Arduino's pins? It's very unlikely that they can supply enough current to run it.

Ran

Agreed. You should drive the stepper pins with a seperate power source through some darlington transistors or a driver (IC with multiple transistors) like in the playground article. You need one transistor on each pin.

Im using a 5 volt stepper motor , ive tried the code form the arduino stepper motor tutorial and it runs perfectly. The problem im having is in the code. i only want the stepper motor to move in discretely. i.e if i move the pot clockwise a few degrees, the Motor should also move closckwise a few deg. if i move it counter clockwise a few dew the motor should also do the same. :(