range finder and LEDs

I am controlling LED strip lights with a Maxbotic range finder and have the lights pulsing faster as an object gets closer (see code).

/*
* EZ rangefinder Distance Sensor
* prints distance and changes LED flash rate
* depending on distance from sensor
*/

const int sensorPin = 5;
const int ledPin = 13; //pin connected to LED

long value = 0;
int cm = 0;
int inches = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  value = pulseIn(sensorPin, HIGH);
  cm = value / 58;   // pulse width is 58 microseconds per cm
  inches  = value / 147;  // pulse is 147 microseconds per inch
  Serial.print(cm);
  Serial.print(',');
  Serial.print(inches);
  
  digitalWrite(ledPin,HIGH);
  delay(cm * 5);  //each centimeter adds 10 milliseconds delay
  digitalWrite(ledPin, LOW);
  delay( cm * 5);
  delay(20);
}

I'd like to have the lights "throb" if an object is outside of 5'..Do I use a switch statement? What is the best way?

Do I use a switch statement?

No, you’d use an if/else statement.

What is the best way?

The best way would be for you to define what “throb” means. It appears that what you are after is a non-linear relationship between distance and delay.

Thanks Paul,
Sorry, to be clear- I'd like the lights to evenly fade up and down in their brightness when an object is at or beyond a defined distance. The effect is like how the Apple start button "throbs" up and down...

Sorry, to be clear

If you had been, I wouldn't have had to ask. :slight_smile:

Look at the blink without delay example. The example shows how to turn an LED on or off at specific times. What you want to do is to create a new brightness value each time the LED should change state. Keep in mind that the brightness value does not continue increasing forever. Instead, once it reaches some limit values, the amount of change changes sign.

When an object is in range, do one thing. When it is not, do something else.