Neste artigo será apresentado um exemplo alternativo àquele já apresentado em outra postagem só que em vez de utilizar o sinal luminoso com LEDs individuais, será utilizado uma Matriz de LEDs 8x8 a qual deixará o trabalho mais elegante.
Esse projeto permite simular um sensor de proximidade através do HC-SR04, escalonando a distância com sinal luminoso na Matriz de LED e um sinal sonoro com um Buzzer.
Componentes:
- 01 Arduino
- 02 CI registradores de deslocamento 74HC595
- 01 Matriz de LEDs 8x8
- 01 Sonorizador(Buzzer)
- 01 Sensor Ultrassônico HC-SR04
Após conectar todos os fios carregue o seguinte código:
1
2 // Projeto Sensor de Distância com Indicador Sonoro e Luminoso
3
4 #include
<Ultrasonic.h> // Para utilizar o sensor HC-SR04
5
6 #
define latchPin 8 // Pino conectado ao pino 12 do 74HC595 (Latch)
7 #
define clockPin 12 // Pino conectado ao pino 11 do 74HC595 (Clock)
8 #
define dataPin 11 // Pino conectado ao pino 14 do 74HC595 (Data)
9 #
define pinBip 13 // Pino do som conectado ao pino
13 do arduino
10 #
define trig 7 // Pino 7 do Arduino será a saída de trigger
11 #
define echo 6 // Pino 6 do Arduino será a entrada de echo
12
13 float dist_cm; // Variável que armazena o valor da distância em centímetros
14 int E = 10; // Escala em que se deseja trabalhar, caso queira a distâ¢ncia real,
utilize E = 1.
15
16 // Determinam os desenhos na Matriz de LEDs
17 byte led1[8];
18 byte led2[8];
19 byte led3[8];
20 byte led4[8];
21 byte led5[8];
22 byte led6[8];
23 byte led7[8];
24 byte led8[8];
25 byte led9[8];
26
27 // Mostram os desenhos na Matriz de LEDs previamente definidos.
28 void screenUpdate1();
29 void screenUpdate2();
30 void screenUpdate3();
31 void screenUpdate4();
32 void screenUpdate5();
33 void screenUpdate6();
34 void screenUpdate7();
35 void screenUpdate8();
36 void screenUpdate9();
37
38 void shiftIt(byte dataOut);
39
40 void barraLED(float entValor, int c); // Função que controla a Matriz de Leds
41
42 void funBip(float entValor, int c); // Função que controla o sonorizador
43
44 Ultrasonic ultrasonic(trig, echo); // Setar trig e echo no
HC-SR04
45
46 unsigned
long changeTime; // Variável auxiliar de controle do tempo no
sonorizador
47
48 void setup()
49 {
50 led1[0] = B00000000;
51 led1[1] = B00000000;
52 led1[2] = B00000000;
53 led1[3] = B10000000;
54 led1[4] = B10000000;
55 led1[5] = B00000000;
56 led1[6] = B00000000;
57 led1[7] = B00000000;
58
59 led2[0] = B00000000;
60 led2[1] = B00000000;
61 led2[2] = B00000000;
62 led2[3] = B11000000;
63 led2[4] = B11000000;
64 led2[5] = B00000000;
65 led2[6] = B00000000;
66 led2[7] = B00000000;
67
68 led3[0] = B00000000;
69 led3[1] = B00000000;
70 led3[2] = B00100000;
71 led3[3] = B11100000;
72 led3[4] = B11100000;
73 led3[5] = B00100000;
74 led3[6] = B00000000;
75 led3[7] = B00000000;
76
77 led4[0] = B00000000;
78 led4[1] = B00000000;
79 led4[2] = B00110000;
80 led4[3] = B11110000;
81 led4[4] = B11110000;
82 led4[5] = B00110000;
83 led4[6] = B00000000;
84 led4[7] = B00000000;
85
86 led5[0] = B00000000;
87 led5[1] = B00001000;
88 led5[2] = B00111000;
89 led5[3] = B11111000;
90 led5[4] = B11111000;
91 led5[5] = B00111000;
92 led5[6] = B00001000;
93 led5[7] = B00000000;
94
95 led6[0] = B00000000;
96 led6[1] = B00001100;
97 led6[2] = B00111100;
98 led6[3] = B11111100;
99 led6[4] = B11111100;
100 led6[5] = B00111100;
101 led6[6] = B00001100;
102 led6[7] = B00000000;
103
104 led7[0] = B00000010;
105 led7[1] = B00001110;
106 led7[2] = B00111110;
107 led7[3] = B11111110;
108 led7[4] = B11111110;
109 led7[5] = B00111110;
110 led7[6] = B00001110;
111 led7[7] = B00000010;
112
113 led8[0] = B00000011;
114 led8[1] = B00001111;
115 led8[2] = B00111111;
116 led8[3] = B11111111;
117 led8[4] = B11111111;
118 led8[5] = B00111111;
119 led8[6] = B00001111;
120 led8[7] = B00000011;
121
122 led9[0] = B00000000;
123 led9[1] = B00000000;
124 led9[2] = B00000000;
125 led9[3] = B00000000;
126 led9[4] = B00000000;
127 led9[5] = B00000000;
128 led9[6] = B00000000;
129 led9[7] = B00000000;
130
131 // define os pinos como saída
132 pinMode(latchPin, OUTPUT);
133 pinMode(clockPin, OUTPUT);
134 pinMode(dataPin, OUTPUT);
135
136 Serial.begin(9600); //Inicia comunicação serial
137 }
138
139 void loop()
140 {
141 float dist_cm;
142 long microsec = ultrasonic.timing();
143
144 dist_cm
= ultrasonic.convert(microsec, Ultrasonic::CM);
145
146 Serial.println(dist_cm/E);
147
148
barraLED(dist_cm, E);
149
150 }
151
152 void barraLED(float entValor, int E) /* Esta função direciona qual desenho que irá
formar de acordo com o intervalo de distância */
153
154 {
155
156 if(entValor > 0 && entValor <= 32/E)
157 {
158
159
funBip(entValor, E);
160
screenUpdate1();
161
162 for(int i = 0; i < 8; i++)
163 {
164
(led1[i] = led1[i]); // Para
a Matriz de Leds ficar acesa sem ficar piscando.
165 }
166 }
167
168
169 if(entValor > 32/E && entValor <= 64/E)
170 {
171
172
funBip(entValor, E);
173
screenUpdate2();
174 for(int i = 0; i < 8; i++)
175 {
176
(led2[i] = led2[i]); // Para
a Matriz de Leds ficar acesa sem ficar piscando.
177 }
178 }
179
180
181 if(entValor > 64/E && entValor <= 96/E)
182 {
183
184
funBip(entValor, E);
185
screenUpdate3();
186 for(int i = 0; i < 8; i++)
187 {
188
(led3[i] = led3[i]); // Para
a Matriz de Leds ficar acesa sem ficar piscando.
189 }
190
191 }
192
193
194 if(entValor > 96/E && entValor <= 128/E)
195 {
196
197
funBip(entValor, E);
198
screenUpdate4();
199 for(int i = 0; i < 8; i++)
200 {
201
(led4[i] = led4[i]); // Para
a Matriz de Leds ficar acesa sem ficar piscando.
202 }
203
204 }
205
206
207 if(entValor > 128/E && entValor <= 160/E)
208 {
209
210 funBip(entValor, E);
211
screenUpdate5();
212 for(int i = 0; i < 8; i++)
213 {
214
(led5[i] = led5[i]); // Para
a Matriz de Leds ficar acesa sem ficar piscando.
215 }
216
217 }
218
219
220 if(entValor > 160/E && entValor <= 192/E)
221 {
222
223
funBip(entValor, E);
224
screenUpdate6();
225 for(int i = 0; i < 8; i++)
226 {
227
(led6[i] = led6[i]); // Para
a Matriz de Leds ficar acesa sem ficar piscando.
228 }
229 }
230
231
232 if(entValor > 192/E && entValor <= 224/E)
233 {
234
235
funBip(entValor, E);
236
screenUpdate7();
237 for(int i = 0; i < 8; i++)
238 {
239
led7[i] = led7[i]; // Para
a Matriz de Leds ficar acesa sem ficar piscando.
240 }
241
242 }
243
244
245 if(entValor > 224/E && entValor <= 255/E)
246 {
247
248
funBip(entValor, E);
249
screenUpdate8();
250 for(int i = 0; i < 8; i++)
251 {
252
led8[i] = led8[i]; // Para
a Matriz de Leds ficar acesa sem ficar piscando.
253 }
254
255 }
256
257
258 if(entValor > 255/E)
259 {
260
261
funBip(entValor, E);
262
screenUpdate9();
263
264 }
265
266 }
267
268
269 void screenUpdate1() //
função para exibir a imagem
270 {
271
272 byte
row = B10000000; // linha 1
273
274 for (byte k = 0; k < 9; k++)
275 {
276
digitalWrite(latchPin, LOW); // abre o latch, deixando-o pronto para receber dados
277
shiftIt(~led1[k]); //
envia o array de LEDs (invertido) para os chips
278
shiftIt(row); //
envia o número binário da linha para os chips
279
digitalWrite(latchPin, HIGH); // Fecha o latch, enviando os dados no registrador para o display de
matriz
280 row
= row >> 1; //
deslocamento para a direita
281 }
282
283 }
284
285 void screenUpdate2()
286 {
287 byte
row = B10000000;
288
289
290 for (byte k = 0; k < 9; k++)
291 {
292
digitalWrite(latchPin, LOW);
293
shiftIt(~led2[k]);
294
shiftIt(row);
295
digitalWrite(latchPin, HIGH);
296 row
= row >> 1;
297 }
298
299 }
300
301
302
303 void screenUpdate3()
304 {
305 byte
row = B10000000;
306
307
308 for (byte k = 0; k < 9; k++)
309 {
310
digitalWrite(latchPin, LOW);
311 shiftIt(~led3[k]);
312
shiftIt(row);
313
digitalWrite(latchPin, HIGH);
314 row
= row >> 1;
315 }
316
317 }
318
319 void screenUpdate4()
320 {
321 byte
row = B10000000;
322
323 for (byte k = 0; k < 9; k++)
324 {
325
digitalWrite(latchPin, LOW);
326
shiftIt(~led4[k]);
327
shiftIt(row);
328
digitalWrite(latchPin, HIGH);
329 row
= row >> 1;
330 }
331
332 }
333
334
335 void screenUpdate5()
336 {
337 byte
row = B10000000;
338
339
340 for (byte k = 0; k < 9; k++)
341 {
342
digitalWrite(latchPin, LOW);
343
shiftIt(~led5[k]);
344
shiftIt(row);
345
digitalWrite(latchPin, HIGH);
346 row
= row >> 1;
347 }
348
349 }
350
351
352 void screenUpdate6()
353 {
354 byte
row = B10000000;
355
356
357 for (byte k = 0; k < 9; k++)
358 {
359
digitalWrite(latchPin, LOW);
360
shiftIt(~led6[k]);
361 shiftIt(row);
362
digitalWrite(latchPin, HIGH);
363 row
= row >> 1;
364 }
365
366 }
367
368
369
370 void screenUpdate7()
371 {
372 byte
row = B10000000;
373
374
375 for (byte k = 0; k < 9; k++)
376 {
377
digitalWrite(latchPin, LOW);
378
shiftIt(~led7[k]);
379
shiftIt(row);
380
digitalWrite(latchPin, HIGH);
381 row
= row >> 1;
382 }
383
384 }
385
386
387 void screenUpdate8()
388 {
389 byte row
= B10000000;
390
391
392 for (byte k = 0; k < 9; k++)
393 {
394
digitalWrite(latchPin, LOW);
395
shiftIt(~led8[k]);
396
shiftIt(row);
397
digitalWrite(latchPin, HIGH);
398 row
= row >> 1;
399 }
400
401
402 }
403
404
405 void screenUpdate9()
406 {
407 byte
row = B10000000;
408
409
410 for (byte k = 0; k < 9; k++)
411 {
412
digitalWrite(latchPin, LOW);
413
shiftIt(~led9[k]);
414 shiftIt(row);
415
digitalWrite(latchPin, HIGH);
416 row
= row >> 1;
417 }
418
419
420 }
421
422
423 void shiftIt(byte dataOut) // Desloca 8
bits, com o menos significativo deslocado primeiro, durante o extremo
ascendente do clock
424 {
425
426 boolean
pinState;
427
digitalWrite(dataPin, LOW); //
libera o registrador de deslocamento, deixando-o pronto para enviar dados
428
429 for (int i=0; i<8; i++) // para
cada bit em dataOut, envie um bit
430 {
431
digitalWrite(clockPin, LOW); /* define clockPin como LOW, antes de enviar o bit */
432
433 if ( dataOut & (1<<i) ) /*se o valor de dataOut e (E lógico)
434 uma máscara de bits
forem verdadeiros, defina pinState como
1 (HIGH)*/
435 {
436
pinState = HIGH;
437 }
438 else
439 {
440
pinState = LOW;
441 }
442
443
digitalWrite(dataPin, pinState);// define
dataPin como HIGH ou LOW, dependendo de pinState
444
digitalWrite(clockPin, HIGH); // envia o bit durante o extremo ascendente do clock
445
digitalWrite(dataPin, LOW);
446 }
447
digitalWrite(clockPin, LOW); // interrompe o deslocamento
448 }
449
450
451 void funBip(float entValor, int E) // Função que determina o sinal sonoro de acordo com
a distância
452 {
453
454 if(entValor > 0 && entValor <= 32/E)
455 {
456
457 if((millis() - changeTime) > 20) /* É preciso utilizar a função millis(), se não a matrriz de leds
fica piscando proporcionalmente a delay().
458 */
459
460 {
461
462
tone(pinBip, 5000, 100);
463
changeTime = millis();
464 }
465
466 else
467 {
468
noTone(pinBip);
469 }
470
471 }
472
473 if(entValor > 32/E && entValor <= 64/E)
474 {
475
476 if((millis() - changeTime) > 40)
477 {
478
479
tone(pinBip, 4000, 100);
480
changeTime = millis();
481 }
482
483 else
484 {
485
noTone(pinBip);
486 }
487
488 }
489
490
491 if(entValor > 64/E && entValor <= 96/E)
492 {
493
494 if((millis() - changeTime) > 80)
495 {
496
497
tone(pinBip, 3000, 100);
498
changeTime = millis();
499 }
500
501 else
502 {
503
noTone(pinBip);
504 }
505
506 }
507
508 if(entValor > 96/E && entValor <= 128/E)
509 {
510
511 if((millis() - changeTime) > 120)
512 {
513
514
tone(pinBip, 3000, 100);
515
changeTime = millis();
516 }
517
518 else
519 {
520
noTone(pinBip);
521 }
522
523 }
524
525 if(entValor > 128/E && entValor <= 160/E)
526 {
527
528 if((millis() - changeTime) > 160)
529 {
530
531
tone(pinBip, 3000, 100);
532
changeTime = millis();
533 }
534
535 else
536 {
537
noTone(pinBip);
538 }
539
540 }
541
542 if(entValor > 160/E && entValor <= 192/E)
543 {
544
545 if((millis() - changeTime) > 200)
546 {
547
548
tone(pinBip, 3000, 100);
549
changeTime = millis();
550 }
551
552 else
553 {
554
noTone(pinBip);
555 }
556
557 }
558
559 if(entValor > 192/E && entValor <= 224/E)
560 {
561
562 if((millis() - changeTime) > 240)
563 {
564
565
tone(pinBip, 3000, 100);
566
changeTime = millis();
567 }
568
569 else
570 {
571
noTone(pinBip);
572 }
573 }
574
575 if(entValor > 224/E && entValor <= 255/E)
576 {
577
578 if((millis() - changeTime) > 280)
579 {
580
581
tone(pinBip, 3000);
582
changeTime = millis();
583 }
584
585 else
586 {
587
588
noTone(pinBip);
589 }
590
591 }
592
593 if(entValor > 255/E)
594 {
595
screenUpdate9();
596
noTone(pinBip);
597 }
598
599 }
Depois de carregado o programa, quando movimenta o objeto em relação ao sensor ultrassônico, observa-se a variação do som junto com a indicação luminosa na Matriz de LEDs, tanto na aproximação quanto no afastamento.
Nenhum comentário:
Postar um comentário