arduino + ultrasound parking garage senzor

hello arduino fans! :sunglasses:
i am a beginer in arduino and i need some help

I would like to made garage parking distance detector with ping senzor ultrasound senzor. + green and red led light

when car arrive in garage on 2 meters the green light turn on, when the car is in the distance 30cm the green led turn off and red turn on.

Is this code for program ok?
i use the example on arduino page for this parallax ultrasound senzor + if statment for led

i need help to make funcional and working code
please help
thanks :wink:

/* Ping))) Sensor

This sketch reads a PING))) ultrasonic rangefinder and returns the
distance to the closest object in range. To do this, it sends a pulse
to the sensor to initiate a reading, then listens for a pulse
to return. The length of the returning pulse is proportional to
the distance of the object from the sensor.

The circuit:

  • +V connection of the PING))) attached to +5V
  • GND connection of the PING))) attached to ground
  • SIG connection of the PING))) attached to digital pin 7

created 3 Nov 2008
by David A. Mellis
modified 30 Jun 2009
by Tom Igoe

*/

// this constant won’t change. It’s the pin number
// of the sensor’s output:
const int pingPin = 7; //const- pign senzor
int green = 13; //green led
int red = 12; //red led

void setup() {
// for serial monitor
Serial.begin(9600);
pinMode(zelena, OUTPUT);
pinMode(rdeca, OUTPUT);
}

void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, cm;

// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);

// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);

// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);

Serial.print(inches); // show distance in serial monitor
Serial.print("in, ");
Serial.print(cm);
Serial.print(“cm”);
Serial.println();

delay(100);
}

long microsecondsToInches(long microseconds)
{
// According to Parallax’s datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.

return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;

if (pingPin >= 200) //if distance is from 2meters to 30 cm green led is on
digitalWrite(green, HIGH);
digitalWrite(red, LOW);

if (pingPin <= 30) //if distance is 30cm red led turn on, green turn off
digitalWrite(green, LOW);
digitalWrite(red, HIGH);

}

[/quote]

my senzor will be URM 37 v3.2

Your last 6 lines of code seems to be in the wrong place.

Hi simel

sounds like a good starter project :)

if you use the URM37 sensor, have a look at this: http://milesburton.com/wiki/index.php?title=URM37_Ultrasonic_Distance_Measurement_Library

also, theres already a library for the URM37, so you dont have to use the PING example. http://download.milesburton.com/Arduino/URM37/URM37_2.0.zip

This code:

long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;

if (pingPin >= 200) //if distance is from 2meters to 30 cm green led is on
digitalWrite(green, HIGH);
digitalWrite(red, LOW);

if (pingPin <= 30) //if distance is 30cm red led turn on, green turn off
digitalWrite(green, LOW);
digitalWrite(red, HIGH);

}

should be:

long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;

if (pingPin >= 200) { //if distance is from 2meters to 30 cm green led is on
digitalWrite(green, HIGH);
digitalWrite(red, LOW);
}
if (pingPin <= 30) { //if distance is 30cm red led turn on, green turn off
digitalWrite(green, LOW);
digitalWrite(red, HIGH);
}
}

without the {} around the if(), only the first line after the test will be based on the tested condition.

Wayne

I have also made a library for the URM37 sensor, this mainly due to issues I’ve had with the above library (issues likely linked to my setup, I’d like to add).
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1271882162

Can’t say whether mine or his is better, all I can say is that it is another option :slight_smile:

PS:

if (pingPin >= 200) { //if distance is from 2meters to 30 cm green led is on

if (pingPin <= 30) { //if distance is 30cm red led turn on, green turn off

The first is would be incorrect too, going by the comment.
This could easily be remedied by changing it to:

if (pingPin <= 200) {          //if distance is from 2meters to 30 cm green led is on
...
if (pingPin <= 30) {             //if distance is 30cm red led turn on, green turn off

Since there is no else in between the if’s, the second if will take care of the lowerbound threshold of the first if (ie: if it measures a distance below 30, it will turn green on… then instantly off again, effectively not having it on in the first place).

edit: or perhaps even better, switch the two if’s around, and do put an else in between em… won’t even put green on for that tiny fraction of a millisecond.