Hardware Reference
In-Depth Information
// Set trigger to high so we call pull
it low later
// Pull the trigger low at a regular interval.
setInterval
(
ping
,
ms
);
// Pull trigger low and start timing.
function
ping
() {
// console.log('ping');
b
.
digitalWrite
(
trigger
,
0
);
startTime
=
process
.
hrtime
();
}
// Compute the total time and get ready to trigger again.
function
pingEnd
(
x
) {
if
(
x
.
attached
) {
console
.
log
(
"Interrupt handler attached"
);
return
;
}
if
(
startTime
) {
pulseTime
=
process
.
hrtime
(
startTime
);
b
.
digitalWrite
(
trigger
,
1
);
console
.
log
(
'pulseTime = '
+
(
pulseTime
[
1
]/
1000000
-
0.8
).
toFixed
(
3
));
}
}
This code is more complex than others in this chapter, because we have to tell the device
when to start measuring and time the return pulse.