#include "U8glib.h"
// Constructor call used to initialize oled display. Obtained from example code given by U8glib.h library
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_DEV_0 | U8G_I2C_OPT_NO_ACK | U8G_I2C_OPT_FAST);
// Defining Pin numers
const int trigPin = 9;
const int echoPin = 10;
const int buzzerPin = 8;
const int greenLedPin = 2;
const int yellowLedPin = 3;
const int redLedPin = 4;
// Bitmap data for displaying a custom icon on the OLED screen. This data represents an image in a format that the OLED can interpret.
// I used this online tool called image2cpp (https://javl.github.io/image2cpp/) that can turn your image into an array
const unsigned char bitmap_car_image [] PROGMEM = { // The PROGMEM keyword denotes that the array is stored in the program memory of the Arduino microcontroller. Storing data in program memory(PROGMEM) instead of RAM can be helpful for large amounts of data, which would otherwise take up too much space in RAM.
0xc0, 0x40, 0x00, 0x00, 0x00, 0x02, 0x03, 0xc0, 0x8f, 0xff, 0xff, 0xff, 0xf1, 0x03, 0xe0, 0x70,
0x00, 0x00, 0x00, 0x0e, 0x07, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 0xff, 0xc0, 0xff, 0x00, 0x00,
0x00, 0xff, 0x03, 0xc0, 0x00, 0x05, 0x75, 0xc0, 0x00, 0x03, 0x47, 0xf8, 0x05, 0x51, 0x40, 0x1f,
0xe2, 0x47, 0xfc, 0x05, 0x65, 0x80, 0x3f, 0xe2, 0x40, 0x78, 0x06, 0x45, 0x40, 0x1e, 0x02, 0x30,
0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x1e, 0x01, 0xff, 0xff, 0xff, 0x80, 0x78, 0x07, 0xe2, 0x00,
0x00, 0x00, 0x47, 0xe0, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0xbf, 0xff, 0xff, 0xff,
0xfd, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x06, 0x00
};
void setup() {
// Start serial communication
Serial.begin(9600);
// Define pins as inputs or ouputs/initialize sensor pins
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(greenLedPin, OUTPUT);
pinMode(yellowLedPin, OUTPUT);
pinMode(redLedPin, OUTPUT);
}
void loop() {
// Creating variables to calculate and store distance
long duration;
int distance;
// Clear the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin to HIGH. This triggers the ultrasonic sensor to send out a pulse
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the time duration (in microseconds) that the echoPin stays HIGH. This duration is the time taken for the pulse to travel to the object and back
duration = pulseIn(echoPin, HIGH);
// Calculates the distance to the object in centimeters.
// The speed of sound is approximately 0.034 cm/microsecond. Since the pulse travels to the object and back, the duration is divided by 2
distance = duration * 0.034 / 2;
// Update the buzzer and LEDs based on the distance of the car or object is from the sensor
if (distance < 10) {
// When the object is less than 10 cm away, the buzzer will beep rapidly, with delays of around 100 ms and a red LED will turn on, warning that the object is very close and should stop to avoid a collision.
digitalWrite(greenLedPin, LOW);
digitalWrite(yellowLedPin, LOW);
digitalWrite(redLedPin, HIGH);
tone(buzzerPin, 3000);
delay(100);
} else if (distance < 25) {
// If the object is less than 25 cm away, the beeping rate will increase, with delays of about 250 ms between beeps and a yellow LED will turn on, signalling a moderate distance that requires caution.
digitalWrite(greenLedPin, LOW);
digitalWrite(yellowLedPin, HIGH);
digitalWrite(redLedPin, LOW);
tone(buzzerPin, 1000);
delay(250);
} else if (distance < 50) {
// If the object is less than 50 cm away from the ultrasonic sensor, the buzzer will beep with a delay of approximately 500 ms and a green LED will turn on, indicating a relatively safe distance.
digitalWrite(greenLedPin, HIGH);
digitalWrite(yellowLedPin, LOW);
digitalWrite(redLedPin, LOW);
tone(buzzerPin, 500);
delay(500);
} else {
// Object is far enough away/at a safe distance
digitalWrite(greenLedPin, HIGH);
digitalWrite(yellowLedPin, LOW);
digitalWrite(redLedPin, LOW);
noTone(buzzerPin); // Turn off the buzzer
}
// Print the distance to the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Update the distance displayed in real time using functions from U8glib.h library
u8g.firstPage();
do {
u8g.setFont(u8g_font_unifont); // This font will be used for any text that is drawn on the display.
u8g.drawStr(10, 10, "Parking Sensor"); // Draws the string "Parking Sensor" at the coordinates (10, 10) on the display. Doesn't change, therefore using drawStr() function
// Draws a bitmap image at the coordinates (36, 15). The parameters 7 and 15 indicate the width and height of the bitmap image in pixels.
u8g.drawBitmapP(36, 15, 7, 15, bitmap_car_image); // bitmap_car_image is a pointer to the image data that will be drawn (refer to lines 16-24).
u8g.setPrintPos(0, 50); // Sets the current print position to the coordinates (0, 50). This position will be used for subsequent text printed using the print function.
u8g.print("Distance: ");
u8g.print(distance);
u8g.print(" cm");
} while (u8g.nextPage());
delay(500);
}
// Sources to help with U8glib.h Library:
// https://github.com/olikraus/u8glib/wiki/userreference#begin
// https://youtu.be/Lw0Aaoo3YSY?si=xLshK0GiyNA42dD0