-
Notifications
You must be signed in to change notification settings - Fork 1
/
ultrasonic_glass.ino
49 lines (39 loc) · 1.2 KB
/
ultrasonic_glass.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
// Define pins
const int trigPin = D5;
const int echoPin = D6;
const int buzzerPin = D8; // Pin connected to the buzzer & led
// Declare Variables
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(buzzerPin, OUTPUT); // Sets the buzzerPin as an Output
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2;
// Prints the distance on the Serial Monitor [ with baud rate 9600 ]
Serial.print("Distance: ");
Serial.println(distance);
while (duration < 2353) {
Serial.println(duration);
int i = duration / 20;
digitalWrite(buzzerPin, HIGH);
delay(i / 2);
digitalWrite(buzzerPin, LOW);
delay(i);
break;
}
// delay(1000);
}