Hi Guys,
I need a help, to use rotary encoder for stepper motor feedback.
Actually i want to run the stepper motor according the degree of the rotary encoder.
So, I run the stepper motor, and until the encoder reach (n degree), i mean the destination, and then the stepper motor stop running.
First step i try to run the stepper motor and read the degree with the encoder. (But the encoder doesn't give the feedback yet.)
This is my code:
#include <AccelStepper.h>
// Define a stepper and the pins it will use
AccelStepper stepper1(AccelStepper::DRIVER, 4, 5);
const int encoderPinA = 2;
const int encoderPinB = 3;
int angle = 0;
int val;
int encoderPos = 0;
boolean encoderALast = LOW; // remembers the previous pin state
void setup()
{
Serial.begin (9600);
pinMode(encoderPinA, INPUT);
digitalWrite(encoderPinA, HIGH);
pinMode(encoderPinB, INPUT);
digitalWrite(encoderPinB, HIGH);
attachInterrupt(0, encoder, CHANGE);
}
void loop()
{
axis1_1();
}
void encoder()
{
boolean encoderA = digitalRead(encoderPinA);
if ((encoderALast == HIGH) && (encoderA == LOW))
{
if (digitalRead(encoderPinB) == LOW)
{
encoderPos--;
}
else
{
encoderPos++;
}
if (encoderPos>360)
{
encoderPos=0;
}
else if(encoderPos<-360)
{
encoderPos=0;
}
angle=encoderPos;
Serial.println (angle);
}
encoderALast = encoderA;
}
void axis1_1()
{
stepper1.setMaxSpeed(200000);
stepper1.setAcceleration(10000);
stepper1.runToNewPosition(10000);
}
Note: i'm using Arduino Duemilanove, Stepper Motor NEMA23 with driver and Autonic rotary encoder with 360PPR.
Please help me Guys.