# Changing Stepper Direction with Encoder

I have an absolute encoder attached to my stepper motor. When the encoder value is between >=0 and <10000, I want the stepper to rotate in one direction. When the econder value is between -10000 and <0, I want the stepper to rotate in the opposite direction. So far, it only rotates in one direction and never changes direction. Any advice as to why this is happening would be appreciated.

#include<Stepper.h>

//Stepper
const int StepperStepPin = 10;
const int StepperDirectionPin = 9;
const int StepperMaxRPM = 100;
Stepper stepper(800, StepperStepPin, StepperDirectionPin);

//Encoder
int val;
int encoder0PinA = 6;
int encoder0PinB = 7;
int encoder0Pos = 0;
int encoder0PinALast = LOW;
int n = LOW;

void setup() {

stepper.setSpeed(StepperMaxRPM);

pinMode (encoder0PinA,INPUT);
pinMode (encoder0PinB,INPUT);
Serial.begin (9600);

}

void loop() {

if ((encoder0PinALast == LOW) && (n == HIGH)) {
encoder0Pos--;
} else {
encoder0Pos++;
}
}
encoder0PinALast = n;

while((encoder0Pos >=0)&&(encoder0Pos <10000)){
stepper.step(1);
}

while((encoder0Pos >-10000)&&(encoder0Pos <0)){
stepper.step(-1);
}

}

How is the stepper, rotating the encoder in the positive direction, going to result in the value getting from 10000 to -10000?

I see what you are saying; so should the void loop be made to look something like below?

while((encoder0Pos >=0)&&(encoder0Pos <10000)){
stepper.step(1);
}

while(encoder0Pos >=10000){
stepper.step(-1);
}

while((encoder0Pos >-10000)&&(encoder0Pos <0)){
stepper.step(-1);
}

while(encoder0Pos <=-10000){
stepper.step(1);
}

Currently that alteration doesn’t change anything.

He means where, when, how does encoder0Pos change value in the while( ) loops?

You have while loops inside loop() that need to be if-statements, so that you can read the encoder continually.

wickedhurricane:
I have an absolute encoder attached to my stepper motor. When the encoder value is between >=0 and <10000, I want the stepper to rotate in one direction. When the econder value is between -10000 and <0, I want the stepper to rotate in the opposite direction.

Maybe I’m missing something, but how is that possible?
If the motor is rotating the encoder won’t it just settle at the interchange between the two options (for example at zero) because any single step will then cause it to change direction.

And if that is not the problem I suspect the code is providing positive feedback so that it keeps going forever.

…R

Does anyone know where I can view sketches where servo shaft position is controlled in similiar fashion by a quadrature encoder?

wickedhurricane: Does anyone know where I can view sketches where servo shaft position is controlled in similiar fashion by a quadrature encoder?

I have no suggestions.

According to your title it is a stepper motor you are using, not a servo? Which is it? Post a link to the motor datasheet so there is no confusion.

Maybe you can tell us what you are trying to achieve and why you need an encoder on a stepper motor? Can't you just count the steps?

...R

These are infinite loops:

while((encoder0Pos >=0)&&(encoder0Pos <10000)){
stepper.step(1);
}

while((encoder0Pos >-10000)&&(encoder0Pos <0)){
stepper.step(-1);
}

encoder0Pos never gets re-measured or recalculated, so once a ‘while’ statement starts executing, it will execute forever. I don’t think that is what was intended.