-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensore-di-parcheggio.ino
63 lines (51 loc) · 1.41 KB
/
sensore-di-parcheggio.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
// Parking Sensor
// Define sensor pins
int triggerPort = 7;
int echoPort = 8;
int buzzer = 9;
// Define time variables
unsigned long time;
unsigned long blink_time;
unsigned long pause_time;
void setup() {
// Set pin modes
pinMode(triggerPort, OUTPUT);
pinMode(echoPort, INPUT);
pinMode(buzzer, OUTPUT);
// Initialize serial communication
Serial.begin(9600);
Serial.println("Ultrasonic Sensor:");
}
void loop() {
// Lower trigger pin output
digitalWrite(triggerPort, LOW);
// Send a 10 microsecond pulse on trigger pin
digitalWrite(triggerPort, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPort, LOW);
// Measure the duration of the echo pulse
long duration = pulseIn(echoPort, HIGH);
// Calculate distance based on duration
long distance = 0.034 * duration / 2;
// Print the results to serial monitor
Serial.print("Duration: ");
Serial.print(duration);
Serial.print(", Distance: ");
if (duration > 38000) {
Serial.println("Out of range");
} else {
Serial.print(distance);
Serial.println(" cm");
}
// Activate buzzer based on distance
if (distance > 3 && distance <= 200) {
delay(distance * 10);
digitalWrite(buzzer, HIGH);
delay(distance * 10);
} else if (distance <= 3) {
digitalWrite(buzzer, HIGH);
delay(1000);
}
digitalWrite(buzzer, LOW);
delay(10);
}