// Stepper motor on Wokwi!
#include <Stepper.h>
#define ENCODER_CLK 2
#define ENCODER_DT 3
const int stepsPerRevolution = 50; // change this to fit the number of steps per revolution
int lastClk = HIGH;
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
void setup() {
Serial.begin(115200);
pinMode(ENCODER_CLK, INPUT);
pinMode(ENCODER_DT, INPUT);
// set the speed at 60 rpm:
myStepper.setSpeed(60);
}
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 ⏩");
myStepper.step(stepsPerRevolution);
}
if (newClk == LOW && dtValue == LOW) {
Serial.println("Rotated counterclockwise ⏪");
myStepper.step(-stepsPerRevolution);
}
}
// // step one revolution in one direction:
// Serial.println("clockwise");
// myStepper.step(stepsPerRevolution);
// delay(500);
// // step one revolution in the other direction:
// Serial.println("counterclockwise");
// myStepper.step(-stepsPerRevolution);
// delay(500);
}