Created
September 24, 2019 04:46
-
-
Save IsmailSan/ef100f4becc2237059d1202a1d26757f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Wire.h> | |
#include "SSD1306.h" | |
SSD1306 display(0x3c, 21, 22); | |
const int trigger = 2; | |
const int echo = 5; | |
int distance; | |
long duration; | |
void setup() { | |
Serial.begin(9600); | |
display.init(); | |
pinMode(trigger, OUTPUT); | |
pinMode(echo, INPUT); | |
} | |
void loop() { | |
display.clear(); | |
digitalWrite(trigger, LOW); | |
delayMicroseconds(2); | |
digitalWrite(trigger, HIGH); | |
delayMicroseconds(10); | |
digitalWrite(trigger, LOW); | |
duration = pulseIn(echo, HIGH); | |
distance = duration / 58.8; | |
display.drawString(0, 0, "Distance : " + String(distance) + " cm"); | |
display.display(); | |
Serial.print("Distance : "); | |
Serial.print(distance); | |
Serial.println(" cm"); | |
delay(500); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment