#include <SoftwareSerial.h>
#define BT_RX_PIN 10
#define BT_TX_PIN 11
#define MOTOR_A_IN1 2
#define MOTOR_A_IN2 3
#define MOTOR_B_IN1 4
#define MOTOR_B_IN2 5
SoftwareSerial bluetooth(BT_RX_PIN, BT_TX_PIN);
void setup() {
pinMode(MOTOR_A_IN1, OUTPUT);
pinMode(MOTOR_A_IN2, OUTPUT);
pinMode(MOTOR_B_IN1, OUTPUT);
pinMode(MOTOR_B_IN2, OUTPUT);
bluetooth.begin(9600);
}
void loop() {
if (bluetooth.available()) {
char command = bluetooth.read();
if (command == 'F') {
moveForward();
} else if (command == 'B') {
moveBackward();
} else if (command == 'L') {
turnLeft();
} else if (command == 'R') {
turnRight();
} else {
stopMoving();
}
}
}
void moveForward() {
digitalWrite(MOTOR_A_IN1, HIGH);
digitalWrite(MOTOR_A_IN2, LOW);
digitalWrite(MOTOR_B_IN1, HIGH);
digitalWrite(MOTOR_B_IN2, LOW);
}
void moveBackward() {
digitalWrite(MOTOR_A_IN1, LOW);
digitalWrite(MOTOR_A_IN2, HIGH);
digitalWrite(MOTOR_B_IN1, LOW);
digitalWrite(MOTOR_B_IN2, HIGH);
}
void turnLeft() {
digitalWrite(MOTOR_A_IN1, LOW);
digitalWrite(MOTOR_A_IN2, HIGH);
digitalWrite(MOTOR_B_IN1, HIGH);
digitalWrite(MOTOR_B_IN2, LOW);
}
void turnRight() {
digitalWrite(MOTOR_A_IN1, HIGH);
digitalWrite(MOTOR_A_IN2, LOW);
digitalWrite(MOTOR_B_IN1, LOW);
digitalWrite(MOTOR_B_IN2, HIGH);
}
void stopMoving() {
digitalWrite(MOTOR_A_IN1, LOW);
digitalWrite(MOTOR_A_IN2, LOW);
digitalWrite(MOTOR_B_IN1, LOW);
digitalWrite(MOTOR_B_IN2, LOW);
}