/* Parking Assistant Distance Meter -- PM Wiegand -- April 2025
Shows distance to sensor in inches as car approaches. Beeps when too close.
Room for future eye candy upgrades.
Hardware:
For Arduino Uno R3 or Mega
uses 4-digit 7-segment display with wiring:
top row: DP0, SP0, SP5, DP1, DP2, SP1
bot row: SP4, SP3, SP7, SP2, SP6, DP3
1k ohm resistors on each digit pin connection
active buzzer
HC-SR04 ultrasonic distance sensor
*/
#include "SevSeg.h" //https://github.com/DeanIsMe/SevSeg (install with library manager)
#include <TimerOne.h> //for triggering ISR for display refreshing
SevSeg sevseg; //Instantiate a seven segment controller object
#define BUZPIN 22 //connect to + of active buzzer
#define TRIGPIN 26 //connect to trigger pin of ultrasonic module
#define ECHOPIN 24 //connect to echo pin of ultrasonic module
#define MINDISTANCE 10 //minimum distance before buzzer sounds
unsigned long prevMillis = millis();
unsigned long curMillis = 0;
unsigned long milliDelay = 1000; //delay between distance measurements
void setup() {
Serial.begin(9600);
byte numDigits = 4; // Number of Digits
byte digitPins[] = { 2, 3, 4, 5 }; // connect to DP0 to DP3
byte segmentPins[] = { 6, 7, 8, 9, 10, 11, 12, 13 }; // Connect to segments SP0 to SP7
pinMode(BUZPIN, OUTPUT);
digitalWrite(BUZPIN, LOW);
pinMode(TRIGPIN, OUTPUT);
pinMode(ECHOPIN, INPUT);
sevseg.begin(COMMON_CATHODE, numDigits, digitPins, segmentPins); // This seven segment display is a common cathode type
Timer1.initialize(1000); //refresh display every 1000 microseconds
Timer1.attachInterrupt(displayDistance);
//If you are running this and your display is all light up then you should change the COMMON_CATHODE to COMMON_ANODE
}
void displayDistance() {
sevseg.refreshDisplay();
}
int getDistance() {
long duration; // variable for the duration of sound wave travel
int distance_cm; // variable for centimeters measurement
int distance_inch; // variable for inches measurement
// Clears the trigPin condition
digitalWrite(TRIGPIN, LOW);
delayMicroseconds(2);
// Sets the trigPin HIGH (ACTIVE) for 10 microseconds
digitalWrite(TRIGPIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGPIN, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
sevseg.refreshDisplay();
duration = pulseIn(ECHOPIN, HIGH);
// Calculating the distance
//distance_cm = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
distance_inch = duration * 0.0133 / 2; // Speed of sound wave divided by 2 (go and back)
return distance_inch;
}
void loop() {
// put your main code here, to run repeatedly:
int curDistance;
int deltaDistance;
static int prevDistance;
static int numParks = 0;
curMillis = millis();
if (curMillis - prevMillis > milliDelay) {
curDistance = getDistance();
sevseg.setNumber(curDistance);
prevMillis = curMillis;
deltaDistance = abs(prevDistance - curDistance);
if (curDistance > 200){sevseg.blank();}
if (deltaDistance < 5){
numParks++;
} else {
numParks = 0;
}
if (numParks > 5) { //parked
//digitalWrite(BUZPIN, LOW);
noTone(BUZPIN);
sevseg.blank();
} else {
if (curDistance < MINDISTANCE) {
//digitalWrite(BUZPIN, HIGH);
tone(BUZPIN, 500);
Serial.print("numParks=");Serial.print(numParks);Serial.print("curDistance=");Serial.println(curDistance);
} else {
//digitalWrite(BUZPIN, LOW);
noTone(BUZPIN);
}
}
prevDistance = curDistance;
}
}