const int motorStep = 2;
const int motorDir = 3;
const int ms1Pin = 4;
const int ms2Pin = 5;
const int enablePin = 6;
const int potPin = A0;
const int buttonPin = 7;
void setup() {
pinMode(motorStep, OUTPUT);
pinMode(motorDir, OUTPUT);
pinMode(ms1Pin, OUTPUT);
pinMode(ms2Pin, OUTPUT);
pinMode(enablePin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
int potValue = analogRead(potPin);
int motorSpeed = map(potValue, 0, 1023, 0, 255);
if (digitalRead(buttonPin) == HIGH) {
digitalWrite(motorDir, HIGH); // Clockwise
digitalWrite(enablePin, LOW); // Enable driver
analogWrite(motorStep, motorSpeed);
} else {
digitalWrite(motorDir, LOW); // Counterclockwise
digitalWrite(enablePin, HIGH); // Disable driver
analogWrite(motorStep, 0);
}
}