// Pin assignments..
const int dirPin = 10; // Direction pin
const int stepPin = 11; // Step pin
const int enablePin = 12; // Enable pin
const int forwardButton = 5; // Forward button pin
const int reverseButton = 8; // Reverse button pin
const int stopButton = 9; // Stop button pin
const int potPin = A0; // Potentiometer pin
// Variables
bool isRunning = false;
bool direction = true; // true = forward, false = reverse
int potValue = 0; // Stores the potentiometer value
unsigned long stepDelay = 500; // Step delay (speed control)
void setup() {
// Initialize pins
pinMode(dirPin, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(enablePin, OUTPUT);
digitalWrite(enablePin, LOW); // Enable stepper driver
// Initialize buttons with internal pull-ups
pinMode(forwardButton, INPUT_PULLUP);
pinMode(reverseButton, INPUT_PULLUP);
pinMode(stopButton, INPUT_PULLUP);
// Initialize potentiometer input
pinMode(potPin, INPUT);
}
void loop() {
// Read potentiometer value and map it to a delay range
potValue = analogRead(potPin); // Read value from potentiometer (0-1023)
stepDelay = map(potValue, 0, 1023, 2000, 200); // Map pot value to delay (slower to faster)
// Check button states
if (digitalRead(forwardButton) == LOW) {
direction = true;
isRunning = true;
}
if (digitalRead(reverseButton) == LOW) {
direction = false;
isRunning = true;
}
if (digitalRead(stopButton) == LOW) {
isRunning = false;
}
// Control motor direction and movement
if (isRunning) {
digitalWrite(dirPin, direction); // Set motor direction
// Generate step signal with variable speed
digitalWrite(stepPin, HIGH);
delayMicroseconds(stepDelay); // Adjust speed here by changing the delay
digitalWrite(stepPin, LOW);
delayMicroseconds(stepDelay);
}
}
Restart