// Define the stepper motor connections #define STEP_PIN 5 #define DIR_PIN 6 #define button 4 // Define the motor steps per revolution and microstep resolution const int STEPS_PER_REV = 200; const int MICROSTEP_RESOLUTION = 16; void setup() { // Set the step and direction pins as outputs pinMode(STEP_PIN, OUTPUT); pinMode(DIR_PIN, OUTPUT); pinMode(button, INPUT_PULLUP);} void loop() { // Move the stepper motor one revolution clockwise if (!digitalRead(button)) { rotateClockwise(1); } else {rotateantiClockwise(1);} // Delay for 1 second delay(1000); } void rotateClockwise(int revolutions) { // Set the direction pin to rotate clockwise digitalWrite(DIR_PIN, HIGH); // Calculate the number of steps required for the given number of revolutions int steps = revolutions * STEPS_PER_REV * MICROSTEP_RESOLUTION; // Step the motor for the calculated number of steps for (int i = 0; i < steps; i++) { digitalWrite(STEP_PIN, HIGH); delayMicroseconds(500); // Adjust this delay for the desired speed digitalWrite(STEP_PIN, LOW); delayMicroseconds(500); // Adjust this delay for the desired speed } } void rotateantiClockwise(int revolutions) { // Set the direction pin to rotate clockwise digitalWrite(DIR_PIN, LOW); // Calculate the number of steps required for the given number of revolutions int steps = revolutions * STEPS_PER_REV * MICROSTEP_RESOLUTION; // Step the motor for the calculated number of steps for (int i = 0; i < steps; i++) { digitalWrite(STEP_PIN, HIGH); delayMicroseconds(500); // Adjust this delay for the desired speed digitalWrite(STEP_PIN, LOW); delayMicroseconds(500); // Adjust this delay for the desired speed } }