PING SENSOR CODING HELP !!!!

PLZ HALP !!!

i was trying to make a ping sensor work with my duelmilanove, my robot needs a ping sensor for reading distance so i am doing a hello wolrd for ping, i used the inch reader code on arduinos website, and now i am trying to make a pin 13 LED light up when an object gets within 6in of the PING, but my code has a crap load of errors like error 'time wad not declared in this scope, and other crap… if someone is to fix my code and make it work, i will be SUPER happy and i might even send you some paypal cash, and maybe you could even work with my to develop code for my bot, for even more money… anyway here is my code…

// this constant won’t change. It’s the pin number
// of the sensor’s output:
const int pingPin = 7;

void setup() {
// Pin 13 has an LED connected on most Arduino boards:
pinMode(13, OUTPUT);
}

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

// 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);

inches =time(duration);

long time(long microseconds)

return time / 74 / 2; // divide the time by 2 to get inches, by time i mean the microseconds between the ping sending the sound and reciveing it
}

{
if (inches < 6)
{
digitalWrite(13, HIGH); // set the LED on
}

if (inches > 6)
{
digitalWrite(13, LOW); // sets the LED off
}

Try this [CODE UNTESTED]:

// when to light up the led
const int THRESHOLD = 6;
// this constant won't change.  It's the pin number
// of the sensor's output:
const int pingPin = 7;
const int ledPin = 13;

void setup() {
  // ledPin has an LED connected on most Arduino boards:
  pinMode(ledPin, OUTPUT);    
}

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

  pulse();
  
  // 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);


  inches = time(duration);
  
  if (inches <= THRESHOLD) //within THRESHOLD inches
  {
    digitalWrite(ledPin, HIGH);   // set the LED on
  }
  else
  {
    digitalWrite(ledPin, LOW);    // sets the LED off 
  }
}

void pulse() {
  // 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);
}

long time(long microseconds) {
  return microseconds / 74 / 2; // divide the time by 2 to get inches, by time i mean the microseconds between the ping sending the sound and reciveing it
}

Next time, when you post code, please use the button in the post editor :slight_smile:

it uploaded, but now when i get below six in in the ping, it makes a faint screech and the light blinks, and remains blinking every few milliseconds at a random interval

That might be what's called noise. Does it happen just at the transition between 6 inches, or say at 12 inches as well?

nothing happens at twelve inches and that is what i wanted, i dont want the light to go on at twelve inches... but this flickering happens at anywhere when the light should be solid, so how do i eleminate this noise...

(the light should be solid when PING dectects anything closer than 6in)

You need to do multiple readings. For instance, do ten and divide the sum by ten afterwards.

See this: http://www.arduino.cc/en/Tutorial/Smoothing then this: http://www.arduino.cc/playground/Code/AverageList

:)

And then you could try this UNTESTED CODE:

// when to light up the led
const int THRESHOLD = 6;
// how many samples to take for smoothing
const int SAMPLES = 10;
// this constant won't change.  It's the pin number
// of the sensor's output:
const int pingPin = 7;
const int ledPin = 13;

void setup() {
  // ledPin has an LED connected on most Arduino boards:
  pinMode(ledPin, OUTPUT);    
}

void loop()
{
  long totalDistance = 0;
  for (int i=0; i<SAMPLES; i++) {
    totalDistance += distance();
  }
  long inches = totalDistance / SAMPLES;
  
  if (inches <= THRESHOLD) //within THRESHOLD inches
  {
    digitalWrite(ledPin, HIGH);   // set the LED on
  }
  else
  {
    digitalWrite(ledPin, LOW);    // sets the LED off 
  }
}

long distance() {
  pulse();
  
  // 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);
  long duration = pulseIn(pingPin, HIGH);


  return time(duration);
}

void pulse() {
  // 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);
}

long time(long microseconds) {
  return microseconds / 74 / 2; // divide the time by 2 to get inches, by time i mean the microseconds between the ping sending the sound and reciveing it
}

now the LED on pin 13 will stay on after i remove my hand, unless i move it direct up to about 2 feet away. HOWEVER the led on the PING turns on when i move hand over the ping and then turns off perfectly when i move it away.

What do you mean the led on the ping? (I have never tried one so I do not know what you're talking about).

Also, the code does nothing to that LED as I know of... Was it not the led at 13 that flickered before?

well... the ping sensor has a light on it and that is the one that is working right now... the one that i said wasnt, is the LED built into the arduino, which is on pin 13