-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sketch.ino
55 lines (48 loc) · 1.17 KB
/
Sketch.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
#include <LiquidCrystal.h>
int regSelect = 13;
int enable = 12;
int d4 = 11;
int d5 = 10;
int d6 = 9;
int d7 = 8;
int trigPin = 7;
int echoPin = 6;
int switchPin = 5;
int responseTime;
int switchState;
int newVal;
int oldVal = 1;
float distance;
LiquidCrystal Display(regSelect, enable, d4, d5, d6, d7);
void setup() {
Display.begin(16, 2);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(switchPin, INPUT);
digitalWrite(switchPin, HIGH);
}
void loop() {
newVal = digitalRead(switchPin);
if (newVal == 0 && oldVal == 1) {
if (switchState == true) {
digitalWrite(trigPin, LOW);
delayMicroseconds(10);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
responseTime = pulseIn(echoPin, HIGH);
Display.setCursor(0, 0);
Display.print("Nearest Target");
Display.setCursor(0, 1);
distance = (responseTime * 123436685. / 1000000. / 3600. / 2.);
Display.print(distance);
Display.print("cm");
Display.print(" ");
Display.print("away");
switchState = false;
} else {
Display.clear();
switchState = true;
}
}
}