/* Example sketch to control a stepper motor with A4988 stepper motor driver, AccelStepper library and Arduino: continuous rotation. More info: https://www.makerguides.com */ // Include the AccelStepper library: #include "AccelStepper.h" // Define stepper motor connections and motor interface type. #define dirPin 2 #define stepPin 3 #define motorInterfaceType 1 // Motor interface type must be set to 1 when using a driver #define ms1Pin 4 #define ms2Pin 5 #define ms3Pin 6 #define speedPin A0 double lastTime = 0; // double lastTimesensors = 0; double thisTime = 0; const int SPEED_MIN = 50; const int SPEED_MAX = 800; // Create a new instance of the AccelStepper class: AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin); void setup() { Serial.begin(115200); // Set the maximum speed in steps per second: stepper.setMaxSpeed(4000); // MS // 1 2 3 // 0 0 0 full // 1 0 0 1/2 // 0 1 0 1/4 // 0 0 1 1/8 // 1 1 1 1/16 digitalWrite(ms1Pin, 1); digitalWrite(ms2Pin, 1); digitalWrite(ms3Pin, 1); // Set the speed in steps per second: stepper.setSpeed(200); } void loop() { thisTime = millis(); if ((thisTime - lastTime) > 1000) { lastTime = thisTime; int speed_in = analogRead(speedPin); Serial.print("speed_in : "); Serial.print(speed_in); int speed = map(speed_in, 0, 1023, SPEED_MIN, SPEED_MAX); Serial.print(" | speed : "); Serial.println(speed); stepper.setSpeed(speed); } // Step the motor with a constant speed as set by setSpeed(): stepper.runSpeed(); }