-
Notifications
You must be signed in to change notification settings - Fork 0
/
blue_led_test.cpp
42 lines (35 loc) · 988 Bytes
/
blue_led_test.cpp
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
//Programa: Conectando Sensor Ultrassonico HC-SR04 ao Arduino
//Autor: FILIPEFLOP
//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);
int ledAlerta = 7;
void setup()
{
Serial.begin(9600);
Serial.println("Lendo dados do sensor...");
}
void loop()
{
//Le as informacoes do sensor, em cm e pol
float cmMsec, inMsec;
long microsec = ultrasonic.timing();
cmMsec = ultrasonic.convert(microsec, Ultrasonic::CM);
inMsec = ultrasonic.convert(microsec, Ultrasonic::IN);
//Exibe informacoes no serial monitor
Serial.print("Distancia em cm: ");
Serial.print(cmMsec);
Serial.print(" - Distancia em polegadas: ");
Serial.println(inMsec);
delay(10);
if(cmMsec <= 20) {
digitalWrite(ledAlerta,HIGH);
}
else {
digitalWrite(ledAlerta,LOW);
}
}