-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3_Arduino2Max_Ultrasonic.ino
More file actions
57 lines (42 loc) · 1.29 KB
/
3_Arduino2Max_Ultrasonic.ino
File metadata and controls
57 lines (42 loc) · 1.29 KB
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
/*
Example modified, just putting out a integer value through serial output.
Based_On:
https://dronebotworkshop.com/hc-sr04-ultrasonic-distance-sensor-arduino/
https://www.youtube.com/watch?v=6F1B_N6LuKw
HC-SR04 Basic Demonstration
HC-SR04-Basic-Demo.ino
Demonstrates functions of HC-SR04 Ultrasonic Range Finder
Displays results on Serial Monitor
*/
// This uses Serial Monitor to display Range Finder distance readings
// Hook up HC-SR04 with trigger to Arduino Pin 10, Echo to Arduino pin 13
#define triggerPin 10
#define echoPin 13
float duration, distance;
void setup() {
Serial.begin (9600);
pinMode(triggerPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
// Write a pulse to the HC-SR04 triggergerger Pin
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
// Measure the response from the HC-SR04 Echo Pin
duration = pulseIn(echoPin, HIGH);
// Determine distance from duration
// Use 343 metres per second as speed of sound
distance = (duration / 2) * 0.0343;
// Send results to Serial Monitor
if (distance >= 400 || distance <= 2) {
Serial.println(0);
}
else {
Serial.print(distance);
delay(500);
}
delay(500);
}