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;
}
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
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
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
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.
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.
If you really have no idea at all how to code, this forum can't help you because it's to help people who have written some code and have some problem with it. If you want someone to code for you, post in the Gigs and Collaborations forum.
Helping someone, and doing something for someone, are two very different things.
Your, "I'm new to programming" line suggests, at least infers that you might do some coding. So the answers are aimed at guiding you towards solutions that you can implement yourself. You already have a huge head start, because you have someone else's working program to study.
Professionals usually begin by attempting to fully understand and describe the problem without using any code. That usually makes it extremely easy to code. It is why you received so many indirect answers.