// Define the connections to the A4988 driver
#define STEP_PIN 3
#define DIR_PIN 2
#define BUTTON_CW 4
#define BUTTON_CCW 5
// Define the number of steps per revolution for your stepper motor
#define STEPS_PER_REV 200
void setup() {
// Set the pin modes
pinMode(STEP_PIN, OUTPUT);
pinMode(DIR_PIN, OUTPUT);
pinMode(BUTTON_CW, INPUT_PULLUP);
pinMode(BUTTON_CCW, INPUT_PULLUP);
// Initialize the direction to clockwise
digitalWrite(DIR_PIN, LOW);
}
void loop() {
// Read the button states
bool buttonCW = !digitalRead(BUTTON_CW); // Inverted because of INPUT_PULLUP
bool buttonCCW = !digitalRead(BUTTON_CCW);
if (buttonCW) {
// Rotate clockwise
digitalWrite(DIR_PIN, LOW);
rotateMotor(STEPS_PER_REV); // Complete one revolution clockwise
rotateMotor(STEPS_PER_REV);
} else if (buttonCCW) {
// Rotate counterclockwise
digitalWrite(DIR_PIN, HIGH);
rotateMotor(STEPS_PER_REV); // Complete one revolution counterclockwise
rotateMotor(STEPS_PER_REV);
}
}
void rotateMotor(int steps) {
for (int i = 0; i < steps; i++) {
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(1000); // Adjust the delay to control speed
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(1000); // Adjust the delay to control speed
}
}