Beginner Question

New to electronics, so I'm just toying around with making things work.

I've got an Uno sitting here, with some LEDs and a Ping sensor.

I can get the LEDs and Ping working separately, using the Fade and Ping sketches. What I want to do is to merge them together.

Say the Ping is reading at 10", the LED should be dim, and as the Ping readings get lower, to say 2", the LED gets brighter.

I'm assuming a "simple" if statement in the loop should cover it, but I don't know how to make it. The 10" and 2" settings I can get, it's how I get the in-between stuff to work with the LED that I don't know.

I mean, I could do the math, and set 10" to 0, and 2" to 255, and do the math for all the decimal inches, but surely there's a better way?

Yes, use map on the ping range to assign a value that you can then use for PWM to control LED brightness.

http://arduino.cc/en/Reference/Map

Alright, so if I understand this right, in the example:

y = map(x, 1, 50, 50, 1);

if x=1, y=50, and if x=50, y=1.

So lets say I have the variables/values/whatever of "brightness" and "inches", writing it as:

brightness = map(inches, 10, 2, 0, 255);

It'd do what I want it to do?

Thanks for your help!

Got it to work, thanks!

if (inches <= 10 && inches >= 2)
{
brightness = map(inches, 10, 2, 0, 255);
}
else if (inches > 10)
{
brightness = 0;
}
else
{
  brightness = 255;
}

There ya go.