-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalma.ino
523 lines (441 loc) · 8.17 KB
/
alma.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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/**
* Programa que controla as funcionalidades do Robot C.o.L.a.2
* Desenvolvido pelo projeto Oya Que Legal
* Eng: Tiago Neves
* Prof: Elaine S. Santana
* Autor: Tiago Neves
*/
#include <LedControl.h>
#include <Ultrasonic.h>
#include <Servo.h>
//variaveis
Servo pescoso;
Servo pesEsq;
Servo pesDir;
//sonar
const int sonar_vcc = 13;
const int sonar_trg = 12;
const int sonar_ech = 11;
//boca
const int boca_esq = 5;
const int boca_dir = 6;
//matrizes
const int olhos_din = 10;
const int olhos_cs = 9;
const int olhos_clk = 8;
//servo pescoco (eng ferro)
const int pescoco_vcc = 4;
const int pescoco_dat = A0;
//servo pe esquedo
const int peEsq_vcc = 5;
const int peEsq_dat = A1;
//servo pe direito
const int peDir_vcc = 3;
const int peDir_dat = A2;
//configuracoes
bool sonar_ligado = false;
//animacao olhos
//configura led matrizes 8x8 olhos
LedControl olhos = LedControl(olhos_din, olhos_clk, olhos_cs,2);
/******************
* olho piscando
******************/
const byte ani_pisca_dir[][8] = {
{
B01111000,
B11111100,
B11111110,
B11111111,
B11001111,
B11001111,
B11111111,
B01111110
},{
B01111000,
B11111100,
B11111100,
B11111100,
B11001100,
B11001100,
B11111100,
B01111100
},{
B01110000,
B11110000,
B11110000,
B11110000,
B11000000,
B11000000,
B11110000,
B01110000
},{
B01000000,
B11000000,
B11000000,
B11000000,
B11000000,
B11000000,
B11000000,
B01000000
},{
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000
},{
B01110000,
B11110000,
B11110000,
B11110000,
B11000000,
B11000000,
B11110000,
B01110000
},{
B01111000,
B11111100,
B11111110,
B11111111,
B11001111,
B11001111,
B11111111,
B01111110
}};
const int ani_pisca_dir_len = sizeof(ani_pisca_dir)/8;
const byte ani_pisca_esq[][8] = {
{
B01111110,
B11111111,
B11001111,
B11001111,
B11111111,
B11111110,
B11111100,
B01111000
},{
B01111100,
B11111100,
B11001100,
B11001100,
B11111100,
B11111100,
B11111100,
B01111000
},{
B01110000,
B11110000,
B11000000,
B11000000,
B11110000,
B11110000,
B11110000,
B01110000
},{
B01000000,
B11000000,
B11000000,
B11000000,
B11000000,
B11000000,
B11000000,
B01000000
},{
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000
},{
B01110000,
B11110000,
B11000000,
B11000000,
B11110000,
B11110000,
B11110000,
B01110000
},{
B01111110,
B11111111,
B11001111,
B11001111,
B11111111,
B11111110,
B11111100,
B01111000
}};
const int ani_pisca_esq_len = sizeof(ani_pisca_esq)/8;
void pisca() {
for (int i=0;i<ani_pisca_esq_len;i++) {
for (int j = 0; j < 8; j++) {
olhos.setRow(0, j, ani_pisca_esq[i][j]);
olhos.setRow(1, j, ani_pisca_dir[i][j]);
}
delay(50);
}
}
void olhosAbertos() {
for (int j = 0; j < 8; j++) {
olhos.setRow(0, j, ani_pisca_esq[0][j]);
olhos.setRow(1, j, ani_pisca_dir[0][j]);
}
}
/**********************
* FIM do olho piscando
**********************/
/******************
* olho procura
******************/
const byte ani_proc_esq[][8] = {
{
B01111110,
B11111111,
B11001111,
B11001111,
B11111111,
B11111110,
B11111100,
B01111000
},{
B01111110,
B11111111,
B11111111,
B11111111,
B11001111,
B11001110,
B11111100,
B01111000
},
{
B01111110,
B11111111,
B11001111,
B11001111,
B11111111,
B11111110,
B11111100,
B01111000
},
{
B01001110,
B11001111,
B11111111,
B11111111,
B11111111,
B11111110,
B11111100,
B01111000
},{
B01111110,
B11111111,
B11001111,
B11001111,
B11111111,
B11111110,
B11111100,
B01111000
}};
const int ani_proc_esq_LEN = sizeof(ani_proc_esq)/8;
const byte ani_proc_dir[][8] = {
{
B01111000,
B11111100,
B11111110,
B11111111,
B11001111,
B11001111,
B11111111,
B01111110
},{
B01111000,
B11111100,
B11111110,
B11111111,
B11111111,
B11111111,
B11001111,
B01001110
},{
B01111000,
B11111100,
B11111110,
B11111111,
B11001111,
B11001111,
B11111111,
B01111110
},
{
B01111000,
B11111100,
B11001110,
B11001111,
B11111111,
B11111111,
B11111111,
B01111110
},{
B01111000,
B11111100,
B11111110,
B11111111,
B11001111,
B11001111,
B11111111,
B01111110
}};
const int ani_proc_dir_LEN = sizeof(ani_proc_dir)/8;
void olho_procura() {
for (int i=0;i<ani_proc_dir_LEN;i++) {
for (int j = 0; j < 8; j++) {
olhos.setRow(0, j, ani_proc_esq[i][j]);
olhos.setRow(1, j, ani_proc_dir[i][j]);
}
delay(200);
}
}
/**
* funcao que rederiza qualquer frame das animacoes
*/
void rederizaOlho(const byte* imagemEsq, const byte* imagemDir) {
for (int i = 0; i < 8; i++) {
olhos.setRow(0, i, imagemEsq[i]);
olhos.setRow(1, i, imagemDir[i]);
}
}
void olhaProLado() {
rederizaOlho(ani_proc_esq[1],ani_proc_dir[1]);
}
/*
* SENSOR ultrasonico
*/
Ultrasonic ultrasonic(sonar_trg, sonar_ech);
bool verificaCaminho() {
float cmMsec;
long microsec = ultrasonic.timing();
cmMsec = ultrasonic.convert(microsec, Ultrasonic::CM);
Serial.print("Distâcia a frente: "); Serial.print(cmMsec); Serial.print("\n");
if(cmMsec<20 && cmMsec>0.10) {
return false;
}
return true;
}
/*
* Boca Funcoes
*/
void abreBoca(int qnt) {
analogWrite(boca_esq, qnt);
analogWrite(boca_dir, qnt);
}
void fechaBoca() {
analogWrite(boca_esq, LOW);
analogWrite(boca_dir, LOW);
}
/*
* FIM Funcoes BOCA
*/
/*
* funcoes PESCOSO
*/
void pescosoverifica() {
pesEsq.detach();
pesDir.detach();
delay(3000);
digitalWrite(sonar_vcc,LOW);
fechaBoca();
pescoso.attach(pescoco_dat);
rederizaOlho(ani_proc_esq[1],ani_proc_dir[1]);
for (int pos = 90; pos <= 150; pos++) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
pescoso.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
abreBoca(30);
rederizaOlho(ani_proc_esq[3],ani_proc_dir[3]);
for (int pos = 150; pos >= 30; pos--) { // goes from 180 degrees to 0 degrees
pescoso.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
rederizaOlho(ani_proc_esq[1],ani_proc_dir[1]);
for (int pos = 30; pos <= 90; pos++) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
pescoso.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
olhosAbertos();
pescoso.detach();
digitalWrite(sonar_vcc,HIGH);
abreBoca(1);
delay(2000);
desviar();
}
/**
* funcoes dos pes
*/
void desviar() {
olhaProLado();
delay(2000);
pesEsq.attach(peEsq_dat);
pesDir.attach(peDir_dat);
pesEsq.write(170);
pesDir.write(170);
delay(7000);
pesEsq.write(1);
pesDir.write(170);
delay(700);
}
void andar1cm() {
pesEsq.attach(peEsq_dat);
pesDir.attach(peDir_dat);
pesEsq.write(1);
pesDir.write(1);
}
/**
* FIM funcoes dos pes
*/
void setup() {
olhos.shutdown(0,false); // Wake up displays
olhos.shutdown(1,false);
olhos.setIntensity(0,0.5); // Set intensity levels
olhos.setIntensity(1,0.5);
olhos.clearDisplay(0); // Clear Displays
olhos.clearDisplay(1);
Serial.begin(9600);
pinMode(sonar_vcc,OUTPUT);
digitalWrite(sonar_vcc,HIGH);
pescoso.attach(pescoco_dat);
pesEsq.attach(peEsq_dat);
pesDir.attach(peDir_dat);
pinMode(boca_esq,OUTPUT);
pinMode(boca_dir,OUTPUT);
abreBoca(1);
randomSeed(analogRead(0)); //humanizacao
}
void loop() {
pescoso.write(90);
pescoso.detach();
olhosAbertos();
if (!verificaCaminho()) {
pescosoverifica();
desviar();
} else {
andar1cm();
}
int rnd = random(300);
Serial.print("Humanização: "); Serial.print(rnd); Serial.print("\n");
if (rnd==99) {
pisca();
olho_procura();
} else if (rnd==33) {
olho_procura();
} else if(rnd==177) {
pisca();
}
}