This is a “parking sensor” that I made for my knapsack, but you can use it on your car.
You’ll need an arduino board, a ultrasonic range sensor, a 9v battery, a switch and some wires.
This is the code:
//pin 6 = ultrasonic sensor pin
//pin 11 = 5v buzzer
const int pingPin = 6;
int SPKR = 11;
long previousMillis = 0;
void setup() {
pinMode(SPKR, OUTPUT);
Serial.begin(9600);
}
void loop()
{
long duration, inches, cm;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
duration = map(duration, 0, 15000, 0, 5000);
Serial.println(duration);
unsigned long currentMillis = millis();
if(currentMillis – previousMillis > (duration/2)) {
previousMillis = currentMillis;
if (duration!=0) {
if (duration<2000) {
for (int i=0; i<500; i++) {
digitalWrite(SPKR, HIGH);
delayMicroseconds(150);
digitalWrite(SPKR, LOW);
delayMicroseconds(150);
}
}
}
}
delay(100);
}