#include <Stepper.h>
#define ENCODER_CLK 2
#define ENCODER_DT 3
const int stepsPerRevolution = 2500; // change this to fit the number of steps per revolution
// for your motor
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
void setup() {
pinMode(ENCODER_CLK, INPUT);
pinMode(ENCODER_DT, INPUT);
// set the speed at 60 rpm:
myStepper.setSpeed(120);
// initialize the serial port:
Serial.begin(9600);
}
int lastClk = HIGH;
void loop()
{
int newClk = digitalRead(ENCODER_CLK);
if (newClk != lastClk) {
// There was a change on the CLK pin
lastClk = newClk;
int dtValue = digitalRead(ENCODER_DT);
if (newClk == LOW && dtValue == HIGH) {
Serial.println("Rotated clockwise ⏩");
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
//delay(500);
}
if (newClk == LOW && dtValue == LOW) {
Serial.println("Rotated counterclockwise ⏪");
Serial.println("counterclockwise");
myStepper.step(-stepsPerRevolution);
//delay(500);
}
}
}