-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathultrasonic distance detector.ino
62 lines (49 loc) · 1.36 KB
/
ultrasonic distance detector.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <Adafruit_GFX.h>
#include <Adafruit_SH1106.h>
#define trigPin 9
#define echoPin 8
Adafruit_SH1106 display(SEEK_SET);
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
display.begin(SH1106_SWITCHCAPVCC, 0x3C); //initialize with the I2C addr 0x3C (128x64)
display.clearDisplay();
}
void loop() {
float duration;
float distance_cm;
float distance_in;
digitalWrite(trigPin, LOW); //PULSE ___|---|___
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance_cm = (duration/2) / 29.15;
distance_in = (duration/2) / 74.07;
display.setCursor(25,0); //oled display
display.setTextSize(1);
display.setTextColor(WHITE);
display.println("Distance Meter");
display.setCursor(10,20); //oled display
display.setTextSize(1);
display.setTextColor(WHITE);
display.println(distance_cm);
display.setCursor(90,20);
display.setTextSize(1);
display.println("cm");
display.setCursor(10,45); //oled display
display.setTextSize(1);
display.setTextColor(WHITE);
display.println(distance_in);
display.setCursor(90,45);
display.setTextSize(1);
display.println("in");
display.display();
delay(500);
display.clearDisplay();
//for debugging
Serial.println(distance_cm);
Serial.println(distance_in);
}