#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// Define constants for the ultrasonic sensor
const int trigPin = 10;
const int echoPin = 11;
// Define constants for the OLED display
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Define variables
long duration;
int distance;
void setup() {
// Initialize the serial communication
Serial.begin(9600);
// Set ultrasonic sensor pins as OUTPUT and INPUT
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// Initialize the OLED display
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
delay(2000);
display.clearDisplay();
}
void loop() {
// Measure distance using the ultrasonic sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
// Print the distance to the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Display the distance on the OLED
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.print("Distance: ");
display.print(distance);
display.print(" cm");
display.display();
// Add a delay to avoid flooding the Serial Monitor and OLED display
delay(500);
}