#define DIR_PIN 2
#define STEP_PIN 3
void setup() {
pinMode(STEP_PIN, OUTPUT);
pinMode(DIR_PIN, OUTPUT);
digitalWrite(STEP_PIN, LOW);
Serial.begin(115200);
}
void loop() {
if (Serial.available() > 0) {
String command = Serial.readStringUntil('\n');
command.trim(); // Remove any extra spaces or newline characters
if (command.equalsIgnoreCase("right")) {
rotateStepper(true); // Rotate clockwise
} else if (command.equalsIgnoreCase("left")) {
rotateStepper(false); // Rotate counterclockwise
} else {
Serial.println("Invalid command. Enter 'right' or 'left'.");
}
}
}
void rotateStepper(bool clockwise) {
if (clockwise) {
digitalWrite(DIR_PIN, HIGH); // Set direction to clockwise
Serial.println("Rotating clockwise...");
} else {
digitalWrite(DIR_PIN, LOW); // Set direction to counterclockwise
Serial.println("Rotating counterclockwise...");
}
for (int i = 0; i < 200; i++) {
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(500); // Adjust speed by changing the delay
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(500); // Adjust speed by changing the delay
}
delay(1000); // Wait one second before the next command
}