#include <SoftwareSerial.h>
#include <Servo.h>
// ==================================
// Bluetooth HC-05
// ==================================
SoftwareSerial BT(2, 7); // RX, TX
// ==================================
// MX1508 Motor Driver
// ==================================
#define IN1 5
#define IN2 6
#define IN3 11
#define IN4 3
// ==================================
// Servo Motor SG90
// ==================================
#define SERVO_PIN 4
Servo steering;
// ==================================
// Motor Speed
// ==================================
int motorSpeed = 200;
// ==================================
// Steering Angles - CORRECTED
// ==================================
const int CENTER = 90;
const int LEFT = 120;
const int RIGHT = 60;
// ==================================
// Safety Timeout
// ==================================
const unsigned long TIMEOUT = 150;
unsigned long lastCommandTime = 0;
bool moving = false;
char currentDirection = 'S';
// ==================================
// STOP MOTORS
// ==================================
void stopMotors() {
analogWrite(IN1, 0);
analogWrite(IN2, 0);
analogWrite(IN3, 0);
analogWrite(IN4, 0);
moving = false;
currentDirection = 'S';
}
// ==================================
// FORWARD
// ==================================
void forward() {
analogWrite(IN1, motorSpeed);
analogWrite(IN2, 0);
analogWrite(IN3, motorSpeed);
analogWrite(IN4, 0);
moving = true;
currentDirection = 'F';
}
// ==================================
// BACKWARD
// ==================================
void backward() {
analogWrite(IN1, 0);
analogWrite(IN2, motorSpeed);
analogWrite(IN3, 0);
analogWrite(IN4, motorSpeed);
moving = true;
currentDirection = 'B';
}
// ==================================
// SETUP
// ==================================
void setup() {
Serial.begin(9600);
BT.begin(9600);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
stopMotors();
steering.attach(SERVO_PIN);
steering.write(CENTER);
lastCommandTime = millis();
Serial.println("Bluetooth RC Car Ready");
}
// ==================================
// MAIN LOOP
// ==================================
void loop() {
while (BT.available()) {
char command = BT.read();
if (command == '\n' || command == '\r') {
continue;
}
Serial.print("Received: ");
Serial.println(command);
switch (command) {
// FORWARD
case 'F':
steering.write(CENTER);
forward();
lastCommandTime = millis();
break;
// BACKWARD
case 'B':
steering.write(CENTER);
backward();
lastCommandTime = millis();
break;
// LEFT - CORRECTED
case 'L':
steering.write(LEFT);
forward();
lastCommandTime = millis();
break;
// RIGHT - CORRECTED
case 'R':
steering.write(RIGHT);
forward();
lastCommandTime = millis();
break;
// STOP
case 'S':
stopMotors();
steering.write(CENTER);
break;
// CENTER STEERING
case 'C':
steering.write(CENTER);
break;
// INCREASE SPEED
case '+':
motorSpeed = min(255, motorSpeed + 15);
if (moving) {
if (currentDirection == 'B') {
backward();
} else {
forward();
}
}
break;
// DECREASE SPEED
case '-':
motorSpeed = max(80, motorSpeed - 15);
if (moving) {
if (currentDirection == 'B') {
backward();
} else {
forward();
}
}
break;
}
}
// ==================================
// AUTOMATIC STOP
// ==================================
if (moving &&
millis() - lastCommandTime > TIMEOUT) {
stopMotors();
steering.write(CENTER);
}
}