PRE-REQUISITE
The app is a reference document for users who have built Arduino enabled car and need the android app to control it over a Bluetooth module. (Eg. HC-06).
In the below example I have configured Arduino Uno with the Ultrasonic Sensor, Bluetooth Module HC-06 and L293D which is basically a collision avoidance car. If you need more information about the schematic let me know in the comments section.
Referenced App: Arduino Bluetooth Controller
SET-UP
- Please make sure you configure and pair your Bluetooth module on your android phone under settings.
- The buttons on the app are preconfigured to send specific codes which you have to configure on your Arduino.
- The Pin setup code is as below
Function
|
Code
|
Forward
|
1
|
Back
|
2
|
Right
|
3
|
Left
|
4
|
Stop
|
5
|
Auto Pilot
|
8
|
Fn1
|
6
|
Fn2
|
7
|
Fn3
|
9
|
- Please find below the code snippet. The code sent from the app can be read by arduino as below. In the below snippet if the received character is 1 then it will process the code block for user defined function "forward()" which sends the instructions to run both the motors in forward direction.
receivedChar = mySerial.read(); if (receivedChar == '1') { forward(); flag = 6; }
- Few things to note here,
- The autopilot function is very basic, you can add a servo and rotate it 180 degrees to determine the best possible path instead of a random path.
- i have provided 3 buttons which do not have any implementation in the sample code ( fn1 till 3) but they are empty slots provided in the app. You can use it in case you want to perform some additional magic with the robot.
- Below is the full example code.
For detailed explanation of the Schemaic Representation please see here
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SoftwareSerial.h> | |
char receivedChar; | |
int motorrightforward = 11; | |
int motorleftforward = 5; | |
int motorrightback = 6; | |
int motorleftback = 3; | |
int DELAY = 100; | |
SoftwareSerial mySerial(8, 7); // RX, TX | |
int trig = 2; | |
int echo = 4; | |
int flag = 6; | |
void setup() | |
{ | |
pinMode(motorrightforward, OUTPUT); | |
pinMode(motorleftforward, OUTPUT); | |
pinMode(motorrightback, OUTPUT); | |
pinMode(motorleftback, OUTPUT); | |
pinMode(trig, OUTPUT); | |
pinMode(echo, INPUT); | |
Serial.begin(9600); | |
mySerial.begin(57600); | |
flag = 6; // flag is used to give a seamless experience while switching between autopilot and manual mode. | |
} | |
void loop() | |
{ | |
mySerial.println(""); | |
while (!mySerial.available() && flag == 6) | |
{ | |
; // does nothing at all. | |
} | |
receivedChar = mySerial.read(); | |
Serial.println("Received: "); | |
Serial.print(receivedChar); | |
if (receivedChar == '1') { | |
forward(); | |
flag = 6; | |
} | |
else if (receivedChar == '2') { | |
backward(); | |
flag = 6; | |
} | |
else if (receivedChar == '3') { | |
right(); | |
flag = 6; | |
} | |
else if (receivedChar == '4') { | |
left(); | |
flag = 6; | |
} | |
else if (receivedChar == '5') { | |
stop(); | |
flag = 6; | |
} | |
else if(receivedChar == '8') // auto pilot) | |
{ | |
flag = 7; | |
// autopilot(); | |
// flag = 'y'; | |
Serial.print(flag); | |
} | |
else if(flag == 7) | |
{ | |
autopilot(); | |
} | |
} | |
int ping() | |
{ | |
long dur, in, cm; | |
digitalWrite(trig, LOW); | |
delayMicroseconds(2); | |
digitalWrite(trig, HIGH); | |
delayMicroseconds(5); | |
digitalWrite(trig, LOW); | |
dur = pulseIn(echo, HIGH); | |
cm = tocms(dur); | |
return (int)cm; | |
} | |
long tocms(long dur) | |
{ | |
return dur / 29 / 2 ; | |
} | |
void autopilot() | |
{ | |
int x = 0; | |
x = ping(); | |
if ( x > 40 || x == 0) | |
{ | |
forward(); | |
} | |
// if the obstacle is less than 40 cms away the car will rotate randomly based on the distance captured | |
// right for even reading and left for odd reading. | |
else | |
if ( x < 40 ) | |
{ | |
if(x%2==0) | |
right(); | |
else | |
left(); | |
} | |
} | |
void forward() | |
{ | |
delay(DELAY); | |
Serial.println(""); | |
Serial.println("Moving forward"); | |
digitalWrite(motorrightforward,HIGH); | |
digitalWrite(motorleftforward,HIGH); | |
digitalWrite(motorrightback,LOW); | |
digitalWrite(motorleftback,LOW); | |
} | |
void backward() | |
{ | |
delay(DELAY); | |
Serial.println(""); | |
Serial.println("Moving back"); | |
digitalWrite(motorrightforward,LOW); | |
digitalWrite(motorleftforward,LOW); | |
digitalWrite(motorrightback,HIGH); | |
digitalWrite(motorleftback,HIGH); | |
} | |
void left() | |
{ | |
Serial.println(""); | |
Serial.println("Turning LEFT"); | |
digitalWrite(motorrightforward,HIGH); | |
digitalWrite(motorleftforward,LOW); | |
digitalWrite(motorrightback,LOW); | |
digitalWrite(motorleftback,HIGH); | |
} | |
void right() | |
{ | |
Serial.println(""); | |
Serial.println("Turning Right"); | |
digitalWrite(motorrightforward,LOW); | |
digitalWrite(motorleftforward,HIGH); | |
digitalWrite(motorrightback,HIGH); | |
digitalWrite(motorleftback,LOW); | |
} | |
void stop() | |
{ | |
Serial.println(""); | |
Serial.println("Stopping"); | |
digitalWrite(motorrightforward,LOW); | |
digitalWrite(motorleftforward,LOW); | |
digitalWrite(motorrightback,LOW); | |
digitalWrite(motorleftback,LOW); | |
} |
Thanks for sharing. This is great. I'm fairly new to the arduino world so i've been having some difficulty programming the ultrasonic to work while implementing a remote control feature. This worked like a charm. Thanks again!
ReplyDeleteThanks for sharing. This is great. I'm fairly new to the arduino world so i've been having some difficulty programming the ultrasonic to work while implementing a remote control feature. This worked like a charm. Thanks again!
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteI did not get your question. Can you come again?
Deletenot sure if this would help but here is the link with the detailed schematics
Deletehttps://robust-tech.blogspot.com/2016/03/arduino-bluetooth-controller-detailed.html