ุงู†ุชู‚ู„ ู„ู„ู…ุญุชูˆู‰
Robo EgyptRobo Egypt
โ˜ฐ Course contents
Robo Maker: home
0. Lab tour1
1. The start: the Arduino6
2. Wiring and the traffic light4
3. Inputs: the button4
4. Sound and movement5
5. Sensors and numbers5
6. Distance, the screen and colours5
7. Motors and the car4
8. The project: the city's smart gate3
9. From blocks to C++ code5
10. Sensors and screens in code5
11. Smart systems5
12. Bluetooth: control from the phone1

๐Ÿ› ๏ธ 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.
โš™๏ธ The full code: a drive function runs the two motors
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);
    }
  }
}
F arrived: both wheels turn forward
F arrived: both wheels turn forward
B arrived: backward
B arrived: backward
S arrived: stopped
S arrived: stopped
๐Ÿ–ฅ๏ธ 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.

โš™๏ธ The letter L for left and R for 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);
  }
}
L: left
L: left
R: right
R: right
โœ๏ธ Practise
Finish the Bluetooth car's code.
โœ๏ธ Practise here in the lessonor open the lab in a new page โ†—

๐ŸŽฏ Challenge yourself

Easy

Add the letter B for backward.

๐Ÿ‘€ Show the solution
โš™๏ธ 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);
  }
}
In the lab
In the lab

Result: it goes backward.

Medium

The F command moves for only one second, then the car stops by itself.

๐Ÿ‘€ Show the solution
โš™๏ธ 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);
    }
  }
}
In the lab
In the lab

Result: it moves, then stops.

Hard

The car writes on the Serial what it is doing: โ€œforwardโ€ or โ€œstopโ€.

๐Ÿ‘€ Show the solution
โš™๏ธ 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");
    }
  }
}
In the lab
In the lab
๐Ÿ–ฅ๏ธ Serial Monitor
forward
stop

Result: forward, then stop.

Finished the lesson? Mark it so your progress counts.