Can someone point me in the right direction.
I am able to read the encode and move the motor base on the encoder. The problem I have is I can't figure out how to move the motor at the same speed as the encoder.
Right now I have to hard code the speed at 500rpm: stepper.setSpeed(500);
but I want the speed of the motor to be dynamic (base on how fast the encoder moves).
#include <Stepper.h>
volatile boolean fired;
volatile boolean up;
// Encoder sensor pins
#define PINA 2
#define PINB 3
#define INTERRUPT 0 // that is, pin 2
// Motor pins
const int dirPin = 8;
const int stepperPin = 7;
// the number of steps on your motor
#define STEPS 200
Stepper stepper(STEPS, 7, 8);
void interruptServiceRoutine (){
pinMode(dirPin, OUTPUT);
pinMode(stepperPin, OUTPUT);
if (digitalRead(PINA)){
up = digitalRead (PINB);
}else{
up = !digitalRead (PINB);
}
fired = true;
}
void setup (){
digitalWrite (PINA, HIGH);
digitalWrite (PINB, HIGH);
attachInterrupt (INTERRUPT, interruptServiceRoutine, CHANGE);
Serial.begin (115200);
}
void loop (){
stepper.setSpeed(500);
if (fired){
if (up){
stepper.step(-1);
}else{
stepper.step(1);
}
fired = false;
}
}