Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#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