Parking Sensor. I'm new to programming, please help

Hi, I made a parking sensor. The only problem is that I don't know how to stop the code when I park the car. If anyone could write in my code what It need, I would be grateful.
Here is my code.

#define trigPin1 5                                 
#define echoPin1 4                       


long duration, distance, UltraSensor1, UltraSensor2; //we'll use these variable to store and generate data

char data;
String SerialData="";

void setup()
{
Serial.begin (9600);                           

pinMode(trigPin1, OUTPUT);                     
pinMode(echoPin1, INPUT);                       
pinMode(11, OUTPUT);    this is green light
pinMode(12, OUTPUT);     this is yellow light
pinMode(13, OUTPUT);       this is red light  

}

void loop()
{
SonarSensor(trigPin1, echoPin1);         
UltraSensor1 = distance;                                

while(Serial.available())
{
  delay(10);
  data=Serial.read();
  SerialData+=data;
}

if(SerialData=="display distance")
{
Serial.print("distance measured by the first sensor: ");
Serial.print(UltraSensor1);
Serial.println(" cm");

Serial.println("---------------------------------------------------------------------------------------------------------");
}

SerialData="";
if(UltraSensor1 <=50)//
{
  digitalWrite(11,HIGH);
}
else               
{
  digitalWrite(11,LOW);
}
if(UltraSensor1 <=30)//
{
  digitalWrite(12,HIGH);
  digitalWrite(11,LOW);
}
else               
{
  digitalWrite(12,LOW);
}

  if(UltraSensor1 <=20)//
{
  digitalWrite(13,HIGH);
  digitalWrite(11,LOW);
  digitalWrite(12,LOW);
 }
 else 
 {
 digitalWrite(13,LOW);
 }
 
 if(UltraSensor1 <=10)//
{
  delay(200);
  digitalWrite(13,LOW);
  delay(200);
 }
 }



// SonarSensor function used to generate and read the ultrasonic wave
void SonarSensor(int trigPinSensor,int echoPinSensor)//it takes the trigPIN and the echoPIN
{
  //START SonarSensor FUNCTION
  //generate the ultrasonic wave
//----------------------------------------------------------------------------------------------------------------------
digitalWrite(trigPinSensor, LOW);// put trigpin LOW
delayMicroseconds(2);// wait 2 microseconds
digitalWrite(trigPinSensor, HIGH);// switch trigpin HIGH
delayMicroseconds(10); // wait 10 microseconds
digitalWrite(trigPinSensor, LOW);// turn it LOW again
//----------------------------------------------------------------------------------------------------------------------

//read the distance
//----------------------------------------------------------------------------------------------------------------------
duration = pulseIn(echoPinSensor, HIGH);
distance= (duration/2) / 29.1;
}

How would the Arduino know that the car is stopped? If you don't know, you can't write code for it.

Arduino would know because distance is not moving for example. But I dont know how to code it.

What do you want to happen when the car stops? LEDs behave differently, or...?????

What about when the sensor is at the limit of its reading? In that case, the distance is also not moving.

How do you define, "not moving"? What about moving very slowly? How fast must it be changing to count as moving? For how long must it be stopped to be counted as stopped?

I would like the lights to go off, to save power because it is currently on a 9V battery. When car is out, lights are off and that is okay. But when I park Its always on because of close distance. I would like to count stop after 10 seconds for example

@aarg has a point - the current state of your setup means that it is difficult for the arduino to figure out if the car has stopped.

Of course you can write some code that prevents the led from lighting up if the distance hasn't changed for a while, but it is not a good solution for various reasons.

In addition, even if your LEDs are turned off, if your arduino code is constantly running 24 hours a day I suspect that your 9v battery will run out very quickly.

The simplest solution - unplug the battery from the arduino after you have parked your car.

Some versions of Arduino can be put into low power standby modes. But many models have on board circuits that consume current and can't be turned off in software.

It’s easiest for me to put a switch between the arduino and the battery, but I thought there was some simple solution in the code.
If car is not moving for 10 sec, digital writes low. Else do code normaly :smiley:

This is how it looks like

Hi,
Use a DC-DC converter to connect your project to part of cars electrical system that is only powered up when you are driving or in reverse.
KISS.
Depending on the age of your car, electronics control wise, just power your project with the Reverse Lamp power at the back of the car.
KISS

Tom... :smiley: :+1: :coffee: :australia:

I made a sensor that will stand in the garage, not in the car :smiley:

Hi,

Use a plug pack to power your project.

Only light up LEDs for a certain distance range sensed.

Look at NewPing library for that sensor.

Tom... :smiley: :+1: :coffee: :australia:

Hi.

I made one which is powered from the car's rear USB outlet. So it powers down when the ignition switch is turned off. I was wondering why you hadn't done the same....

Installed on the car it works quite well when reverse parking in the street.

Now I feel like an under-acheiver for simply gluing a length of wood to the floor for the tyre to bump up against. :slight_smile:

3 Likes

Hahhahah I will do that, because no one helped me with the code :+1::+1::+1:

A tennis ball suspended from the ceiling that hits the rear windshield when you are close enough.
hutb22z7lcyy
Tom... :smiley: :+1: :coffee: :australia:

Well that seems very easy to code. Every time it moves, save the value of millis(). if millis() minus the saved time is more than 10 seconds, turn the LED off.

That is unfair. It took a very long time to get a good definition of "stopped" from you... coding for something you're not sure about is a giant waste of time. Especially when someone else is doing it.

1 Like

You could try a MPU6050 to detect car movement

How to put it in code?