The plan is to control a NEMA 17 stepper motor with an endless rotary encoder.
First, I found a code to read the rotary encoders data on www.HowToMechatronics.com by Dejan Nedelkovski. It basically manipulates an integer and outputs it on the Serial Monitor.
Works almost perfectly except that it actually doubles the count of steps the rotary encoder does.
So a clockwise turn subtracts 1, outputs it in the SM, subtracts 1 and outputs it in the SM again. Same in the other direction with adding 1. Although being not a big deal for intended use of the rotary encoder data, I think this has something to do with the problem.
In the code down below you will find a modified version of the code from Mr. Nedelkovski. I added a few lines to move the motor a few steps in each direction.
Clockwise rotation works as I expected.
One step on the rotary equals subtract, output, movement, subtract, output, movement.
But counterclockwise rotation now adds 1 to the rotary position and subtracts it immediately, leaving the motor in moving back and forward.
But if I move the rotary very very slowly it will work.
Why are the rotary values jumping around once I added the lines?
Is there a way to solve this?
The code:
/* Arduino Rotary Encoder Tutorial
*
* by Dejan Nedelkovski, www.HowToMechatronics.com
*
* to make it work with a stepper
* modified by me
*
*/
#include <Stepper.h>
#define outputA 52
#define outputB 50
int counter = 0;
int dir = 0;
int aState;
int aLastState;
Stepper myStepper(200, 47, 49, 51, 53);
void setup() {
pinMode (outputA,INPUT);
pinMode (outputB,INPUT);
Serial.begin (9600);
// Reads the initial state of the outputA
aLastState = digitalRead(outputA);
// set the speed at 60 rpm:
myStepper.setSpeed(60);
}
void loop() {
aState = digitalRead(outputA); // Reads the "current" state of the outputA
// If the previous and the current state of the outputA are different, that means a Pulse has occured
if (aState != aLastState){
// If the outputB state is different to the outputA state, that means the encoder is rotating clockwise
if (digitalRead(outputB) != aState) {
counter ++;
myStepper.step(50);
} else {
counter --;
myStepper.step(-50);
}
Serial.print("Position: ");
Serial.println(counter);
}
aLastState = aState; // Updates the previous state of the outputA with the current state
}
Thanks for taking your time to read this and help me. I hope that a detailed explanation will help solving the problem.
if (digitalRead(outputB) != aState && aState==1)