Using Ping))) data to pwm a led

Hi, I'm a beginning arduino user, and I just got a Ping))) ultrasonic rangefinder. I ran the sample sketch off the arduino site and it's printing distances to the serial monitor like a charm.

What I'd like to do is to take that data and use it to brigten or dim a LED. I'd also probably have to be able to set the tolerance, so that, say- 3 inches is 225 and 50 inches is 0, or whatever.

Eventually I'd like to connect multiple Pings))) to RGB leds and make a space with position reactive lighting. But first I have to figure out how to control one LED!

Does anyone have any experience using the Ping)))?

If you're printing values, you're 99% there.

The remaining 1% can be found here:
http://arduino.cc/en/Reference/Map
and here

I think Map Value is the key! Thank you! I'll see what I can do...

also, I know that I can analogWrite to pins 9, 10, and 11 to fade LED's... what if I want to add another led so I can have an ultraviolet phase? do I just have to define one of the other pins to use analogWrite too?

do I just have to define one of the other pins to use analogWrite too?

Sadly, no you can't do that.
PWM is on pins 3,5,6,9,10 & 11.
However, if you've used all six, you could probably still easily get another PWM channel using software.

six pins ought to be fine. I’m going to use a blinkM for the RGB and I will probably need a couple of uv leds to get it bright enough.

I’m still not entirely sure how to get my distance data into the leds. There’s a variable called pingPin… should I set one of my led variables to be equal to that, but inside a map statement?

Here’s the original Ping code:

// Code is from:
// http://arduino.cc/en/Tutorial/Ping?from=Tutorial.UltrasoundSensor

int pingPin = 7;

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

void loop()
{
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // We 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.println(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;
}

"pingPin" is the digital pin you connect the Ping to.
"duration" is the length of pulse returned by the pin, proportional to range
According to your sketch, this will be 76uS per inch.
So, you need to print the value of "duration" to find the range of values to expect to feed into "map".
Now, invent a new "int" variable "ledVal"

and set "ledVal = map (duration, minRange, maxRange, 0, 255);"

so, now "ledVal" has a value in the range 0..255, proportional to range.

Awesome! It works!!!

int pingPin = 7;
#define RLED 9 //my red led


int rVal;

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

void loop()
{
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // We 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.print(duration);
  Serial.print(" raw value,");
  Serial.println();

  delay(100);
  
  // my code for the LED
  rVal = map (duration, 100, 10000, 0, 255);
  pinMode(RLED, OUTPUT);
  analogWrite(RLED, rVal);
}

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;
}

Awesome! It works!!!

I trust you never had any doubts that it wouldn't? :sunglasses:

Now I just have to order three more Pings)) and I'll have a rgb/uv reactive lamp. Assuming that I have pins for that much stuff, and I can duplicated the code I've got three more times.

Now I just have to order three more Pings))

There, I'm afraid, Mrs AWOL won't allow me to help further!

Ooh! Just as I was typing this, my BlinkM MaxM superpowered RGB Led showed up :smiley: ... Unfortunately I don't think there's enough room on my protoboard for both it and the Ping... maybe if I squeeeeze.