#include "PinChangeInterrupt.h"
#define step_pin 2 // Pin 9 connected to Steps pin on A4988 driver
#define dir_pin 3 // Pin 8 connected to Direction pin
//#define MS1 10 // Pin 10 connected to MS1 pin
//#define MS2 11 // Pin 11 connected to MS2 pin
//#define SLEEP 12 // Pin 12 connected to SLEEP pin
volatile boolean TurnDetected; // need volatile for Interrupts
volatile boolean rotationdirection; // CW or CCW rotation
// Rotary Encoder Module connections
const int PinCLK = 5; // Generating interrupts using CLK signal
const int PinDT = 6; // Reading DT signal
const int PinSW = 7; // Reading Push Button switch
int StepperPosition = 0; // To store Stepper Motor Position
int StepsToTake =200; // Controls the speed of the Stepper per Rotary click
int direction; // Variable to set Rotation (CW-CCW) of stepper
// Interrupt routine runs if CLK goes from HIGH to LOW
void rotarydetect () {
delay(4); // delay for Debouncing
if (digitalRead(PinCLK))
rotationdirection= digitalRead(PinDT);
else
rotationdirection= !digitalRead(PinDT);
TurnDetected = true;
}
void setup() {
//pinMode(MS1, OUTPUT);
//pinMode(MS2, OUTPUT);
pinMode(dir_pin, OUTPUT);
pinMode(step_pin, OUTPUT);
//pinMode(SLEEP, OUTPUT);
//digitalWrite(SLEEP, HIGH); // Wake up A4988 driver
delay(5); // Wait for A4988 driver wake up
/* Configure type of Steps on A4988 driver:
// MS1 MS2
//
// LOW LOW = Full Step //
// HIGH LOW = Half Step //
// LOW HIGH = A quarter of Step //
// HIGH HIGH = An eighth of Step //
*/
//digitalWrite(MS1, LOW); // Configures to Full Steps
//digitalWrite(MS2, LOW); // Configures to Full Steps
pinMode(PinCLK,INPUT); // Set Pin to Input
pinMode(PinDT,INPUT);
pinMode(PinSW,INPUT);
digitalWrite(PinSW, HIGH); // Pull-Up resistor for switch
attachPCINT(digitalPinToPCINT(PinCLK),rotarydetect,FALLING);
}
void loop() {
if (!(digitalRead(PinSW))) { // check if button is pressed
if (StepperPosition == 0) { // check if button was already pressed
} else {
if (StepperPosition > 0) { // Stepper was moved CW
while (StepperPosition != 0){ // Do until Motor position is back to ZERO
digitalWrite(dir_pin, HIGH); // (HIGH = anti-clockwise / LOW = clockwise)
for (int x = 1; x < StepsToTake; x++) {
digitalWrite(step_pin, HIGH);
delay(1);
digitalWrite(step_pin, LOW);
delay(1);
}
StepperPosition=StepperPosition-StepsToTake;
}
}
else {
while (StepperPosition != 0){
digitalWrite(dir_pin, LOW); // (HIGH = anti-clockwise / LOW = clockwise)
for (int x = 1; x < StepsToTake; x++) {
digitalWrite(step_pin, HIGH);
delay(1);
digitalWrite(step_pin, LOW);
delay(1);
}
StepperPosition=StepperPosition+StepsToTake;
}
}
StepperPosition=0; // Reset position to ZERO after moving motor back
}
}
// Runs if rotation was detected
if (TurnDetected) {
TurnDetected = false; // do NOT repeat IF loop until new rotation detected
// Which direction to move Stepper motor
if (rotationdirection) { // Move motor CCW
digitalWrite(dir_pin, HIGH); // (HIGH = anti-clockwise / LOW = clockwise)
for (int x = 1; x < StepsToTake; x++) {
digitalWrite(step_pin, HIGH);
delay(1);
digitalWrite(step_pin, LOW);
delay(1);
}
StepperPosition=StepperPosition-StepsToTake;
}
if (!rotationdirection) { // Move motor CW
digitalWrite(dir_pin, LOW); // (HIGH = anti-clockwise / LOW = clockwise)
for (int x = 1; x < StepsToTake; x++) {
digitalWrite(step_pin, HIGH);
delay(1);
digitalWrite(step_pin, LOW);
delay(1);
}
StepperPosition=StepperPosition+StepsToTake;
}
}
}