My Fourth Project: Smart Tank Chassis With Bluetooth
by Rookie P in Circuits > Arduino
2723 Views, 42 Favorites, 0 Comments
My Fourth Project: Smart Tank Chassis With Bluetooth
It is far better if the tank can move according to my instruction at instant rather than the directions set before. After some researches I plan to control the tank with my mobile phone via Bluetooth module.
Parts
Wiring
It is quite simple. On top of the third project I connect RXD to pin 7, TXD to pin 6, GND to GND in Arduino and VCC to 5V. The wiring can be done with a small breadboard.
Code
The code is very similar to previous one except the initial Bluetooth pin setting and the final loop. For example: if the Bluetooth receive a letter "w", then the tank moves forward.
#include <SoftwareSerial.h>
SoftwareSerial BT(6, 7); //set TX and RX on bluetooth to pin 6 and 7 respectivelychar command;
int motorPin = 8; //right side to IB - forward
int motorPin2 = 9; //left side to IA - forward
int motorPin3 = 10; //right side to IA - backward
int motorPin4 = 11; //left side to IB - backward
void setup() {
BT.begin (9600);
pinMode(motorPin, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
}
void stop() {
digitalWrite(motorPin, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
}
void forward(){
digitalWrite(motorPin, HIGH);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
}
void backward() {
digitalWrite(motorPin, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, HIGH);
}
void turnLeft() {
digitalWrite(motorPin, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
}
void turnRight() {
digitalWrite(motorPin, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
}
void loop() {
if (BT.available() > 0) {
command = BT.read();
switch (command) {
case 'w' :
forward();
break;
case 'x' :
backward();
break;
case 'a' :
turnLeft();
break;
case 'd' :
turnRight();
break;
case 's' :
stop();
break;
}
}
}
Upload this code to Arduino first before moving to the next step.
App
I choose to create an app in Android just because my phone is in Android. : P (Please let me know if you find a similar app for iOS) With MIT App Inventor we can create an app free. Here is the link:
http://appinventor.mit.edu/explore/
The rationale behind is pretty simple: Pair the mobile with Bluetooth module. Send "w" to the module when pressing forward button. When the module receives "w", Arduino will instruct the tank moving forward. The App can be downloaded here.
Downloads
Result
After installing this app, go to Setting > Bluetooth > and searching for the device. Do the pairing and input the password. This password is either 0000 or 1234 usually. Then open the App.
You may see the screen with just an button "Connect to Bluetooth". Press it on and select the device. Five direction buttons appear if success. Yeah! Let's start!
Thanks again for watching it. See you.