Arduino Projects
Overview
Arduino projects
Bluetooth Car
Item required
- Arduino Uno
- L293D Motor Driver
- Motor & Wheels
- Bluetooth Module HS-06
- Lithium Battery
- Battery Holder
Install Arduino IDE & install the "Adafruit Motor Shield v1" library
Install "Serial Bluetooth Terminal" android/ios app, connect to bluetooth and send text 'F' & 'S'
Install Arduino Bluetooth Controller on the phone and control the car
https://play.google.com/store/apps/details?id=com.giristudio.hc05.bluetooth.arduino.control
1#include <AFMotor.h>
2#include <SoftwareSerial.h>
3
4SoftwareSerial bluetoothSerial(9, 10); // RX, TX
5
6AF_DCMotor motor1(1, MOTOR12_1KHZ);
7AF_DCMotor motor2(2, MOTOR12_1KHZ);
8AF_DCMotor motor3(3, MOTOR34_1KHZ);
9AF_DCMotor motor4(4, MOTOR34_1KHZ);
10
11char command;
12int speed = 180;
13int speed_reducing_factor = 3;
14
15void setup() {
16 Serial.begin(9600);
17 bluetoothSerial.begin(9600);
18
19 Serial.println("Waiting for Bluetooth data...");
20}
21
22void loop() {
23 if (bluetoothSerial.available()) {
24 command = bluetoothSerial.read();
25 Serial.print("Received: ");
26 Serial.println(command);
27
28 if (command == 'F') Forward();
29 else if (command == 'B') Backward();
30 else if (command == 'R') TurnRight();
31 else if (command == 'L') TurnLeft();
32 else if (command == 'S') Stop();
33 }
34}
35
36void Forward() {
37 motor1.setSpeed(speed);
38 motor2.setSpeed(speed);
39 motor3.setSpeed(speed);
40 motor4.setSpeed(speed);
41 motor1.run(FORWARD);
42 motor2.run(FORWARD);
43 motor3.run(FORWARD);
44 motor4.run(FORWARD);
45}
46
47void Backward() {
48 motor1.setSpeed(speed);
49 motor2.setSpeed(speed);
50 motor3.setSpeed(speed);
51 motor4.setSpeed(speed);
52 motor1.run(BACKWARD);
53 motor2.run(BACKWARD);
54 motor3.run(BACKWARD);
55 motor4.run(BACKWARD);
56}
57
58void TurnRight() {
59 motor1.setSpeed(speed);
60 motor2.setSpeed(speed);
61 motor3.setSpeed(speed);
62 motor4.setSpeed(speed);
63 motor1.run(FORWARD);
64 motor2.run(FORWARD);
65 motor3.run(BACKWARD);
66 motor4.run(BACKWARD);
67}
68
69void TurnLeft() {
70 motor1.setSpeed(speed);
71 motor2.setSpeed(speed);
72 motor3.setSpeed(speed);
73 motor4.setSpeed(speed);
74 motor1.run(BACKWARD);
75 motor2.run(BACKWARD);
76 motor3.run(FORWARD);
77 motor4.run(FORWARD);
78}
79
80void Stop() {
81 motor1.run(RELEASE);
82 motor2.run(RELEASE);
83 motor3.run(RELEASE);
84 motor4.run(RELEASE);
85}
Reference
comments powered by Disqus