-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistance.ino
40 lines (36 loc) · 1.04 KB
/
distance.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
/*
DISTANCE RETURN
Use the arduino serial monitor on baud rate 115200 to see the distance in centimetres from the distance sensor to whatever is in front of it.
You will recieve about ten readings per second.
*/
const int RSL = 9;
// Distance Detecting Function:
#define mm 0
#define cm 1
#define in 2
#define ft 3
int distance(int trigger=12, int echo=13, int units = cm) {
pinMode(trigger, OUTPUT);
digitalWrite(trigger, LOW);
delayMicroseconds(2);
digitalWrite(trigger, HIGH);
delayMicroseconds(10);
digitalWrite(trigger, LOW);
pinMode(echo, INPUT);
long rawInput = pulseIn(echo, HIGH);
if (units == mm) { return round(0.1723 * rawInput); } else {
if (units == cm) { return round(0.01723 * rawInput); } else {
if (units == in) { return round(0.006783 * rawInput); } else {
if (units == ft) { return round(0.00056525 * rawInput); } else {
return 0; }}}}
}
void setup() {
pinMode(RSL, OUTPUT);
delay(2000);
digitalWrite(RSL, HIGH);
Serial.begin(115200);
}
void loop() {
Serial.println(distance(12, 13, cm));
delay(100);
}