Led and Ping sensor

Hi
I just got a arduino uno for christmas and I was wondering if anyone good help me out. I have an led in pin 13 and ground. I also have a ping ultrasonic rangefinder connected to ground, 5 volts and signal. What I want to do is when the range finder detects and object < 70 cm to turn the led on and when it detects an object > 70 cm to turn the led off. This is what I have for software so far...

const int pingPin = 7;

void setup() {
// initialize serial communication:
Serial.begin(9600);
}

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);
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.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
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 < 70;
{
digitalWrite(13, HIGH);
}

if pingPin > 70;
{
digitalWrite(13, LOW);
}

You need to put your code inside a function, llike 'loop'.

Also, you didn't mention a current limiting resistor on the LED - you must ahve one.

It is a good habit to get into is to explicitly initialise all pin modes, like the one your LED is connected to.

I'm pretty sure you need to declare your LED in the setup method.

like so:

pinMode(13, OUTPUT);

oh, and, your last block, the "if" statement isn't inside your loop, thus it never gets called.

Thanks for the reply. Groov, what do you mean about "current limiting resistor on the LED" ?

This is the new code

const int pingPin = 7;
pinMode(13, OUTPUT);

void setup() {
// initialize serial communication:
Serial.begin(9600);
}

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);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();

delay(100);

if pingPin < 70;
{
digitalWrite(13, HIGH);
}

if pingPin > 70;
{
digitalWrite(13, LOW);
}

}
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.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
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;
}

led's will burn up if they have a alot of current flowing through them ! they are generally designed to bear around 20 ma of current, a current limiting resistor is added in series to either the ground or 5V to limit the current flowing through it the general value is 470 Ohm you can go for lesser value like 270 if you feel its not looking bright :slight_smile:

thank you very much! Do you know what is wrong with the code?
thanks again.

I took the liberty to throw all your code away, and make a new one. :slight_smile:

#define fP 7 //Pin the ping sensor is connected to

void setup()
{
  pinMode(13, OUTPUT);     
}

void loop()
{
  if (ping() > 70)
  {
    digitalWrite(13, HIGH);   // set the LED on
  }
  else
  {
    digitalWrite(13, LOW);    // set the LED off
  }
}


int ping() //Get CM to obstacle in front of the sensor
{
  long duration;
  pinMode(fP, OUTPUT);
  digitalWrite(fP, LOW);
  delayMicroseconds(2);
  digitalWrite(fP, HIGH);
  delayMicroseconds(15);
  digitalWrite(fP, LOW);
  delayMicroseconds(20);
  pinMode(fP, INPUT);
  duration = pulseIn(fP, HIGH);
  return duration / 29.0 / 2.0;
}

Here I put the function for the ping sensor out in a function instead of having it inside the loop (I like to keep the loop as clear of all that as possible to make it easier to read through)

The ping() should then return an integer with the distance in CM to the tings in front of it, and if it is over 70, then turn the pin HIGH, if not turn it LOW.

Bld thank You so much!! I am new to electronics and just got an arduino for Christmas. You are very nice person to help me out! :slight_smile:

Did it work? :slight_smile:

Only put it together without testing it...

It's not working, I am not using a resistor, is that a problem?
Thanks

No resistor needed, it should be in the sensor.

Exactly what sensor is it that you have?

It works! I think I messed up on the wiring for the Ping Sensor. Thanks everyone for the help. Especially bld! :slight_smile: