I am suppose to control the position of a lead screw Stepper Motor using absolute Encoder EMS 22A.
To move the Lead Screw, required multiple rotations of the Encoder. How will i program this?
What i did was to determine the "Largest Change" to count the number of cycle that has occurred. However, with fast rotation the program was not able to detect the change.
void loop(){
unsigned long currentMillis = millis();
pos = encoder.ReadAngle();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
pos2 = encoder.ReadAngle();
}
int change = pos2-pos;
if (change > 200){
cycle--;
}
if (change < -200){
cycle++;
}
if(change > 1){
motor.Forward();
}
if(change < -1){
motor.Reverse();
}
if(change <= 1 && change >= -1){
motor.Stop();
}
Serial.print(pos);
Serial.print(" ");
Serial.println(cycle);
}