const int dirPin = 3;
const int stepPin = 4;
const int potPin = A0; // Connect potentiometer to analog pin A0
const int stepsPerRevolution = 200;
const int stopThreshold = 50; // Threshold value to determine if the motor should stop
void setup() {
// Initialize pins
pinMode(dirPin, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(potPin, INPUT);
}
void loop() {
// Read the potentiometer value (0 to 1023)
int potValue = analogRead(potPin);
// Determine the delay time based on the potentiometer value
// The delay time ranges from 5000 microseconds (slow speed) to 500 microseconds (fast speed)
// If the potentiometer value is below the stop threshold, set delay to a high value (effectively stop)
int delayTime = (potValue < stopThreshold) ? 10000 : map(potValue, stopThreshold, 1023, 10000, 500);
// Set motor direction
digitalWrite(dirPin, HIGH);
// Perform the stepping
for (int x = 0; x < stepsPerRevolution; x++) {
if (potValue >= stopThreshold) { // Only step if the potentiometer is above the stop threshold
digitalWrite(stepPin, HIGH);
delayMicroseconds(delayTime);
digitalWrite(stepPin, LOW);
delayMicroseconds(delayTime);
}
}
// Add a small delay to allow for the potentiometer reading to settle
delay(100);}