-
Notifications
You must be signed in to change notification settings - Fork 0
/
bluetoothCar.ino
209 lines (181 loc) · 5.1 KB
/
bluetoothCar.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
Bluetooth Control Car.
The pin configuration for the car is as follows:-
For Motor Controller (l298N):-
Arduino pin 11,10,9,8 pins to l298N Driver pins respectively.
|
V
( put the l298n Driver Such that the ic behind the heat sink is visible to you
so the control pins of l298N is closet to you then attacht 11,10,9,8 sequentially
from left to right in the l298N motor driver)
Arduino pin 5,6 => To the enable A and enable B pin (corner header pins of l298n Driver)
Bluetooth Module connections :-
Gnd => Gnd arduino
5v OR vcc => 5v arduino
Tx of bluetooth module => Rx of arduino.
The Software you need to Run this program is "Arduino car" android application made by "one day of Code".
Also a Python script(using pySerial library) can be used.
You can tune the offset, brake force, speed etc according to your configuration and needs.
*/
// Communication Rate (Must be same for bluetooth module).
#define SERIAL_RATE 9600
// Direction constants.
#define FRONT 'F'
#define BACK 'B'
#define RIGHT 'R'
#define LEFT 'L'
#define STOP 'S'
#define ENABLE_A 5 // Enable motor A for pwm speed control.(pin 5).
#define ENABLE_B 6 // Enable motor B for pwm speed control.(pin 6).
// Motors control pins.
int m1a = 8;
int m1b = 9;
int m2a = 10;
int m2b = 11;
// Initial speed of Motors.
int speed = 100;
// The force at with brake is applied.(time)
int brakeForce = 100;
/*
If one motor runs slower OR the car is tilting in
certain direcction while going forward then change these
offsets to change the speed of each motor so that
the path is straight of the car.
*/
int m1SpeedOffset = 0;
int m2SpeedOffset = 0;
void setup(){
// First we Start with 100 speed.
analogWrite(ENABLE_A, speed + m1SpeedOffset);
analogWrite(ENABLE_B, speed + m2SpeedOffset);
pinMode(m1a, OUTPUT); // Digital pin 10 set as output Pin
pinMode(m1b, OUTPUT); // Digital pin 11 set as output Pin
pinMode(m2a, OUTPUT); // Digital pin 12 set as output Pin
pinMode(m2b, OUTPUT); // Digital pin 13 set as output Pin
Serial.begin(SERIAL_RATE);
}
// Move motor in desired driection.
void moveMotor(char dir){
if(dir == BACK) // Reverse
{
digitalWrite(m1a, HIGH);
digitalWrite(m1b, LOW);
digitalWrite(m2a, LOW);
digitalWrite(m2b, HIGH);
return;
}
if(dir == FRONT) // Forward
{
digitalWrite(m1a, LOW);
digitalWrite(m1b, HIGH);
digitalWrite(m2a, HIGH);
digitalWrite(m2b, LOW);
return;
}
if(dir == RIGHT) //Right
{
digitalWrite(m1a, LOW);
digitalWrite(m1b, HIGH);
digitalWrite(m2a, LOW);
digitalWrite(m2b, HIGH);
return;
}
if(dir == LEFT) //Left
{
digitalWrite(m1a, HIGH);
digitalWrite(m1b, LOW);
digitalWrite(m2a, HIGH);
digitalWrite(m2b, LOW);
return;
}
if(dir == STOP) //Stop
{
brakeMotor();
return;
}
// For Extra and more controll.
if(dir == 'I') //Backward Left
{
digitalWrite(m1a, HIGH);
digitalWrite(m1b, LOW);
digitalWrite(m2a, LOW);
digitalWrite(m2b, LOW);
return;
}
if(dir == 'J') //Forward Left
{
digitalWrite(m1a, LOW);
digitalWrite(m1b, HIGH);
digitalWrite(m2a, LOW);
digitalWrite(m2b, LOW);
return;
}
if(dir == 'G') //Backward Right
{
digitalWrite(m1a, LOW);
digitalWrite(m1b, LOW);
digitalWrite(m2a, LOW);
digitalWrite(m2b, HIGH);
return;
}
if(dir == 'H') //Forward Right
{
digitalWrite(m1a, LOW);
digitalWrite(m1b, LOW);
digitalWrite(m2a, HIGH);
digitalWrite(m2b, LOW);
return;
}
//SPEED increment and decrement using X and Y keys.
if(dir == 'X' && (speed + m2SpeedOffset) <= 240 && (speed + m1SpeedOffset) <= 240){
speed += 10;
//Serial.println(speed); // UnComment if you want serial output on screen.
return;
}
if(dir == 'Y' && speed >= 15){
speed -= 10;
//Serial.println(speed); // UnComment if you want serial output on screen.
return;
}
/*
// Code for slider to work and change speed.
// Work in progress.
if (dir == "M") {
speed=180;
int temp = 0;
int val=1;
for (int x = 0; val > 0; x++) {
temp = val + 10 *temp;
val = Serial.read();
}
speed = temp;
Serial.println(speed);
Serial.println(speed);
return;
}
*/
return;
}
void brakeMotor(){
// Electronic Braking System implemented.
// Each time, in the Arduino car app the button is released after pressing 'S' is sent and break is applied.
moveMotor(BACK);
delay(brakeForce);
//Stop Motors.
digitalWrite(m1a, LOW);
digitalWrite(m1b, LOW);
digitalWrite(m2a, LOW);
digitalWrite(m2b, LOW);
return;
}
void loop(){
char val; // Value Varaible for recieved value.
while(Serial.available() > 0){
// Changing speed.
analogWrite(ENABLE_A, speed + m1SpeedOffset);
analogWrite(ENABLE_B, speed + m2SpeedOffset);
val = Serial.read();
moveMotor(val);
//Serial.println(val); // UnComment if you want serial output on screen.
}
}