#include "Stepper.h" #define STEPS 3200 #define SDELAY 100 bool clickFlag; bool dirFlag; const int clkPin=2; const int aPin=3; const int bPin=4; int ogPos = 0; int newPos; int qSteps; Stepper nema(STEPS, 8, 10, 9, 11); void isr () { delay(4); if (digitalRead(clkPin)) clickFlag = digitalRead(aPin); else clickFlag = !digitalRead(aPin); dirFlag = true; } void setup () { pinMode(clkPin,INPUT); pinMode(aPin,INPUT); pinMode(bPin,INPUT); digitalWrite(bPin, HIGH); attachInterrupt (0,isr,FALLING); } void loop () { nema.setSpeed(600); if (!(digitalRead(aPin))) { if (ogPos == 0) { } else { nema.step(-(ogPos*50)); ogPos=0; // Reset position to ZERO } } // Runs if rotation was detected if (clickFlag) { newPos = ogPos; if (dirFlag) { ogPos = ogPos - 1;} else { ogPos = ogPos + 1;} clickFlag = false; / if ((newPos + 1) == ogPos) { qSteps = 50; nema.step(qSteps); } if ((ogPos + 1) == newPos) { qSteps = -50; nema.step(qSteps); } } }