// Motor pins
const int dirPin1 = 2;
const int stepPin1 = 3;
const int dirPin2 = 4;
const int stepPin2 = 5;
// Button pins
const int button1Pin = 6;
const int button2Pin = 7;
void setup() {
pinMode(stepPin1, OUTPUT);
pinMode(dirPin1, OUTPUT);
pinMode(stepPin2, OUTPUT);
pinMode(dirPin2, OUTPUT);
pinMode(button1Pin, INPUT_PULLUP); // Buttons wired to GND, so use internal pull-up
pinMode(button2Pin, INPUT_PULLUP);
// Set initial direction forward for both motors
digitalWrite(dirPin1, HIGH);
digitalWrite(dirPin2, LOW);
}
void loop() {
// Check button 1 press
if (digitalRead(button1Pin) == LOW) { // Button pressed (active LOW)
stepMotor(stepPin1);
delay(50); // Debounce + speed limit
}
// Check button 2 press
if (digitalRead(button2Pin) == LOW) { // Button pressed (active LOW)
stepMotor(stepPin2);
delay(50); // Debounce + speed limit
}
}
void stepMotor(int stepPin) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(800);
digitalWrite(stepPin, LOW);
delayMicroseconds(800);
}