-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor_de_re_led_normal.ino
51 lines (45 loc) · 1.01 KB
/
sensor_de_re_led_normal.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
#define TRIG_PIN 6
#define ECHO_PIN 5
#define BUZZER_PIN 3
#define LED_PIN 8
long duration, distance;
int buzzerSpeed;
int buzzerTone;
void setup() {
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
}
void loop() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
duration = pulseIn(ECHO_PIN, HIGH);
distance = (duration / 2) / 29.1;
if (distance < 10) {
buzzerSpeed = 50;
buzzerTone = 1500;
ledBlink(50);
} else if (distance >= 10 && distance <= 30) {
buzzerSpeed = 100;
buzzerTone = 1000;
ledBlink(300);
} else {
buzzerSpeed = 200;
buzzerTone = 500;
ledBlink(500);
}
// Emite o tom do buzzer
tone(BUZZER_PIN, buzzerTone);
delay(buzzerSpeed);
noTone(BUZZER_PIN);
}
void ledBlink(int interval) {
digitalWrite(LED_PIN, HIGH);
delay(interval / 2);
digitalWrite(LED_PIN, LOW);
delay(interval / 2);
}