-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctions.h
448 lines (413 loc) · 13 KB
/
Functions.h
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
////////////////////////////////////////////////////////////////////////////
// Titlu proiect: Smart Car (Radio control) //
// Autor: Aninu //
// Versiune: V 2.0 //
// URL: https://aninu.xyz/post/smart-car-v1/ //
// GitHub: https://github.com/aaninu/Arduino_Car //
////////////////////////////////////////////////////////////////////////////
/** Include librariile necesare. */
#include "Settings.h"
#include "Radio.h"
//////////////////////////////////////////////////////////////////////
void DebugMode_Setup();
void DebugMode_Msg(String text);
void DebugMode_Msg(String text, int valoare);
void DebugMode_Msg(String text, bool valoare);
void DebugMode_Msg(String text, char* valoare);
void DebugMode_Msg(String text, String valoare);
void DebugMode_Serial();
void Radio_Decode_Variable(String Variabila, bool Value);
void Radio_DecodeInt_Value(String sVariabila, int iValoare);
void LEDs_Setup();
void Control_LED_Pozitii();
void Control_LED_FazaLunga();
void Control_LED_Stop();
void System_LED_LeftRight();
void Control_LED_Left();
void Control_LED_Right();
void Control_LED_Avarii();
void Motor_Setup();
void Control_Motor_Top();
void Control_Motor_Bottom();
void Control_Motor_Left(bool status);
void Control_Motor_Right(bool status);
void Control_Motor_Full_OFF();
void Buzzer_Setup();
void Control_Buzzer();
//////////////////////////////////////////////////////////////////////
/** Se foloseste pentru afisarea mesajelor de debug */
void DebugMode_Setup(){
if (DebugMode){
Serial.begin(9600);
DebugMode_Msg("DebugMode_Setup() ...");
}
}
void DebugMode_Msg(String text){
if (DebugMode){
Serial.print("[DebugMode]: ");
Serial.println(text);
}
}
void DebugMode_Msg(String text, int valoare){
if (DebugMode){
Serial.print("[DebugMode]: ");
Serial.print(text);
Serial.print(" {");
Serial.print(valoare);
Serial.println("}");
}
}
void DebugMode_Msg(String text, bool valoare){
if (DebugMode){
Serial.print("[DebugMode]: ");
Serial.print(text);
Serial.print(" {");
Serial.print(valoare);
Serial.println("}");
}
}
void DebugMode_Msg(String text, char* valoare){
if (DebugMode){
Serial.print("[DebugMode]: ");
Serial.print(text);
Serial.print(" {");
Serial.print(valoare);
Serial.println("}");
}
}
void DebugMode_Msg(String text, String valoare){
if (DebugMode){
Serial.print("[DebugMode]: ");
Serial.print(text);
Serial.print(" {");
Serial.print(valoare);
Serial.println("}");
}
}
/** Init Serial Comunication */
void DebugMode_Serial(){
if (Serial.available() > 0){
DebugSerialValue = Serial.read();
DebugMode_Msg("Serial.read():", DebugSerialValue);
}
}
/** Radio Var/Val Received */
void Radio_Decode_Variable(String Variabila, bool Value){
if (Variabila.equals(COD_LED_Pozitii)){
LED_Pozitii = Value;
DebugMode_Msg("Radio_Decode_Variable(...): [COD_LED_Pozitii]", Value);
}else if (Variabila.equals(COD_LED_FazaLunga)){
LED_FazaLunga = Value;
DebugMode_Msg("Radio_Decode_Variable(...): [COD_LED_FazaLunga]", Value);
}else if (Variabila.equals(COD_LED_Left)){
LED_Left = Value;
DebugMode_Msg("Radio_Decode_Variable(...): [COD_LED_Left]", Value);
}else if (Variabila.equals(COD_LED_Right)){
LED_Right = Value;
DebugMode_Msg("Radio_Decode_Variable(...): [COD_LED_Right]", Value);
}else if (Variabila.equals(COD_LED_Avarii)){
LED_Avarii = Value;
DebugMode_Msg("Radio_Decode_Variable(...): [COD_LED_Avarii]", Value);
}else if (Variabila.equals(COD_Motor_Top)){
Motor_Top = Value;
DebugMode_Msg("Radio_Decode_Variable(...): [COD_Motor_Top]", Value);
}else if (Variabila.equals(COD_Motor_Bottom)){
Motor_Bottom = Value;
DebugMode_Msg("Radio_Decode_Variable(...): [COD_Motor_Bottom]", Value);
}else{
DebugMode_Msg("Radio_Decode_Variable(...):", Variabila);
}
}
/** Decode int value */
void Radio_DecodeInt_Value(String sVariabila, int iValoare){
if (sVariabila == COD_Motor_Servo){
if (iValoare < 400){
Motor_Right = false;
Motor_Left = true;
Control_Motor_Left(true);
}else if (iValoare > 600){
Motor_Left = false;
Motor_Right = true;
Control_Motor_Right(true);
}else{
Motor_Left = false;
Motor_Right = false;
Control_Motor_Left(false);
Control_Motor_Right(false);
}
}else{
DebugMode_Msg("Radio_DecodeInt_Value(...):" + sVariabila + ": ", iValoare);
}
}
/** Setup PIN Leds */
void LEDs_Setup(){
pinMode(PIN_LED_PozitieFata, OUTPUT);
pinMode(PIN_LED_FazaLunga, OUTPUT);
pinMode(PIN_LED_GoDown, OUTPUT);
pinMode(PIN_LED_PozitieSpate, OUTPUT);
pinMode(PIN_LED_Stop, OUTPUT);
pinMode(PIN_LED_Stanga, OUTPUT);
pinMode(PIN_LED_Dreapta, OUTPUT);
DebugMode_Msg("LEDs_Setup() ...");
}
/** Control LED pozitii */
void Control_LED_Pozitii(){
if (LED_Pozitii != oLED_Pozitii){
digitalWrite(PIN_LED_PozitieFata, LED_Pozitii);
digitalWrite(PIN_LED_PozitieSpate, LED_Pozitii);
oLED_Pozitii = LED_Pozitii;
}
}
/** Control LED pozitii */
void Control_LED_FazaLunga(){
if (oLED_FazaLunga != LED_FazaLunga){
digitalWrite(PIN_LED_FazaLunga, LED_FazaLunga);
oLED_FazaLunga = LED_FazaLunga;
}
}
/** Control LED Stop */
void Control_LED_Stop(){
if (oLED_Stop != LED_Stop){
digitalWrite(PIN_LED_Stop, LED_Stop);
digitalWrite(PIN_LED_Stop, LED_Stop);
oLED_Stop = LED_Stop;
}
}
/** LED Left / Right Control Timed */
void System_LED_LeftRight(){
if (LED_Avarii == true){
if (LED_Semnal_Timer == 0){
digitalWrite(PIN_LED_Stanga, true);
digitalWrite(PIN_LED_Dreapta, true);
}else if (LED_Semnal_Timer == 500){
digitalWrite(PIN_LED_Stanga, false);
digitalWrite(PIN_LED_Dreapta, false);
}else if (LED_Semnal_Timer == 1000)
LED_Semnal_Timer = -1;
LED_Semnal_Timer += 1;
}else if (LED_Left == true){
if (LED_Semnal_Timer == 0){
digitalWrite(PIN_LED_Stanga, true);
}else if (LED_Semnal_Timer == 300){
digitalWrite(PIN_LED_Stanga, false);
}else if (LED_Semnal_Timer == 600)
LED_Semnal_Timer = -1;
LED_Semnal_Timer += 1;
}else if (LED_Right == true){
if (LED_Semnal_Timer == 0){
digitalWrite(PIN_LED_Dreapta, true);
}else if (LED_Semnal_Timer == 300){
digitalWrite(PIN_LED_Dreapta, false);
}else if (LED_Semnal_Timer == 600)
LED_Semnal_Timer = -1;
LED_Semnal_Timer += 1;
}else{
LED_Semnal_Timer = 0;
}
}
/** Control LED Left */
void Control_LED_Left(){
if (oLED_Left != LED_Left){
LED_Right = false;
if (LED_Left == false) digitalWrite(PIN_LED_Stanga, false);
oLED_Left = LED_Left;
}
}
/** Control LED Right */
void Control_LED_Right(){
if (oLED_Right != LED_Right){
LED_Left = false;
if (LED_Right == false) digitalWrite(PIN_LED_Dreapta, false);
oLED_Right = LED_Right;
}
}
/** Control LED Avarii */
void Control_LED_Avarii(){
if (oLED_Avarii != LED_Avarii){
LED_Left = false;
LED_Right = false;
if (LED_Avarii == false){
digitalWrite(PIN_LED_Stanga, false);
digitalWrite(PIN_LED_Dreapta, false);
}
oLED_Avarii = LED_Avarii;
}
}
/** Se initializeaza pinii pentru motoare. */
void Motor_Setup(){
pinMode(PIN_Motor_A_Top, OUTPUT);
pinMode(PIN_Motor_A_Bottom, OUTPUT);
pinMode(PIN_Motor_B_Top, OUTPUT);
pinMode(PIN_Motor_B_Bottom, OUTPUT);
pinMode(PIN_Motor_C_Top, OUTPUT);
pinMode(PIN_Motor_C_Bottom, OUTPUT);
pinMode(PIN_Motor_D_Top, OUTPUT);
pinMode(PIN_Motor_D_Bottom, OUTPUT);
DebugMode_Msg("Motor_Setup() ...");
}
/** Se foloseste pentru deplasarea masinii inainte. */
void Control_Motor_Top(){
if (oMotor_Top != Motor_Top && !Motor_Left && !Motor_Right){
digitalWrite(PIN_Motor_A_Top, Motor_Top);
digitalWrite(PIN_Motor_B_Top, Motor_Top);
digitalWrite(PIN_Motor_C_Top, Motor_Top);
digitalWrite(PIN_Motor_D_Top, Motor_Top);
LED_Stop = false;
/* Se opresc motoarele pentru deplasarea masinii in spate. */
digitalWrite(PIN_Motor_A_Bottom, false);
digitalWrite(PIN_Motor_B_Bottom, false);
digitalWrite(PIN_Motor_C_Bottom, false);
digitalWrite(PIN_Motor_D_Bottom, false);
digitalWrite(PIN_LED_GoDown, false);
Buzzer_Status = false;
digitalWrite(PIN_Buzzer, false);
oMotor_Top = Motor_Top;
}
}
/** Se foloseste pentru deplasarea masinii in spate. */
void Control_Motor_Bottom(){
if (oMotor_Bottom != Motor_Bottom && !Motor_Left && !Motor_Right){
digitalWrite(PIN_Motor_A_Bottom, Motor_Bottom);
digitalWrite(PIN_Motor_B_Bottom, Motor_Bottom);
digitalWrite(PIN_Motor_C_Bottom, Motor_Bottom);
digitalWrite(PIN_Motor_D_Bottom, Motor_Bottom);
digitalWrite(PIN_LED_GoDown, Motor_Bottom);
Buzzer_Status = Motor_Bottom;
LED_Stop = false;
/* Se opresc motoarele pentru deplasarea masinii inainte. */
digitalWrite(PIN_Motor_A_Top, false);
digitalWrite(PIN_Motor_B_Top, false);
digitalWrite(PIN_Motor_C_Top, false);
digitalWrite(PIN_Motor_D_Top, false);
oMotor_Bottom = Motor_Bottom;
}
}
/** Se foloseste pentru rotirea masinii la stanga */
void Control_Motor_Left(bool status){
if (status == true){
digitalWrite(PIN_Motor_A_Bottom, true);
digitalWrite(PIN_Motor_C_Bottom, true);
digitalWrite(PIN_Motor_B_Top, true);
digitalWrite(PIN_Motor_D_Top, true);
DebugMode_Msg("Control_Motor_Right(true): sunt active motoarele pentru rotirea la stanga.");
}else{
Control_Motor_Full_OFF();
}
}
/** Se foloseste pentru rotirea masinii la dreapta */
void Control_Motor_Right(bool status){
if (status == true){
digitalWrite(PIN_Motor_B_Bottom, true);
digitalWrite(PIN_Motor_D_Bottom, true);
digitalWrite(PIN_Motor_A_Top, true);
digitalWrite(PIN_Motor_C_Top, true);
DebugMode_Msg("Control_Motor_Right(true): sunt active motoarele pentru rotirea la dreapta.");
}else{
Control_Motor_Full_OFF();
}
}
/** Se foloseste pentru a opri toate motoarele. */
void Control_Motor_Full_OFF(){
digitalWrite(PIN_Motor_A_Top, false);
digitalWrite(PIN_Motor_B_Top, false);
digitalWrite(PIN_Motor_C_Top, false);
digitalWrite(PIN_Motor_D_Top, false);
digitalWrite(PIN_Motor_A_Bottom, false);
digitalWrite(PIN_Motor_B_Bottom, false);
digitalWrite(PIN_Motor_C_Bottom, false);
digitalWrite(PIN_Motor_D_Bottom, false);
DebugMode_Msg("Control_Motor_Full_OFF(): toate motoarele au fost setate ca fiind oprite.");
}
/** Se initializeaza PIN-ul pentru buzzer */
void Buzzer_Setup(){
pinMode(PIN_Buzzer, OUTPUT);
DebugMode_Msg("Buzzer_Setup() ...");
}
/** Se foloseste pentru a controla sunetul emis de buzzer */
void Control_Buzzer(){
if (Buzzer_Status == true){
if (Buzzer_Timer == 0){
digitalWrite(PIN_Buzzer, true);
}else if(Buzzer_Timer == 250){
digitalWrite(PIN_Buzzer, false);
}else if (Buzzer_Timer == 600){
Buzzer_Timer = -1;
}
Buzzer_Timer += 1;
}else{
Buzzer_Timer = 0;
digitalWrite(PIN_Buzzer, false);
}
}
// Init Sensor Top PIN
void Setup_SensorTop(){
// Init Sensor
pinMode(PIN_ST_echo, INPUT);
pinMode(PIN_ST_trig, OUTPUT);
DebugMode_Msg("Setup_SensorTop() ...");
}
// Init Sensor Bottom PIN
void Setup_SensorBottom(){
// Init Sensor
pinMode(PIN_SB_echo, INPUT);
pinMode(PIN_SB_trig, OUTPUT);
DebugMode_Msg("Setup_SensorBottom() ...");
}
/** Se foloseste pentru a opri masina automat daca sunt obstacole la mersul inainte. */
void Sensor_Top(){
if (Motor_Top && Sensor_TOP_STATUS){
long duration, distance;
digitalWrite(PIN_ST_trig, LOW);
delayMicroseconds(2);
digitalWrite(PIN_ST_trig, HIGH);
delayMicroseconds(10);
digitalWrite(PIN_ST_trig, LOW);
duration = pulseIn(PIN_ST_echo, HIGH);
distance = (duration/2) / 29.1;
// Check condition for stop car
if (distance <= 20){
Serial.println("[DebugMODE]: Sensor_Top(): failsafe [<20].");
Motor_Top = false;
Motor_Bottom = false;
Motor_Left = false;
Motor_Right = false;
LED_Stop = true;
Buzzer_Status = false;
Control_Motor_Full_OFF();
}
if (DebugMODE_ST){
Serial.print("[DebugMODE.ST]: ");
Serial.print(distance);
Serial.println(" cm");
}
}
}
/** Se foloseste pentru a opri masina automat daca sunt obstacole la mersul in spate. */
void Sensor_Bottom(){
if (Motor_Bottom && Sensor_BOTTOM_STATUS){
long duration, distance;
digitalWrite(PIN_SB_trig, LOW);
delayMicroseconds(2);
digitalWrite(PIN_SB_trig, HIGH);
delayMicroseconds(10);
digitalWrite(PIN_SB_trig, LOW);
duration = pulseIn(PIN_SB_echo, HIGH);
distance = (duration/2) / 29.1;
// Check condition for stop car
if (distance <= 20){
Serial.println("[DebugMODE]: Sensor_Bottom(): failsafe [<20].");
Motor_Top = false;
Motor_Bottom = false;
Motor_Left = false;
Motor_Right = false;
LED_Stop = true;
Buzzer_Status = false;
Control_Motor_Full_OFF();
}
if (DebugMODE_SB){
Serial.print("[DebugMODE.SB]: ");
Serial.print(distance);
Serial.println(" cm");
}
}
}