// Shows how to run AccelStepper in the simplest,
// fixed speed mode with no accelerations
#include "AccelStepper.h"
int btn = 8;
int tgtDistance = 10; // Set defailt distance value
#define encClk 2
#define encDT 3
#define encSw 9
int mmSteps = 50; // Steps per mm of output
const int dirPin = 4;
const int stepPin = 5;
//
unsigned long pollTime = 5000;
unsigned long last_Run = millis() + pollTime;
// Creates an instance
AccelStepper stepper(AccelStepper::DRIVER, stepPin, dirPin);
void setup()
{
Serial.begin(9600);
stepper.setMaxSpeed(1000); // Top speed after acceleration
stepper.setAcceleration(800); // rate of accel/decel
pinMode(btn, INPUT_PULLUP);
pinMode(encSw, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(encDT), modsetPoint, FALLING);
}
void loop() {
if (digitalRead(btn) == LOW || digitalRead(encSw) == LOW) // Use the standalone button or the encoder button
{
motMove(tgtDistance); // Call the motMove function
}
}
void motMove(int mm) {
if (stepper.distanceToGo() == 0) {
stepper.move(mm * mmSteps);
}
stepper.runToPosition();
}
// Function on interrupt detection
void modsetPoint() {
if (digitalRead(encClk) == 1) {
tgtDistance--;
}
if (digitalRead(encClk) == 0) {
tgtDistance++;
}
tgtDistance = constrain(tgtDistance,0,120); //Limit the range, cannot go backwards
last_Run = millis() + pollTime;
Serial.println(tgtDistance);
}