# Parking Sensor

For this week project I took inspiration from the problem that a lot of people face while parking. This being that it can be really difficult to park close to a wall especially for new drivers. Newer cars are often fitted parking sensors which makes beeping sounds to alert the drivers about there distance from an object. Older cars however do not have this feature so it can often be a challenge to park cars especially for newer drivers.

For my adaptation of the parking sensor I used an ultrasonic sensor to the determine the distance of the sensor to an object in front of it. Then using the equation of a line I determine the rate at which the led should blink. The led will blink with an interval of 800ms if the object is more than 25cm away or it will blink with an interval of 100ms if the object is less than 5cm away. The interval between 5cm and 25cm will be calculated using the equation of the line given below:

interval = 35*distance -75

A button can be used to put the device in parking mode. When the device goes into parking mode the ultrasonic sensor is activated and sound waves are emitted. The time taken for these sound waves to come back determines the distance. The distance is then used to calculate the interval.

```// echo produces a pulse when reflected signal is received
// the reflected signal is the one that is sent by the trig pin
int trigPin = 10;
int echoPin = 9;
int statusLedPin = 2;
int prevState = LOW;
int button = 4;
int warningPin = 5;
float distance = 10;
bool parking = false;
int delayMS = 0;
int interval = 500;
unsigned long prevTime = 0;
int warningState = LOW;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(statusLedPin, OUTPUT);
pinMode(button, INPUT);
pinMode(warningPin, OUTPUT);

}

void loop() {
changeState();
parkingState();
if (parking) {
distance = getDistance();
calculateDelay();
//    Serial.println(delayMS);
}
if (!parking && warningState == HIGH) {
warningState = LOW;
digitalWrite(warningPin, warningState);
}
}

float getDistance() {
digitalWrite(trigPin, LOW); // no signal is sent
delayMicroseconds(2);

digitalWrite(trigPin, HIGH); // after 2ms a signal is sent
delayMicroseconds(10);
digitalWrite(trigPin, LOW); // signal is not sent after 10 ms

// read the time taken for the pulse to return
float duration = pulseIn(echoPin, HIGH);

// calculate the distance
return duration * 0.034 / 2;
}

void changeState() {
if (digitalRead(button) == HIGH && prevState == LOW) {
//    if (parking) {
//      parking = false;
//    } else if (!parking) {
//      parking = true;
//    }

//refactor
parking = !parking;
}
}

void parkingState() {
if (parking) {
digitalWrite(statusLedPin, HIGH);
//    digitalWrite(warningPin, LOW);
} else if (!parking) {
digitalWrite(statusLedPin, LOW);
//    digitalWrite(warningPin, HIGH);
}
}

void calculateDelay() {
if (distance >= 5 && distance <= 25) {
delayMS = (int) 35 * distance - 75;
} else if (distance < 5) {
delayMS = 100;
} else if (distance > 25) {
delayMS = 800;
}
}