-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdot214-RangeFinder.ino
44 lines (30 loc) · 1.21 KB
/
dot214-RangeFinder.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
// Note: VCC on RangeFinder must go to +5V on Portenta
// GND on rangefinder goes to GND on Portenta
#include <Arduino.h> // Only needed by https://platformio.org/
int myTriggerPin = D6; // Trigger on RangeFinder
int myEchoPin = D7; // Echo on Rangefinder
unsigned long myDuration;
void setup() {
Serial.begin(115200);
pinMode(myTriggerPin, OUTPUT);
pinMode(myEchoPin, INPUT_PULLDOWN);
}
void loop() {
digitalWrite(myTriggerPin, LOW);
delayMicroseconds(10);
digitalWrite(myTriggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(myTriggerPin, LOW);
myDuration = pulseIn(myEchoPin, HIGH, 40000UL);
Serial.println("Duration us: "+ String(myDuration));
delay(200);
if (myDuration > 2000 ){ // raw data from 200 to 16000
// where 2000 raw = ~35cm, 4000 raw = ~80cm
digitalWrite(LEDB, LOW); // LEDB Blue LED on if far
} else {
digitalWrite(LEDB, HIGH); // LEDB Blue LED off if close or nothing
}
}
// Note: 29 microseconds per centimeter.
// or 73.746 microseconds per inch
//reminder /2 as the signal goes out and back.