// Define pin numbers
const int buttonPin1 = 2; // Connect your push button to this pin
const int buttonPin2 = 3; // Connect your push button to this pin
const int dirPin1 = 4; // Connect A4988 driver STEP pin to this pin
const int stepPin1 = 5; // Connect A4988 driver STEP pin to this pin
const int dirPin2 = 6; // Connect A4988 driver DIR pin to this pin
const int stepPin2 = 7; // Connect A4988 driver DIR pin to this pin
// Variables for stepper motor control
int stepsPerBB = 12; // Change this according to your motor's specifications
int steps = 20;
int stepDelay = 5; // Adjust this for your desired speed
int endDelay = 10; // last delay after stepper movement
void setup() {
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(dirPin1, OUTPUT);
pinMode(stepPin1, OUTPUT);
pinMode(dirPin2, OUTPUT);
pinMode(stepPin2, OUTPUT);
}
void loop() {
int buttonState1 = digitalRead(buttonPin1);
int buttonState2 = digitalRead(buttonPin2);
if (buttonState1 == HIGH) {
// Button is pressed, turn one BB
digitalWrite(dirPin1, HIGH); // Set direction to clockwise
digitalWrite(dirPin2, HIGH); // Set direction to clockwise
for (int j = 0; j < 1; ++j) {
for (int i = 0; i < stepsPerBB; ++i) {
digitalWrite(stepPin1, HIGH);
delayMicroseconds(stepDelay);
digitalWrite(stepPin1, LOW);
delayMicroseconds(stepDelay);
delay(endDelay); // Pause for a moment before allowing another BB
}
for (int k = 0; k < stepsPerBB; ++k) {
digitalWrite(stepPin1, HIGH);
delayMicroseconds(stepDelay);
digitalWrite(stepPin1, LOW);
delayMicroseconds(stepDelay);
delay(endDelay); // Pause for a moment before allowing another BB
}
for (int n = 0; n < stepsPerBB; ++n) {
digitalWrite(stepPin2, HIGH);
delayMicroseconds(stepDelay);
digitalWrite(stepPin2, LOW);
delayMicroseconds(stepDelay);
delay(endDelay); // Pause for a moment before allowing another BB
}
}
}
if (buttonState2 == HIGH) {
// Button is pressed, turn one BB
digitalWrite(dirPin1, HIGH); // Set direction to clockwise
for (int i = 0; i < stepsPerBB; i++) {
digitalWrite(stepPin1, HIGH);
delayMicroseconds(stepDelay);
digitalWrite(stepPin1, LOW);
delayMicroseconds(stepDelay);
delay(endDelay); // Pause for a moment before allowing another BB
}
}
}