๐ ๏ธ The final project: a Bluetooth car
Section 12: Bluetooth: control from the phoneLesson 12.1 ยท โฑ 15 min
The HC-05 module receives letters from the phone. Serial.available() asks: has a letter arrived? And Serial.read() reads it. F forward, B backward, S stop.
๐ฏ Project goal
Finish the code: F forward, B backward, S stop. Then control the car from the remote.
const int IN1 = 9, IN2 = 8, IN3 = 7, IN4 = 6;
void setup() {
Serial.begin(9600);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
}
void drive(int a, int b, int c, int d) {
digitalWrite(IN1, a);
digitalWrite(IN2, b);
digitalWrite(IN3, c);
digitalWrite(IN4, d);
}
void loop() {
if (Serial.available() > 0) {
char c = Serial.read();
if (c == 'F') {
drive(HIGH, LOW, HIGH, LOW);
}
if (c == 'B') {
drive(LOW, HIGH, LOW, HIGH);
}
if (c == 'S') {
drive(LOW, LOW, LOW, LOW);
}
}
}



๐ฅ๏ธ What happens on the stage?
We sent F, then B, then S in Robo Maker, and the challenge passed.
๐ก Tip
To improve it: add L and R to turn left and right, as in the โAdd commands to the remoteโ challenge.
๐ Another example: the letter L for left and R for right
We add two commands: L turns left, and R turns right.
const int IN1 = 9, IN2 = 8, IN3 = 7, IN4 = 6;
void setup() {
Serial.begin(9600);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
}
void drive(int a, int b, int c, int d) {
digitalWrite(IN1, a);
digitalWrite(IN2, b);
digitalWrite(IN3, c);
digitalWrite(IN4, d);
}
void loop() {
if (Serial.available() > 0) {
char c = Serial.read();
if (c == 'F') drive(HIGH, LOW, HIGH, LOW);
if (c == 'S') drive(LOW, LOW, LOW, LOW);
if (c == 'L') drive(LOW, HIGH, HIGH, LOW);
if (c == 'R') drive(HIGH, LOW, LOW, HIGH);
}
}


โ๏ธ Practise
Finish the Bluetooth car's code.
โ๏ธ Practise here in the lessonโ Close the labor open the lab in a new page โ
๐ฏ Challenge yourself
Easy
Add the letter B for backward.
๐ Show the solution๐ Hide the solution
const int IN1 = 9, IN2 = 8, IN3 = 7, IN4 = 6;
void setup() {
Serial.begin(9600);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
}
void drive(int a, int b, int c, int d) {
digitalWrite(IN1, a);
digitalWrite(IN2, b);
digitalWrite(IN3, c);
digitalWrite(IN4, d);
}
void loop() {
if (Serial.available() > 0) {
char c = Serial.read();
if (c == 'F') drive(HIGH, LOW, HIGH, LOW);
if (c == 'B') drive(LOW, HIGH, LOW, HIGH);
if (c == 'S') drive(LOW, LOW, LOW, LOW);
}
}

Result: it goes backward.
Medium
The F command moves for only one second, then the car stops by itself.
๐ Show the solution๐ Hide the solution
const int IN1 = 9, IN2 = 8, IN3 = 7, IN4 = 6;
void setup() {
Serial.begin(9600);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
}
void drive(int a, int b, int c, int d) {
digitalWrite(IN1, a);
digitalWrite(IN2, b);
digitalWrite(IN3, c);
digitalWrite(IN4, d);
}
void loop() {
if (Serial.available() > 0) {
char c = Serial.read();
if (c == 'F') {
drive(HIGH, LOW, HIGH, LOW);
delay(1000);
drive(LOW, LOW, LOW, LOW);
}
}
}

Result: it moves, then stops.
Hard
The car writes on the Serial what it is doing: โforwardโ or โstopโ.
๐ Show the solution๐ Hide the solution
const int IN1 = 9, IN2 = 8, IN3 = 7, IN4 = 6;
void setup() {
Serial.begin(9600);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
}
void drive(int a, int b, int c, int d) {
digitalWrite(IN1, a);
digitalWrite(IN2, b);
digitalWrite(IN3, c);
digitalWrite(IN4, d);
}
void loop() {
if (Serial.available() > 0) {
char c = Serial.read();
if (c == 'F') {
drive(HIGH, LOW, HIGH, LOW);
Serial.println("forward");
}
if (c == 'S') {
drive(LOW, LOW, LOW, LOW);
Serial.println("stop");
}
}
}

forward
stop
Result: forward, then stop.

