Components and supplies
SH-HC-08 Bluetooth 4.0 BLE Module
L298N Motor Driver Dual H-Bridge
18650 Li-on Battery
Jumper wires (generic)
Rocker Switch, SPST
Battery Holder, 18650 x 2
SG90 Micro-servo motor
Ultrasonic Sensor - HC-SR04 (Generic)
DC 3V - 6V gear motor and wheel
Acrylic Sheet
Arduino Mega 2560
Tools and machines
Soldering iron (generic)
Hot glue gun (generic)
Apps and platforms
Arduino IDE
ArduinoBlue
Project description
Code
Code
arduino
1// Libraries 2#include <ArduinoBlue.h> 3#include <Servo.h> 4 5// Ultrasonic Sensor 6#define TRIGGER 23 7#define ECHO 25 8int distance; 9 10// Bluetooth data 11int prevThrottle = 49; 12int prevSteering = 49; 13int throttle, steering, sliderVal, sliderId, button; 14int automotive = 3; // starting in steering mode 15 16ArduinoBlue phone(Serial1); // pass reference of bluetooth object to ArduinoBlue constructor. 17 18// Engines 19int ENA = 3; 20int IN1 = 2; 21int IN2 = 4; 22int IN3 = 7; 23int IN4 = 8; 24int ENB = 9; 25 26// Velocities 27int acc = 80 + 175/2; 28int spin = 115; 29int turn; 30 31// Servo 32Servo servo; 33int angle = 35; 34 35void setup() { 36 Serial.begin(9600); 37 Serial1.begin(9600); 38 // delay just in case bluetooth module needs time to "get ready". 39 delay(100); 40 Serial.println("setup complete"); 41 pinMode(ENA, OUTPUT); 42 pinMode(IN1, OUTPUT); 43 pinMode(IN2, OUTPUT); 44 pinMode(IN3, OUTPUT); 45 pinMode(IN4, OUTPUT); 46 pinMode(ENB, OUTPUT); 47 48 // Ultrasonic 49 pinMode(ECHO, INPUT); 50 pinMode(TRIGGER, OUTPUT); 51 digitalWrite(TRIGGER, LOW); 52 53 // Servo 54 servo.attach(27); 55 servo.write(angle); 56 57} 58 59 60void loop() { 61 62 // get bluetooth data 63 throttle = phone.getThrottle(); 64 steering = phone.getSteering(); 65 button = phone.getButton(); 66 // ID of the slider moved. In this timegap sliderVal accessible. 67 sliderId = phone.getSliderId(); 68 // Slider value goes from 0 to 200. 69 sliderVal = phone.getSliderVal(); 70 71 // changing the velocity depending on throttle slider position 72 if (sliderId == 1) { 73 Serial.print("Slider ID: "); 74 Serial.print(sliderId); 75 Serial.print("\ Value: "); 76 Serial.println(sliderVal); 77 acc = map(sliderVal, 0, 200, 80, 255); 78 } 79 // changing between steering and auto mode 80 if (button != -1) { 81 automotive = button; 82 } 83 // changing turn speed depending on steering slider position 84 if (sliderId == 0) { 85 Serial.print("Slider ID: "); 86 Serial.print(sliderId); 87 Serial.print("\ Value: "); 88 Serial.println(sliderVal); 89 spin = map(sliderVal, 0, 200, 80, 150); 90 } 91 92 // Display throttle and steering data if steering or throttle value is changed (only if usb is attached) 93 if (prevThrottle != throttle || prevSteering != steering) { 94 Serial.print("Throttle: "); Serial.print(throttle); Serial.print("\ Steering: "); Serial.println(steering); 95 prevThrottle = throttle; 96 prevSteering = steering; 97 } 98 99 if (automotive == 3) { 100 drivemode(); // steering mode 101 } 102 if (automotive == 2) { 103 selfdrivemode(); // auto mode 104 } 105 106} 107 108void findDistance() { 109 int duration; 110 digitalWrite(TRIGGER, HIGH); 111 delayMicroseconds(10); 112 digitalWrite(TRIGGER, LOW); 113 114 duration = pulseIn(ECHO,HIGH); 115 distance = duration / 2 / 7.6; 116 delay(60); 117 118} 119 120 121void selfdrivemode() { 122 for (angle=35; angle <= 70; angle += 10){ 123 servo.write(angle); 124 findDistance(); 125 Serial.println(distance); //value 10 equals 1cm obstacle distance 126 if (distance <= 70) { 127 analogWrite(ENA, 100); 128 analogWrite(ENB, 100); 129 right(); 130 delay(800); 131 stopengine(); 132 } 133 delay(15); 134 } 135 136 servo.write(35); 137 138 for (angle=35; angle >= 0; angle -= 10){ 139 servo.write(angle); 140 findDistance(); 141 Serial.println(distance); //value 10 equals 1cm obstacle distance 142 if (distance <= 70) { 143 analogWrite(ENA, 100); 144 analogWrite(ENB, 100); 145 left(); 146 delay(800); 147 stopengine(); 148 } 149 delay(15); 150 } 151 152 servo.write(35); 153 154 findDistance(); 155 Serial.println(distance); //value 10 equals 1cm obstacle distance 156 if (distance > 70) { 157 analogWrite(ENA, 100); 158 analogWrite(ENB, 100); 159 forward(); 160 delay(400); 161 stopengine(); 162 } 163} 164 165void drivemode() { 166 findDistance(); 167 Serial.println(distance); //value 10 equals 1cm obstacle distance 168 if (distance >= 100) { 169 drivesetup(); 170 } 171 else { 172 analogWrite(ENA, 90); 173 analogWrite(ENB, 90); 174 left(); 175 delay(1000); 176 } 177 178} 179 180void drivesetup() { 181 if (throttle > 60) { 182 if (steering > 40 && steering < 60) { 183 analogWrite(ENA, acc); 184 analogWrite(ENB, acc); 185 forward(); 186 } 187 if (steering <= 40 && steering >= 10) { 188 turn = map(steering, 10, 40, 0, acc); 189 analogWrite(ENA, turn); //decrease 190 analogWrite(ENB, acc); //hold 191 forward(); 192 } 193 if (steering >= 60 && steering <= 90) { 194 turn = map(steering, 60, 90, acc, 0); 195 analogWrite(ENA, acc); //hold 196 analogWrite(ENB, turn); //decrease 197 forward(); 198 } 199 if (steering < 10) { 200 analogWrite(ENA, spin); 201 analogWrite(ENB, spin); 202 left(); 203 } 204 if (steering > 90) { 205 analogWrite(ENA, spin); 206 analogWrite(ENB, spin); 207 right(); 208 } 209 } 210 if (throttle < 40) { 211 if (steering > 40 && steering < 60) { 212 analogWrite(ENA, acc); 213 analogWrite(ENB, acc); 214 back(); 215 } 216 if (steering <= 40 && steering >= 25) { 217 turn = map(steering, 25, 40, 0, acc); 218 analogWrite(ENA, turn); //decrease 219 analogWrite(ENB, acc); //hold 220 back(); 221 } 222 if (steering >= 60 && steering <= 75) { 223 turn = map(steering, 60, 75, acc, 0); 224 analogWrite(ENA, acc); //hold 225 analogWrite(ENB, turn); //decrease 226 back(); 227 } 228 if (steering < 10) { 229 analogWrite(ENA, spin); 230 analogWrite(ENB, spin); 231 left(); 232 } 233 if (steering > 90) { 234 analogWrite(ENA, spin); 235 analogWrite(ENB, spin); 236 right(); 237 } 238 } 239 240 if (throttle == 49) { 241 stopengine(); 242 } 243} 244 245void forward() { 246 digitalWrite(IN1, HIGH); 247 digitalWrite(IN2, LOW); 248 digitalWrite(IN3, HIGH); 249 digitalWrite(IN4, LOW); 250} 251 252void back() { 253 digitalWrite(IN1, LOW); 254 digitalWrite(IN2, HIGH); 255 digitalWrite(IN3, LOW); 256 digitalWrite(IN4, HIGH); 257} 258 259void left() { 260 digitalWrite(IN1, LOW); 261 digitalWrite(IN2, HIGH); 262 digitalWrite(IN3, HIGH); 263 digitalWrite(IN4, LOW); 264} 265 266void right() { 267 digitalWrite(IN1, HIGH); 268 digitalWrite(IN2, LOW); 269 digitalWrite(IN3, LOW); 270 digitalWrite(IN4, HIGH); 271} 272 273void stopengine() { 274 digitalWrite(IN1, LOW); 275 digitalWrite(IN2, LOW); 276 digitalWrite(IN3, LOW); 277 digitalWrite(IN4, LOW); 278} 279
Code
arduino
1// Libraries 2#include <ArduinoBlue.h> 3#include <Servo.h> 4 5// 6 Ultrasonic Sensor 7#define TRIGGER 23 8#define ECHO 25 9int distance; 10 11// 12 Bluetooth data 13int prevThrottle = 49; 14int prevSteering = 49; 15int throttle, 16 steering, sliderVal, sliderId, button; 17int automotive = 3; // starting in steering 18 mode 19 20ArduinoBlue phone(Serial1); // pass reference of bluetooth object to 21 ArduinoBlue constructor. 22 23// Engines 24int ENA = 3; 25int IN1 = 2; 26int 27 IN2 = 4; 28int IN3 = 7; 29int IN4 = 8; 30int ENB = 9; 31 32// Velocities 33int 34 acc = 80 + 175/2; 35int spin = 115; 36int turn; 37 38// Servo 39Servo servo; 40int 41 angle = 35; 42 43void setup() { 44 Serial.begin(9600); 45 Serial1.begin(9600); 46 47 // delay just in case bluetooth module needs time to "get ready". 48 delay(100); 49 50 Serial.println("setup complete"); 51 pinMode(ENA, OUTPUT); 52 pinMode(IN1, 53 OUTPUT); 54 pinMode(IN2, OUTPUT); 55 pinMode(IN3, OUTPUT); 56 pinMode(IN4, 57 OUTPUT); 58 pinMode(ENB, OUTPUT); 59 60 // Ultrasonic 61 pinMode(ECHO, 62 INPUT); 63 pinMode(TRIGGER, OUTPUT); 64 digitalWrite(TRIGGER, LOW); 65 66 67 // Servo 68 servo.attach(27); 69 servo.write(angle); 70 71} 72 73 74void 75 loop() { 76 77 // get bluetooth data 78 throttle = phone.getThrottle(); 79 80 steering = phone.getSteering(); 81 button = phone.getButton(); 82 // ID of 83 the slider moved. In this timegap sliderVal accessible. 84 sliderId = phone.getSliderId(); 85 86 // Slider value goes from 0 to 200. 87 sliderVal = phone.getSliderVal(); 88 89 90 // changing the velocity depending on throttle slider position 91 if (sliderId 92 == 1) { 93 Serial.print("Slider ID: "); 94 Serial.print(sliderId); 95 96 Serial.print("\ Value: "); 97 Serial.println(sliderVal); 98 acc = 99 map(sliderVal, 0, 200, 80, 255); 100 } 101 // changing between steering and auto 102 mode 103 if (button != -1) { 104 automotive = button; 105 } 106 // changing 107 turn speed depending on steering slider position 108 if (sliderId == 0) { 109 Serial.print("Slider 110 ID: "); 111 Serial.print(sliderId); 112 Serial.print("\ Value: "); 113 114 Serial.println(sliderVal); 115 spin = map(sliderVal, 0, 200, 80, 150); 116 117 } 118 119 // Display throttle and steering data if steering or throttle value 120 is changed (only if usb is attached) 121 if (prevThrottle != throttle || prevSteering 122 != steering) { 123 Serial.print("Throttle: "); Serial.print(throttle); Serial.print("\ Steering: 124 "); Serial.println(steering); 125 prevThrottle = throttle; 126 prevSteering 127 = steering; 128 } 129 130 if (automotive == 3) { 131 drivemode(); // steering 132 mode 133 } 134 if (automotive == 2) { 135 selfdrivemode(); // auto mode 136 137 } 138 139} 140 141void findDistance() { 142 int duration; 143 digitalWrite(TRIGGER, 144 HIGH); 145 delayMicroseconds(10); 146 digitalWrite(TRIGGER, LOW); 147 148 duration 149 = pulseIn(ECHO,HIGH); 150 distance = duration / 2 / 7.6; 151 delay(60); 152 153} 154 155 156void 157 selfdrivemode() { 158 for (angle=35; angle <= 70; angle += 10){ 159 servo.write(angle); 160 161 findDistance(); 162 Serial.println(distance); //value 10 equals 1cm obstacle 163 distance 164 if (distance <= 70) { 165 analogWrite(ENA, 100); 166 analogWrite(ENB, 167 100); 168 right(); 169 delay(800); 170 stopengine(); 171 } 172 173 delay(15); 174 } 175 176 servo.write(35); 177 178 for (angle=35; angle 179 >= 0; angle -= 10){ 180 servo.write(angle); 181 findDistance(); 182 Serial.println(distance); 183 //value 10 equals 1cm obstacle distance 184 if (distance <= 70) { 185 analogWrite(ENA, 186 100); 187 analogWrite(ENB, 100); 188 left(); 189 delay(800); 190 191 stopengine(); 192 } 193 delay(15); 194 } 195 196 servo.write(35); 197 198 199 findDistance(); 200 Serial.println(distance); //value 10 equals 1cm obstacle 201 distance 202 if (distance > 70) { 203 analogWrite(ENA, 100); 204 analogWrite(ENB, 205 100); 206 forward(); 207 delay(400); 208 stopengine(); 209 } 210} 211 212void 213 drivemode() { 214 findDistance(); 215 Serial.println(distance); //value 10 equals 216 1cm obstacle distance 217 if (distance >= 100) { 218 drivesetup(); 219 } 220 221 else { 222 analogWrite(ENA, 90); 223 analogWrite(ENB, 90); 224 left(); 225 226 delay(1000); 227 } 228 229} 230 231void drivesetup() { 232 233 if (throttle > 60) { 234 if (steering > 40 && steering < 60) { 235 analogWrite(ENA, 236 acc); 237 analogWrite(ENB, acc); 238 forward(); 239 } 240 if (steering 241 <= 40 && steering >= 10) { 242 turn = map(steering, 10, 40, 0, acc); 243 analogWrite(ENA, 244 turn); //decrease 245 analogWrite(ENB, acc); //hold 246 forward(); 247 248 } 249 if (steering >= 60 && steering <= 90) { 250 turn = map(steering, 251 60, 90, acc, 0); 252 analogWrite(ENA, acc); //hold 253 analogWrite(ENB, 254 turn); //decrease 255 forward(); 256 } 257 if (steering < 10) { 258 analogWrite(ENA, 259 spin); 260 analogWrite(ENB, spin); 261 left(); 262 } 263 if (steering 264 > 90) { 265 analogWrite(ENA, spin); 266 analogWrite(ENB, spin); 267 right(); 268 269 } 270 } 271 if (throttle < 40) { 272 if (steering > 40 && steering 273 < 60) { 274 analogWrite(ENA, acc); 275 analogWrite(ENB, acc); 276 back(); 277 278 } 279 if (steering <= 40 && steering >= 25) { 280 turn = map(steering, 281 25, 40, 0, acc); 282 analogWrite(ENA, turn); //decrease 283 analogWrite(ENB, 284 acc); //hold 285 back(); 286 } 287 if (steering >= 60 && steering <= 288 75) { 289 turn = map(steering, 60, 75, acc, 0); 290 analogWrite(ENA, acc); 291 //hold 292 analogWrite(ENB, turn); //decrease 293 back(); 294 } 295 296 if (steering < 10) { 297 analogWrite(ENA, spin); 298 analogWrite(ENB, 299 spin); 300 left(); 301 } 302 if (steering > 90) { 303 analogWrite(ENA, 304 spin); 305 analogWrite(ENB, spin); 306 right(); 307 } 308 } 309 310 311 if (throttle == 49) { 312 stopengine(); 313 } 314} 315 316void forward() { 317 318 digitalWrite(IN1, HIGH); 319 digitalWrite(IN2, LOW); 320 digitalWrite(IN3, HIGH); 321 322 digitalWrite(IN4, LOW); 323} 324 325void back() { 326 digitalWrite(IN1, LOW); 327 328 digitalWrite(IN2, HIGH); 329 digitalWrite(IN3, LOW); 330 digitalWrite(IN4, HIGH); 331} 332 333void 334 left() { 335 digitalWrite(IN1, LOW); 336 digitalWrite(IN2, HIGH); 337 digitalWrite(IN3, 338 HIGH); 339 digitalWrite(IN4, LOW); 340} 341 342void right() { 343 digitalWrite(IN1, 344 HIGH); 345 digitalWrite(IN2, LOW); 346 digitalWrite(IN3, LOW); 347 digitalWrite(IN4, 348 HIGH); 349} 350 351void stopengine() { 352 digitalWrite(IN1, LOW); 353 digitalWrite(IN2, 354 LOW); 355 digitalWrite(IN3, LOW); 356 digitalWrite(IN4, LOW); 357} 358
Downloadable files
Wire circuit
Wire circuit
Comments
Only logged in users can leave comments
Smartphone Controlled Arduino Car with Auto-Mode | Arduino Project Hub
Anonymous user
4 years ago
I would like to know the demonstration video and circuit diagram. my e-mail adress is bin19971125@gmail.com