Arduino – Control CAROBOT Via Bluetooth (by using MIT app inventor)
Posted by Saya H on
In this article, we will show you how to program the robot car so it can be connected by Bluetooth and create an android application to control it using MIT App Inventor.
Take a look at this previous post on how to build a robot car.
Parts
- Arduino Mega R3
- CAROBOT Motor Driver and Servo Shield
- Chassis
- Battery
- Jumper wires
- Soldering iron
- Arduino IDE
Have an Android device with you. We will use it later to control the car's action.
Wiring
In addition to the pin set up for the Arduino from the previous post, we will use Serial3 on the Arduino Mega for Bluetooth connection. Serial3 on the Arduino Mega is associated with Pin 14 and 15. The wiring diagram is shown below.
- VCC -> 5V
- GND -> GND
- TXD -> Pin 15
- RXD -> Pin 14
Code
Jump to the full code here.
In addition to the code we used in this previous post, we’ll be defining a variable to save the input from Bluetooth.
char msg = 0;
Next, set Serial and Serial3 to 9600 baud rate to start serial communication so that the Arduino can send out commands. The value 9600 represents how fast the data is to be sent. Refer to our HC-05 guide for more details on how to use the HC-05 Bluetooth module.
void setup() {
Serial.begin(9600);
Serial.println("Begin");
Serial3.begin(9600);
pinMode(A, OUTPUT);
pinMode(dirA, OUTPUT);
pinMode(B, OUTPUT);
pinMode(dirB, OUTPUT);
}
We’ll also define a function for stopping the car.
void stopMotor() {
Serial.println("stop");
digitalWrite(A, LOW);
digitalWrite(dirA, LOW);
digitalWrite(B, LOW);
digitalWrite(dirB, LOW);
}
Lastly, we’ll use a switch case to direct the car, depending on the signals sent through Bluetooth. We’ll use the loop function to keep reading user input, which is going to be a char byte from Serial3 and classify it case-by-case to perform the proper action.
void loop() {
if (Serial3.available()){
msg = Serial3.read();
if(msg=='w' ||msg=='a' ||msg=='d'||msg=='s'||msg=='e'){
switch(msg){
case 'w':
Serial.println("forward");
digitalWrite(A, HIGH);
digitalWrite(dirA, HIGH);
digitalWrite(B, HIGH);
digitalWrite(dirB, HIGH);
delay(700);
stopMotor();
break;
case 'd':
Serial.println("right");
digitalWrite(A, HIGH);
digitalWrite(dirA, HIGH);
digitalWrite(B, HIGH);
digitalWrite(dirB, LOW);
delay(200);
stopMotor();
break;
case 'a':
Serial.println("left");
digitalWrite(A, HIGH);
digitalWrite(dirA, LOW);
digitalWrite(B, HIGH);
digitalWrite(dirB, HIGH);
delay(200);
stopMotor();
break;
case 's':
Serial.println("backward");
digitalWrite(A, HIGH);
digitalWrite(dirA, LOW);
digitalWrite(B, HIGH);
digitalWrite(dirB, LOW);
delay(700);
stopMotor();
break;
case'e': // end (stop moving)
stopMotor();
break;
}
}
}
if (Serial.available())
Serial3.write(Serial.read());
}
}
Full Code
Let's put everything together.
#define A 3
#define dirA 12
#define B 11
#define dirB 13
char msg = 0;
void setup() {
Serial.begin(9600);
Serial.println("Begin");
Serial3.begin(9600);
pinMode(A, OUTPUT);
pinMode(dirA, OUTPUT);
pinMode(B, OUTPUT);
pinMode(dirB, OUTPUT);
}
void loop() {
if (Serial3.available()){
msg = Serial3.read();
if(msg=='w' ||msg=='a' ||msg=='d'||msg=='s'||msg=='e'){
switch(msg){
case 'w':
Serial.println("forward");
digitalWrite(A, HIGH);
digitalWrite(dirA, HIGH);
digitalWrite(B, HIGH);
digitalWrite(dirB, HIGH);
delay(700);
stopMotor();
break;
case 'd':
Serial.println("right");
digitalWrite(A, HIGH);
digitalWrite(dirA, HIGH);
digitalWrite(B, HIGH);
digitalWrite(dirB, LOW);
delay(200);
stopMotor();
break;
case 'a':
Serial.println("left");
digitalWrite(A, HIGH);
digitalWrite(dirA, LOW);
digitalWrite(B, HIGH);
digitalWrite(dirB, HIGH);
delay(200);
stopMotor();
break;
case 's':
Serial.println("backward");
digitalWrite(A, HIGH);
digitalWrite(dirA, LOW);
digitalWrite(B, HIGH);
digitalWrite(dirB, LOW);
delay(700);
stopMotor();
break;
case'e': // end (stop moving)
stopMotor();
break;
}
}
}
if (Serial.available())
Serial3.write(Serial.read());
}
Now, connect the robot car to your computer and program it.
In the next post, we will use MIT APP INVENTOR to create an application for Android OS so we can control the robot car using an Android device.