Activating lights with an ultrasonic range finder

My project is using an arduino board, an ultrasonic range finder (1-HCSR04) and an LED light strip (red indoor 1 meter 55-7060R-0).
I would like to have the lights activate at a slow heart beat at say 10m/30 feet; faster heart beat at say 6m/18 feet and really fast at say 2m/6 feet. I can get the range finder to read to the computer but can't write the arduino program with 'if' statements or && commands to trigger the lights at different speeds. I have the lights working.
I am an artist not an electronic guy and could use some collaboration with this part of the project. Help!!

So... Everything is wired-up? You can read distances and blink the LEDs? You just can't get the blink speed right?

I can get the range finder to read to the computer but can't write the arduino program with 'if' statements or && commands to trigger the lights at different speeds.

Have you got any code to show us?

Try getting it to work with just 2 speeds first.

If you're not doing this already, use greater-than or less-than to when you're checking distances. Trying to match an exact distance can often cause problems.

Ideally, this is probably a good application for the [u]map()[/u] function. But, if you don't completely understand that function, start with if-statements first.

If you use the map() function, you'll probably want to "constrain" the results so you don't get unreasonable blink times. For example -

if (Distance > 30)
Distance = 30;

Or, you can do something else if the distance is invalid or out or range.

Thanks I will give map a try. The basic program I can get to work is:
Arduino with PIR motion sensor
// For complete project details, visit: Arduino with PIR Motion Sensor | Random Nerd Tutorials
// Modified by Rui Santos based on PIR sensor by Limor Fried

/* HC-SR04 Sensor
https://www.dealextreme.com/p/hc-sr04-ultrasonic-sensor-distance-measuring-module-133696

This sketch reads a HC-SR04 ultrasonic rangefinder and returns the
distance to the closest object in range. To do this, it sends a pulse
to the sensor to initiate a reading, then listens for a pulse
to return. The length of the returning pulse is proportional to
the distance of the object from the sensor.

The circuit:

  • VCC connection of the sensor attached to +5V
  • GND connection of the sensor attached to ground
  • TRIG connection of the sensor attached to digital pin 2
  • ECHO connection of the sensor attached to digital pin 4
    Original code for Ping))) example was created by David A. Mellis
    Adapted for HC-SR04 by Tautvidas Sipavicius
    This example code is in the public domain.
    */

const int trigPin = 2;
const int echoPin = 4;

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 sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Read the signal from the sensor: 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(echoPin, INPUT);
duration = pulseIn(echoPin, 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;
}

Here ya go:

//    For complete project details, visit: http://RandomNerdTutorials.com/pirsensor
//   Modified by Rui Santos based on PIR sensor by Limor Fried

/* HC-SR04 Sensor
https://www.dealextreme.com/p/hc-sr04-ultrasonic-sensor-distance-measuring-module-133696

  This sketch reads a HC-SR04 ultrasonic rangefinder and returns the
  distance to the closest object in range. To do this, it sends a pulse
  to the sensor to initiate a reading, then listens for a pulse
  to return.  The length of the returning pulse is proportional to
  the distance of the object from the sensor.

  The circuit:
 * VCC connection of the sensor attached to +5V
 * GND connection of the sensor attached to ground
 * TRIG connection of the sensor attached to digital pin 2
 * ECHO connection of the sensor attached to digital pin 4
  Original code for Ping))) example was created by David A. Mellis
  Adapted for HC-SR04 by Tautvidas Sipavicius
  This example code is in the public domain.
*/


class Blinker {
 byte pin;
 int BPM = 0;
 byte lubDub = 0;
 unsigned long lubDubMs[4];
 unsigned long lubDubStartMs = 0;

 public:

 Blinker(byte pin) : pin(pin) {}

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

 void loop() {
   if(BPM == 0) return;

   if(millis() - lubDubStartMs < lubDubMs[lubDub]) return;

   lubDub = (lubDub + 1) % 4;
   digitalWrite(pin, (lubDub % 2)==0 ? HIGH : LOW);

   lubDubStartMs = millis();
 }

 void off() {
   BPM = 0;
   digitalWrite(pin, LOW);
 }

 void setRate(int BPMp) {
   if(BPMp == BPM) return; // no change

   if(BPMp == 0) { // dead
     off();
     return;
   }

   BPM = BPMp;

   if(BPM > 600) { // also dead, but we'll soldier on :)
     // this caps our millisecond period to 100ms
     BPM = 600;
   }

   unsigned long totalMs = 60L * 1000L / BPM;

   // I'm not sure how long a 'lub dub' should be. I'll go with 150ms for the 'lub', 100ms for the 'dub',
   // and 150ms in between. There should be a pause of (say) 250 after the 'dub'.

   // the problem is that this may excced our maximum rate. If it does, we have to compress things :)
   // our usual cycle is 600ms, so things start to compress after 100PBM, which seems reasonable

   // if the 'compression' > 1, then no compression is needed. We just let the gap after the 'dub'
   // take all the slack.

   float compress = (float)totalMs / (float)(150 + 150 + 100 + 200);
   if(compress > 1) compress = 1;

   lubDubMs[0] = 150 * compress;
   lubDubMs[1] = 150 * compress;
   lubDubMs[2] = 100 * compress;
   lubDubMs[3] = totalMs - lubDubMs[0] - lubDubMs[1] - lubDubMs[2];
 }
};

const int trigPin = 2;
const int echoPin = 4;

Blinker led(13); // Blinker on pin 13
unsigned long lastPingMs100;

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

void loop() {
 led.loop();
 if(millis()/100 != lastPingMs100) {
   ping();
   lastPingMs100 = millis() /100;
 }
}

void ping()
{

 // establish variables for duration of the ping,
 // and the distance result in inches and centimeters:
 long duration, inches, cm;

 // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
 // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
 pinMode(trigPin, OUTPUT);
 digitalWrite(trigPin, LOW);
 delayMicroseconds(2);
 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10);
 digitalWrite(trigPin, LOW);

 // Read the signal from the sensor: 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(echoPin, INPUT);
 duration = pulseIn(echoPin, 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();

 // set led rate. You could also compute it using map()

 if(inches < 6*12) led.setRate(120);
 else if(inches < 30*12) led.setRate(60);
 else led.off();
}

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