Arduino Bluetooth Controlled Car Using HC-06 Module and MIT App Inventor
This is a beginner-friendly robotics project and is suitable for learning Arduino, Bluetooth communication, and basic motor control.
Devices & Components
1
Arduino Mega 2560 Rev3
1
wheels x 4
1
Mini breadboard - White
2
Battery holder 4x AA with red/black wire
1
L293D motor driver shield
2
On –off switch
1
HC-06 Bluetooth Module
4
Geared DC Motor
Software & Tools
Arduino IDE
1
MIT app inventor
Project description
Code
roboCarCode
cpp
This is the code for the Arduino Mega
1/* 2FYI: 3The data I transmit will be in this form @sum|leftSpeed|rightSpeed# my code removes the @ and # and 4collects the data inside leaving us with sum|leftSpeed|rightSpeed, when i transmit the motor speeds via bluetooth i also transmit a sum, 5eg if leftSped is 15 and rightSped is 113, then the sum is 1+5+1+1+3 = 11, if any of the data changes via transmission 6then the transmitted sum won't correspond to the speed data sum and I will ignore the data. 7 8The data pieces eg sum and leftSpeed and rightSpeed are seperated by '|'. 9 10The left speed and right speeds i get from the bluetooth module which are sent from the phone have a range of 0 to 510, where 255 means stationary 11and 0 is full throttle backwards and 510 is full throttle forwards, you will see how I subtract 255 from my code later 12 13 14 15*/ 16 17 18 19#include <AFMotor.h> //motor sheild library 20#include <math.h> 21 22 23AF_DCMotor M1(1); //motor objects for each wheel 24AF_DCMotor M2(2); 25AF_DCMotor M3(3); 26AF_DCMotor M4(4); 27 28struct Speedies { // this is a data structure which is convenient as i have a left side motor speed and a right side one. 29 float left; 30 float right; 31}; 32 33 34int lSped = 0; //some useful variables 35int rSped = 0; 36int sum = 0; // the sum as mentioned above 37 38 39uint8_t hold1 = 0; // I use these to find the locations of the '|' between the data pieces I send via bluetooth 40uint8_t hold2 = 0; 41 42 43 44//setting up bluetooth comms 45#include <SoftwareSerial.h> 46 47//rx and tx pins for communication between HC-O6 and arduino Mega 48const byte rxPin = 51; 49const byte txPin = 50; 50 51SoftwareSerial BTSerial(rxPin, txPin); 52 53 54 55 56//messaging keys 57const char START = '@'; 58const char END = '#'; 59 60const int TIMEOUT = 20; // this is the max length of the messagebuffer before I decide something is wrong and start listening for the start character again 61 62 63//messaging var 64String messageBuffer = ""; 65bool waitingToStart = true; // waiting to recieve data as we recieved a start byte (for bluetooth comms) 66 67 68 69void setup() { 70 // put your setup code here, to run once: 71 72 pinMode(rxPin, INPUT); 73 pinMode(txPin, OUTPUT); //setting rx and tx pins in the right modes 74 75 //begin Serial comms 76 BTSerial.begin(9600); 77 Serial.begin(9600); 78} 79 80void loop() { 81 // put your main code here, to run repeatedly: 82 83 84 // this bluetooth recieving section of the code is well explained in this bytes n bits video from time 13:50 (https://www.youtube.com/watch?v=jGoKdF8r7as), and I used it for this project 85 char nextData; 86 87 if (BTSerial.available()) { 88 89 if (waitingToStart) { 90 91 do { 92 nextData = BTSerial.read(); 93 } while ((nextData != START) && BTSerial.available()); 94 if (nextData == START) { 95 Serial.println("message starting"); 96 waitingToStart = false; 97 } 98 } 99 100 101 102 if (!waitingToStart && BTSerial.available()) { 103 104 do { 105 nextData = BTSerial.read(); 106 Serial.println(nextData); 107 messageBuffer += nextData; 108 } while ((nextData != END) && BTSerial.available()); 109 } 110 111 112 113 if (nextData == END) { 114 messageBuffer = messageBuffer.substring(0, messageBuffer.length() - 1); 115 Serial.print("message complete "); 116 Serial.println(messageBuffer); 117 118 speedSet(extractInfo(messageBuffer)); //function to actually process bluetooth messageBuffer and then setMotor Speeds 119 120 121 122 123 124 messageBuffer = ""; 125 waitingToStart = true; 126 } 127 128 if (messageBuffer.length() > TIMEOUT) { 129 Serial.println("timeout error - " + messageBuffer); 130 messageBuffer = ""; 131 waitingToStart = true; 132 } 133 } 134} 135 136 137 138 139int sumDigits(int n) { // this is my function to add the digits in a number like 235 to get 2+3+5 = 10, which i use for error control 140 int sum = 0; 141 while (n > 0) { 142 sum += n % 10; 143 n = n / 10; 144 } 145 return sum; 146} 147 148 149 150 151Speedies extractInfo(String message) { // this function extracts the info from the bluetooth data sent to first check if the data is valid then store it as some variables to send to the speedSet function 152 153 float lSpeed; 154 float rSpeed; 155 156 Speedies info; 157 158 hold1 = message.indexOf('|'); 159 hold2 = message.lastIndexOf('|'); 160 161 sum = message.substring(0, hold1).toInt(); 162 lSped = message.substring((hold1+1), hold2).toInt(); 163 rSped = message.substring((hold2+1), message.length()).toInt(); 164 165 166 if(sum = (sumDigits(lSped) + sumDigits(rSped))){ 167 168 info.left = lSped; 169 info.right = rSped; 170 return info; 171 172 } else{return;} 173 174 175 176 177} 178 179 180void speedSet(Speedies speeda) { // this function uses the extracted bluetooth info then sets the motor speeds 181 182 // i subtract 255 here so i can go from values of 0 tp 510 to -255 to 255, where a negative is backwards 183 184 float rightSpeed = speeda.left-255; //sorry about these two lines of code, they look weird but i just had my left motors swapped with my right ones 185 186 float leftSpeed = speeda.right-255; 187 188 if(leftSpeed < 70 && leftSpeed>-70){ //if the speeds are too low then i set the motor speed to 0 to avoid the motor sounds 189 leftSpeed = 0; 190 } else if(leftSpeed>255){ 191 leftSpeed = 255; 192 } else if(leftSpeed<-255){ // setting max speed limits 193 leftSpeed = -255; 194 } 195 196 if(rightSpeed < 0 && rightSpeed>-70){ 197 rightSpeed = 0; 198 } else if(rightSpeed>255){ 199 rightSpeed = 255; 200 } else if(rightSpeed<-255){ 201 rightSpeed = -255; 202 } 203 204 205 206 if (leftSpeed >= 0) { // here i make negative speeds into backward 207 208 M2.run(FORWARD); 209 M3.run(FORWARD); 210 leftSpeed = leftSpeed; 211 } else { 212 M2.run(BACKWARD); 213 M3.run(BACKWARD); 214 leftSpeed = -1.0 * leftSpeed; 215 } 216 217 if (rightSpeed >= 0) { 218 219 M1.run(FORWARD); 220 M4.run(FORWARD); 221 rightSpeed = rightSpeed; 222 } else { 223 M1.run(BACKWARD); 224 M4.run(BACKWARD); 225 rightSpeed = -1.0 * rightSpeed; 226 } 227 228 M2.setSpeed(round(leftSpeed)); //here i write to the motors and that's all!!! 229 M3.setSpeed(round(leftSpeed)); 230 231 M1.setSpeed(round(rightSpeed)); 232 M4.setSpeed(round(rightSpeed)); 233 234 235}
Comments
Only logged in users can leave comments