This repository has been archived by the owner on Jan 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
codeMal.ino
483 lines (403 loc) · 10.4 KB
/
codeMal.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
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#define JUST_SURVIVE
//#define ROLL_ROLL_ROLL_ATTACK
//#define RANDON_SEARCH
#define buzzer 3
#define btn_start 12
#define in4 4
#define enB 5
#define in3 6
#define in2 7
#define in1 8
#define enA 9
#define linha_direita A1
#define linha_esquerda A0
#define trig 10
#define echo 11
/////////////////////////////////////////////////
// Implementacao de um timer atraves do millis //
/////////////////////////////////////////////////
class Timer {
private:
unsigned long _actual_time;
unsigned long _waited_time;
bool _running;
uint32_t _interval;
public:
bool elapsed;
Timer(uint32_t interval = 1000) {
_interval = interval;
elapsed = false;
_actual_time = millis();
_waited_time = _actual_time + interval;
}
void setInterval(uint32_t interval) {
_interval = interval;
}
uint32_t getInterval() {
return _interval;
}
void start() {
_running = true;
_actual_time = millis();
_waited_time = _actual_time + _interval;
}
void stop() {
_running = false;
}
void wait_next() {
elapsed = false;
}
void update() {
_actual_time = millis();
if (_running) {
if (_actual_time >= _waited_time) {
_waited_time = _actual_time + _interval;
elapsed = true;
}
}
}
};
typedef enum {
FRENTE,
TRAS,
DIREITA,
ESQUERDA,
NOTHING
} direction_t;
class Motores {
private:
uint8_t _pin_IN[4];
uint8_t _pin_EN[2];
uint8_t _velocidade; // valor do sinal pwm 0-255
direction_t _direction;
Timer motorTimer;
void updateDirection() {
switch (_direction) {
case FRENTE:
digitalWrite(_pin_IN[0], 1); digitalWrite(_pin_IN[1], 0);
digitalWrite(_pin_IN[2], 1); digitalWrite(_pin_IN[3], 0);
break;
case TRAS:
digitalWrite(_pin_IN[0], 0); digitalWrite(_pin_IN[1], 1);
digitalWrite(_pin_IN[2], 0); digitalWrite(_pin_IN[3], 1);
break;
case DIREITA:
digitalWrite(_pin_IN[0], 0); digitalWrite(_pin_IN[1], 1);
digitalWrite(_pin_IN[2], 1); digitalWrite(_pin_IN[3], 0);
break;
case ESQUERDA:
digitalWrite(_pin_IN[0], 1); digitalWrite(_pin_IN[1], 0);
digitalWrite(_pin_IN[2], 0); digitalWrite(_pin_IN[3], 1);
break;
case NOTHING:
digitalWrite(_pin_IN[0], 1); digitalWrite(_pin_IN[1], 1);
digitalWrite(_pin_IN[2], 1); digitalWrite(_pin_IN[3], 1);
break;
default:
//TODO: Handle exceptions
break;
}
}
void updateSpeed() {
analogWrite(_pin_EN[0], _velocidade);
analogWrite(_pin_EN[1], _velocidade);
}
void stopMotors() {
digitalWrite(_pin_EN[0], 1); digitalWrite(_pin_EN[1], 1);
digitalWrite(_pin_IN[0], 1); digitalWrite(_pin_IN[1], 1);
digitalWrite(_pin_IN[2], 1); digitalWrite(_pin_IN[3], 1);
}
void begin() {
pinMode(_pin_EN[0], OUTPUT); pinMode(_pin_EN[1], OUTPUT);
pinMode(_pin_IN[0], OUTPUT); pinMode(_pin_IN[1], OUTPUT);
pinMode(_pin_IN[2], OUTPUT); pinMode(_pin_IN[3], OUTPUT);
}
public:
Motores(uint8_t pinENA, uint8_t pinIN1, uint8_t pinIN2, uint8_t pinIN3,
uint8_t pinIN4, uint8_t pinENB) {
_pin_EN[0] = pinENA;
_pin_IN[0] = pinIN1;
_pin_IN[1] = pinIN2;
_pin_IN[2] = pinIN3;
_pin_IN[3] = pinIN4;
_pin_EN[1] = pinENB;
begin();
setSpeedPercent(100);
setDirection(FRENTE);
}
void updateMotor()
{
motorTimer.update();
if (motorTimer.elapsed)
{
motorTimer.stop();
this->stop();
}
}
void runFor(uint32_t timeToRotate)
{
motorTimer.setInterval(timeToRotate);
motorTimer.start();
}
direction_t getDirection() {
return _direction;
}
void setDirection(direction_t direcao) {
_direction = direcao;
updateDirection();
}
uint8_t getSpeed() {
return _velocidade;
}
void setSpeed(uint8_t newSpeed) {
_velocidade = newSpeed;
updateSpeed();
}
double getSpeedPercent() {
return _velocidade * 100.0 / 255.0;
}
void setSpeedPercent(double newSpeed) {
_velocidade = (uint8_t)(((double)newSpeed / 100.0) * 255.0);
updateSpeed();
}
void run() {
updateDirection();
updateSpeed();
}
void stop() {
stopMotors();
}
};
class LineSensor {
private:
uint8_t _pin;
uint16_t _limiar;
public:
bool _is_in_black;
LineSensor(uint8_t pin) {
_pin = pin;
calibrate();
}
void update() {
_is_in_black = analogRead(_pin) > _limiar;
}
void calibrate() {
setLimiar(512);
}
void setLimiar(uint16_t limiar) {
_limiar = limiar;
}
};
class UltrassonicSensor
{
private:
uint8_t _pinTrigger;
uint8_t _pinEcho;
uint32_t _duration = 0;
uint32_t _distance;
uint32_t _timeOut = 3000;
public:
UltrassonicSensor(uint8_t pinTrigger, uint8_t pinEcho)
{
_pinTrigger = pinTrigger;
_pinEcho = pinEcho;
pinMode(_pinTrigger,OUTPUT);
pinMode(_pinEcho,INPUT);
_timeOut = 3000; //see ultrasonic library for more info
}
uint32_t takeTime()
{
digitalWrite(_pinTrigger, LOW);
delayMicroseconds(2);
digitalWrite(_pinTrigger, HIGH);
delayMicroseconds(10);
digitalWrite(_pinTrigger, LOW);
_duration = pulseIn(_pinEcho,HIGH,_timeOut);
if ( _duration == 0 )
_duration = _timeOut;
return _duration;
}
uint32_t takeDistance()
{
takeTime();
_distance = _duration /29 / 2 ;
return _distance;
}
};
/**
FUUCK THE KING!!
*/
void waitForButton() {
Serial.println("Waiting");
digitalWrite(LED_BUILTIN, HIGH);
//Waiting for button
while (digitalRead(btn_start));
Serial.println("Going in 5 sec");
digitalWrite(LED_BUILTIN, LOW);
delay(5000);
digitalWrite(buzzer, 1);
delay(100);
digitalWrite(buzzer, 0);
Serial.println("Go");
}
// The pins were verified!
Motores carro(9, 8, 7, 4, 6, 5); // ENA,IN1,IN2,IN3,IN4,ENB in that order
UltrassonicSensor ultrasonic(10,11); // Trig ,Echo
LineSensor rightLine(A1);
LineSensor leftLine(A0);
float distance;
void setup() {
Serial.begin(115200);
carro.stop();
//Setando IO
pinMode(buzzer, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(btn_start, INPUT_PULLUP);
waitForButton();
carro.run();
carro.setSpeedPercent(35);
carro.setDirection(FRENTE);
}
#ifdef JUST_SURVIVE
/*
void loop(){
digitalWrite(LED_BUILTIN,0);
carro.setSpeedPercent(0);
delay(1000); digitalWrite(LED_BUILTIN,1);
carro.setSpeedPercent(10);
delay(1000); digitalWrite(LED_BUILTIN,0);
carro.setSpeedPercent(20);
delay(1000); digitalWrite(LED_BUILTIN,1);
carro.setSpeedPercent(30);
delay(1000); digitalWrite(LED_BUILTIN,0);
carro.setSpeedPercent(40);
delay(1000); digitalWrite(LED_BUILTIN,1);
carro.setSpeedPercent(50);
delay(1000); digitalWrite(LED_BUILTIN,0);
carro.setSpeedPercent(60);
delay(1000); digitalWrite(LED_BUILTIN,1);
carro.setSpeedPercent(70);
delay(1000); digitalWrite(LED_BUILTIN,0);
carro.setSpeedPercent(80);
delay(1000); digitalWrite(LED_BUILTIN,1);
carro.setSpeedPercent(90);
delay(1000); digitalWrite(LED_BUILTIN,0);
carro.setSpeedPercent(100);
delay(1000); digitalWrite(LED_BUILTIN,1);
}
*/
void loop() {
distance = ultrasonic.takeDistance();
rightLine.update();
leftLine.update();
Serial.print(analogRead(A0)); Serial.print("\t");
Serial.print(analogRead(A1)); Serial.print("\n");
if (!rightLine._is_in_black && !leftLine._is_in_black) {
digitalWrite(LED_BUILTIN, 1);
carro.setSpeedPercent(80);
carro.setDirection(TRAS);
delay(400);
} else if (!rightLine._is_in_black) {
digitalWrite(LED_BUILTIN, 1);
carro.setSpeedPercent(80);
carro.setDirection(TRAS);
delay(400);
carro.setSpeedPercent(60);
carro.setDirection(ESQUERDA);
delay(300);
} else if (!leftLine._is_in_black) {
digitalWrite(LED_BUILTIN, 1);
carro.setSpeedPercent(80);
carro.setDirection(TRAS);
delay(400);
carro.setSpeedPercent(60);
carro.setDirection(DIREITA);
delay(300);
} else {
digitalWrite(LED_BUILTIN, 0);
if (distance < 30) {
//Work around....
//Or maybe not...
distance < 10 ? carro.setSpeedPercent(80) : carro.setSpeedPercent(60);
carro.setDirection(FRENTE);
} else {
carro.setSpeedPercent(55);
carro.setDirection(DIREITA);
}
carro.setSpeedPercent(55);
carro.setDirection(FRENTE);
}
}/*
void loop(){
rightLine.update();
leftLine.update();
if(rightLine._is_in_black && leftLine._is_in_black){
digitalWrite(LED_BUILTIN,1);
carro.setDirection(TRAS);
} else if(rightLine._is_in_black){
digitalWrite(LED_BUILTIN,1);
carro.setDirection(ESQUERDA);
delay(50);
} else if(leftLine._is_in_black){
digitalWrite(LED_BUILTIN,1);
carro.setDirection(DIREITA);
delay(50);
} else {
digitalWrite(LED_BUILTIN,0);
carro.setDirection(FRENTE);
}
}
*/
#endif /*JUST_SURVIVE*/
#ifdef ROLL_ROLL_ROLL_ATTACK
void loop() {
distance = ultrasonic.takeDistance();
rightLine.update();
leftLine.update();
if (!rightLine._is_in_black && !leftLine._is_in_black) {
carro.setDirection(TRAS);
} else if (!rightLine._is_in_black) {
carro.setDirection(LEFT);
delay(50);
} else if (!leftLine._is_in_black) {
carro.setDirection(RIGHT);
delay(50);
} else {
if (distance < 30) {
//Work around....
//Or maybe not...
distance < 10 ? carro.setSpeedPercent(100) : carro.setSpeedPercent(80);
carro.setDirection(FRENTE);
} else {
carro.setSpeedPercent(42);
carro.setDirection(RIGHT);
}
}
}
#endif /*ROLL_ROLL_ROLL_ATTACK*/
#ifdef RANDON_SEARCH
void loop() {
distance = ultrasonic.takeDistance();
rightLine.update();
leftLine.update();
if (!rightLine._is_in_black && !leftLine._is_in_black) {
carro.setDirection(TRAS);
} else if (!rightLine._is_in_black) {
carro.setDirection(LEFT);
delay(50);
} else if (!leftLine._is_in_black) {
carro.setDirection(RIGHT);
delay(50);
} else {
if (distance < 30) {
//Work around....
//Or maybe not...
distance < 10 ? carro.setSpeedPercent(100) : carro.setSpeedPercent(80);
carro.setDirection(FRENTE);
} else {
carro.setSpeedPercent(42);
carro.setDirection(RIGHT);
}
}
}
#endif /*RANDON_SEARCH*/