// Motor pins
const int dirPin1 = 2;
const int stepPin1 = 3;
const int dirPin2 = 4;
const int stepPin2 = 5;
// Button pins
const int button1Pin = 6; // Step Motor 1
const int button2Pin = 7; // Step Motor 2
const int toggleButtonPin = 8; // Toggle direction
// State variables
bool currentDirection = true; // true = HIGH (forward), false = LOW (reverse)
bool lastToggleState = HIGH;
void setup() {
// Motor pin modes
pinMode(stepPin1, OUTPUT);
pinMode(dirPin1, OUTPUT);
pinMode(stepPin2, OUTPUT);
pinMode(dirPin2, OUTPUT);
// Button pin modes
pinMode(button1Pin, INPUT_PULLUP);
pinMode(button2Pin, INPUT_PULLUP);
pinMode(toggleButtonPin, INPUT_PULLUP);
// Set initial direction
digitalWrite(dirPin1, currentDirection);
digitalWrite(dirPin2, currentDirection);
}
void loop() {
// Read toggle button state
bool toggleState = digitalRead(toggleButtonPin);
// On button press (rising edge)
if (toggleState == LOW && lastToggleState == HIGH) {
currentDirection = !currentDirection; // Flip direction
digitalWrite(dirPin1, currentDirection);
digitalWrite(dirPin2, currentDirection);
delay(200); // Debounce
}
lastToggleState = toggleState;
// Step motor 1 if button pressed
if (digitalRead(button1Pin) == LOW) {
stepMotor(stepPin1);
delay(200);
}
// Step motor 2 if button pressed
if (digitalRead(button2Pin) == LOW) {
stepMotor(stepPin2);
delay(200);
}
}
void stepMotor(int stepPin) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(800);
digitalWrite(stepPin, LOW);
delayMicroseconds(800);
}