//Mainecoast Shop Digital product
#include "FastAccelStepper.h"
//#include "AVRStepperPins.h" // Only required for AVR controllers
#define dirPinStepper 5
#define enablePinStepper 6
#define stepPinStepper 9
#define signalPin A0
int speed;
int var;
// Change this based on your setup! max number of pulses per second you motor and driver can handle before stalling
int maxspeed =2650;
int mult = maxspeed/450;
int minspeed = maxspeed/160;
// If using an AVR device use the definitons provided in AVRStepperPins
// stepPinStepper1A
//
// or even shorter (for 2560 the correct pin on the chosen timer is selected):
// stepPinStepperA
FastAccelStepperEngine engine = FastAccelStepperEngine();
FastAccelStepper *stepper = NULL;
void setup() {
//Serial.begin(115200);
// the following is required to initialize the stepper motor witht he correct pins
engine.init();
stepper = engine.stepperConnectToPin(stepPinStepper);
if (stepper) {
stepper->setDirectionPin(dirPinStepper);
stepper->setEnablePin(enablePinStepper);
stepper->setAutoEnable(true);
// Acceleration set to 3000 steps per second
stepper->setAcceleration(30000); // 100 steps/s²
}
}
void loop() {
// reads potentiometer value
var = analogRead(signalPin);
// If potentiometer is in the lower 40%, move ccw
//if (var < 450){
speed = ((maxspeed - var*mult) + minspeed);
stepper->setSpeedInHz(speed);
// the number of steps away
const long stepsThreshold = -1000;
// Get the current and target positions
long currentPosition = stepper->getCurrentPosition();
long targetPosition = 1000;
// Check if steps remaining are below the threshold
if ((targetPosition + currentPosition) > stepsThreshold) {
// Update the target position to a higher value to keep the stepper motor running continuously
stepper->moveTo(currentPosition - 1000);
}
//}
// If stepper potentiometer is in the upper 40%, move cw
}