const int dirPin = 1; // DIR pin connected to digital pin 1 const int stepPin = 0; // STEP pin connected to digital pin 0 enum MotorState { STOP, TURN_RIGHT, TURN_LEFT }; MotorState currentState = STOP; void setup() { Serial.begin(57600); // Initialize serial communication at 9600 baud pinMode(dirPin, OUTPUT); // Set direction pin as output pinMode(stepPin, OUTPUT); // Set step pin as output } void loop() { while (Serial.available() == 0) { // Wait for user input delay(10); // Small delay to avoid busy waiting } char motor = Serial.read(); char direction = Serial.read(); if (motor == 'B' || motor == 'b') { if (direction == 'R' || direction == 'r') { Serial.println("Motor B turning Right!"); right(); } else if (direction == 'L' || direction == 'l') { Serial.println("Motor B turning Left!"); left(); } else { Serial.println("Invalid direction. Use R or L."); } } else { Serial.println("Invalid motor. Use A or B."); } } void right() { // Set direction to clockwise digitalWrite(dirPin, HIGH); Serial.println("Clockwise"); for (int i = 0; i < 200; i++) { digitalWrite(stepPin, HIGH); delayMicroseconds(1000); // 1 ms delay digitalWrite(stepPin, LOW); delayMicroseconds(1000); // 1 ms delay } } void left() { // Set direction to clockwise digitalWrite(dirPin, LOW); Serial.println("Counter-Clockwise"); for (int i = 0; i < 200; i++) { digitalWrite(stepPin, HIGH); delayMicroseconds(1000); // 1 ms delay digitalWrite(stepPin, LOW); delayMicroseconds(1000); // 1 ms delay } }