-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbb_car_arduino.ino
326 lines (288 loc) · 8.21 KB
/
bb_car_arduino.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
BB Car made with love by Blueberry.io
*/
/*
Ultrasonic
*/
const int trigFrontPin = 12;
const int echoFrontPin = 13;
const int trigBackPin = 8;
const int echoBackPin = 9;
const int ultrasonicTimeout = 120 * 58;// Max.Distance(cm) * 58
const int ultrasonicMeasureLength = (ultrasonicTimeout / 1000) * 2; // 2 stands for back and front
long measureDuration;
void ultrasonicSetup() {
pinMode(trigFrontPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoFrontPin, INPUT); // Sets the echoPin as an Input
pinMode(trigBackPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoBackPin, INPUT); // Sets the echoPin as an Input
}
int measureDistanceFront() {
// Clears the trigPin
digitalWrite(trigFrontPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigFrontPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigFrontPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
measureDuration = pulseIn(echoFrontPin, HIGH, ultrasonicTimeout);
// Calculating the distance
if ( measureDuration == 0 ) {
measureDuration = ultrasonicTimeout;
}
return measureDuration*0.034/2;
}
int measureDistanceBack() {
// Clears the trigPin
digitalWrite(trigBackPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigBackPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigBackPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
measureDuration = pulseIn(echoBackPin, HIGH, ultrasonicTimeout);
// Calculating the distance
if ( measureDuration == 0 ) {
measureDuration = ultrasonicTimeout;
}
return measureDuration*0.034/2;
}
/*
Bluetooth
*/
#include <SoftwareSerial.h>
int bluetoothRxPin = 11;
int bluetoothTxPin = 10;
SoftwareSerial bluetooth(bluetoothTxPin, bluetoothRxPin);
void bluetoothSetup() {
bluetooth.begin(9600);
Serial.println(“Arduino zapnuto, test Bluetooth..“);
bluetooth.println(“Arduino zapnuto, test Bluetooth..“);
}
/*
Servo
*/
#include <Servo.h>
int servoAdjustment = 0;
int servoPin = 6;
int angle = 0;
int direction = 1;
int step = 15;
int bluetoothOutput = 1;
int alreadyMeasured = 0;
unsigned long servoPreviousMillis = 0;
int servoMoveInterval = 300;
Servo servo;
void servoSetup() {
servo.attach(servoPin);
}
int getAngle() {
int newAngle = angle+(step*direction);
if(newAngle > 180 || newAngle < 0) {
direction = -1*direction;
return getAngle();
}
return newAngle;
}
void measureDistance() {
unsigned long currentMillis = millis();
if (currentMillis - servoPreviousMillis >= (servoMoveInterval - ultrasonicMeasureLength) && alreadyMeasured == 0) {
alreadyMeasured = 1;
if (bluetoothOutput) {
bluetooth.print(angle + servoAdjustment);
bluetooth.print(” “);
bluetooth.print(measureDistanceFront());
bluetooth.print(” “);
bluetooth.println(measureDistanceBack());
} else {
Serial.print(angle + servoAdjustment);
Serial.print(” “);
Serial.print(measureDistanceFront());
Serial.print(” “);
Serial.println(measureDistanceBack());
}
}
}
void moveServo() {
unsigned long currentMillis = millis();
if (currentMillis - servoPreviousMillis >= servoMoveInterval) {
servoPreviousMillis = currentMillis;
alreadyMeasured = 0;
angle = getAngle();
servo.write(angle);
}
}
/*
LED
*/
int ledPin = LED_BUILTIN;
int ledState = LOW;
unsigned long previousMillis = 0;
const long interval = 1000;
void toggleLed() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
}
void ledSetup() {
pinMode(ledPin, OUTPUT);
}
/*
MOTORS
*/
/*
A0 is referred to as Pin 14
A1 is referred to as Pin 15
A2 is referred to as Pin 16
A3 is referred to as Pin 17
A4 is referred to as Pin 18
A5 is referred to as Pin 19
*/
int dir1PinA = 18;
int dir2PinA = 19;
int speedPinA = 3; // Needs to be a PWM pin to be able to control motor speed
int dir1PinB = 16;
int dir2PinB = 17;
int speedPinB = 5; // Needs to be a PWM pin to be able to control motor speed
int motorA = 0;
int motorB = 0;
String inputString = “”; // a string to hold incoming data
String motorAString = “”; // a string to hold incoming data
String motorBString = “”; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
boolean motorAComplete = false; // whether the string is complete
unsigned long previousMotorMillis = 0;
const long motorInterval = 250;
void motorsSetup() {
pinMode(dir1PinA,OUTPUT);
pinMode(dir2PinA,OUTPUT);
pinMode(speedPinA,OUTPUT);
pinMode(dir1PinB,OUTPUT);
pinMode(dir2PinB,OUTPUT);
pinMode(speedPinB,OUTPUT);
}
void updateMotors() {
analogWrite(speedPinA, abs(motorA));//Sets speed variable via PWM
digitalWrite(dir1PinA, motorA > 0 ? LOW : HIGH);
digitalWrite(dir2PinA, motorA <= 0 ? LOW : HIGH);
analogWrite(speedPinB, abs(motorB));//Sets speed variable via PWM
digitalWrite(dir1PinB, motorB > 0 ? LOW : HIGH);
digitalWrite(dir2PinB, motorB <= 0 ? LOW : HIGH);
}
void stopMotorsWhenNoNewData() {
unsigned long currentMillis = millis();
if (currentMillis - previousMotorMillis >= motorInterval && (motorA != 0 || motorB != 0)) {
motorA = 0;
motorB = 0;
updateMotors();
Serial.println(“Stopping motors”);
}
}
void receiveAndUpdateMotorData() {
unsigned long currentMillis = millis();
if (stringComplete) {
int A = motorAString.toInt();
if (A > -256 && A < 256) {
motorA = A;
}
int B = motorBString.toInt();
if (B > -256 && B < 256) {
motorB = B;
}
Serial.print(“.”);
Serial.println(“A”);
Serial.println(A);
Serial.println(motorA);
Serial.println(“B”);
Serial.println(B);
Serial.println(motorB);
updateMotors();
previousMotorMillis = currentMillis;
// clear the string:
inputString = “”;
motorAString = “”;
motorBString = “”;
stringComplete = false;
}
}
/*
Parsing Input
*/
void readDataSetup() {
Serial.begin(9600);
// reserve 200 bytes for the inputString:
inputString.reserve(200);
motorAString.reserve(5);
motorBString.reserve(5);
}
void parseInputChar(char inChar) {
// add it to the inputString:
if (isDigit(inChar) || inChar == ‘-’) {
// convert the incoming byte to a char and add it to the string:
if (motorAComplete) {
motorBString += (char)inChar;
} else {
motorAString += (char)inChar;
}
}
if (inChar == ' ‘) {
motorAComplete = true;
}
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == ‘\n’) {
motorAComplete = false;
stringComplete = true;
}
}
/*
SETUP & LOOP
*/
void setup() {
readDataSetup();
bluetoothSetup();
ledSetup();
ultrasonicSetup();
servoSetup();
motorsSetup();
}
void loop() {
toggleLed();
bluetoothEvent();
receiveAndUpdateMotorData();
stopMotorsWhenNoNewData();
measureDistance();
moveServo();
}
/*
SerialEvent occurs whenever a new data comes in the
hardware serial RX. This routine is run between each
time loop() runs, so using delay inside loop can delay
response. Multiple bytes of data may be available.
*/
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read();
inputString += inChar;
parseInputChar(inChar);
}
}
void bluetoothEvent() {
while (bluetooth.available() > 0) {
char inChar = (char)bluetooth.read();
inputString += inChar;
parseInputChar(inChar);
}
}