-
Notifications
You must be signed in to change notification settings - Fork 0
/
US.cpp
41 lines (33 loc) · 772 Bytes
/
US.cpp
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
// OTTO ultrasonic version 9
// OttDIY Arduino Project, 2020
#include "US.h"
US::US()
}
US::US(int pinTrigger, int pinEcho)
US::init(pinTrigger, pinEcho);
}
void US::init(int pinTrigger, int pinEcho) {
_pinTrigger = pinTrigger;
_pinEcho = pinEcho;
pinMode(_pinTrigger , OUTPUT);
pinMode(_pinEcho , INPUT);
}
long US::TP_init() {
digitalWrite(_pinTrigger, LOW);
delayMicroseconds(2);
digitalWrite(_pinTrigger, HIGH);
delayMicroseconds(10);
digitalWrite(_pinTrigger, LOW);
long microseconds = pulseIn(_pinEcho, HIGH, 40000); // 40000
return microseconds;
}
float US::read() {
long microseconds = US::TP_init();
long distance;
distance = microseconds / 29 / 2;
if (distance == 0) {
distance = 999;
}
return distance;
}
//end