-
Notifications
You must be signed in to change notification settings - Fork 1
/
LigaLed.ino
60 lines (50 loc) · 1.25 KB
/
LigaLed.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
//Programa: Conectando Sensor Ultrassonico HC-SR04 ao Arduino Uno
//Carrega a biblioteca do sensor ultrassonico
#include <Ultrasonic.h>
//Define os pinos para o trigger e echo
#define pino_trigger 4
#define pino_echo 5
//Inicializa o sensor nos pinos definidos acima
Ultrasonic ultrasonic(pino_trigger, pino_echo);
void setup()
{
// amarelo
pinMode(12, OUTPUT);
// verde
pinMode(7, OUTPUT);
//vermelho
pinMode(8, OUTPUT);
Serial.begin(9600);
Serial.println("Lendo dados do sensor...");
}
void loop()
{
//Le as informacoes do sensor, em cm
float cmMsec;
long microsec = ultrasonic.timing();
cmMsec = ultrasonic.convert(microsec, Ultrasonic::CM);
//Exibe informacoes no serial monitor
Serial.print("Distancia em cm: ");
Serial.println(cmMsec);
delay(400);
if(cmMsec <= 30){
digitalWrite(12, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
}
else if(cmMsec >= 31 && cmMsec <= 100){
digitalWrite(12, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, LOW);
}
else if(cmMsec >= 101 && cmMsec <= 150){
digitalWrite(12, HIGH);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
}
else{
digitalWrite(12, LOW);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
}
}