-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathhcsr04_example.sim
52 lines (44 loc) · 975 Bytes
/
hcsr04_example.sim
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
def measure : int : {
bool timeout := false;
int echo := 0;
start_counter();
while : read_counter() <= 2000 {
digital_write(7, true);
}
digital_write(7, false);
stop_counter();
start_counter();
while : not (digital_read(1)) and true {
if : read_counter() > 200000000 {
timeout := true;
break;
}
}
stop_counter();
if : not(timeout) and true {
start_counter();
while : digital_read(1) and true {
if : read_counter() > 200000000 {
timeout := true;
break;
}
echo := read_counter();
}
stop_counter();
}
if : timeout and true {
echo := 0;
}
return echo;
}
while : true {
int ping:= measure()*1000;
if : ping > 292200 {
digital_write(4, false);
}
else
{
digital_write(4, true);
}
delay(1000);
}