#include <AFMotor.
h> // Include Adafruit Motor Shield library
AF_DCMotor motor1(1); // Connect motor 1 to port 1 on the motor shield
AF_DCMotor motor2(2); // Connect motor 2 to port 2 on the motor shield
char command; // Variable to store the received command
void setup() {
Serial.begin(9600); // Set the data rate for the serial communication
motor1.setSpeed(200); // Set motor speed (0-255)
motor2.setSpeed(200);
}
void loop() {
if (Serial.available() > 0) {
command = Serial.read(); // Read the incoming command
// Control motors based on the command received
if (command == 'F') { // Forward
motor1.run(FORWARD);
motor2.run(FORWARD);
}
else if (command == 'B') { // Backward
motor1.run(BACKWARD);
motor2.run(BACKWARD);
}
else if (command == 'L') { // Left
motor1.run(BACKWARD);
motor2.run(FORWARD);
}
else if (command == 'R') { // Right
motor1.run(FORWARD);
motor2.run(BACKWARD);
}
else if (command == 'S') { // Stop
motor1.run(RELEASE);
motor2.run(RELEASE);
}
}
}