New Project: Small Distance gauge

Hi,
I’m pretty new to all this arduino stuff and decided to start off with a pretty simple project.
Using 9 LEDs and a PING))) ultrasonic range finder. I managed to make a distance gauge, the closer an object is to the sensor the more lights go on. The LED’s are arranged in a line.
I was wondering if there was a smarter way of coding it. In my code I used a bunch of if statments to determine when to turn on a curtain LED. Here is the function I’m calling in my main loop. Note, distance is in centimetres.

void distanceGauge(long distance){
if (distance > 5){
digitalWrite(ledPins[0], LOW);
}
if (distance > 10){
digitalWrite(ledPins[1], LOW);
}
if (distance > 20){
digitalWrite(ledPins[2], LOW);
}
if (distance > 30){
digitalWrite(ledPins[3], LOW);
}
if (distance > 40){
digitalWrite(ledPins[4], LOW);
}
if (distance > 50){
digitalWrite(ledPins[5], LOW);
}
if (distance > 60){
digitalWrite(ledPins[6], LOW);
}
if (distance > 70){
digitalWrite(ledPins[7], LOW);
}
if (distance > 80){
digitalWrite(ledPins[8], LOW);
}
if (distance < 5){
digitalWrite(ledPins[0], HIGH);
}
if (distance < 10){
digitalWrite(ledPins[1], HIGH);
}
if (distance < 20){
digitalWrite(ledPins[2], HIGH);
}
if (distance < 30){
digitalWrite(ledPins[3], HIGH);
}
if (distance < 40){
digitalWrite(ledPins[4], HIGH);
}
if (distance < 50){
digitalWrite(ledPins[5], HIGH);
}
if (distance < 60){
digitalWrite(ledPins[6], HIGH);
}
if (distance < 70){
digitalWrite(ledPins[7], HIGH);
}
if (distance < 80){
digitalWrite(ledPins[8], HIGH);
}
}

  1. There is no point in converting readings from sensors to anything else UNLESS you are going to report the reading to a human.

  2. Look and the use of the boolean op's &&(and) and || (or)

or

  1. Look at he use of the case statement.

All ways post code in code tags (read the instructions for the use of the forums) and always layout code using the auto format option in the IDE before posting.

Mark

thanks, I'll remember that for next time

To reduce the amount of similar code and make the logic more obvious, I'd put the threshold values in an array (each array entry will be the threshold value associated with the corresponding entry in ledPins ) and then write a for loop to process each LED; the for loop would set the LED output HIGH or LOW based on whether the distance was above or below the LED's threshold value.

Like this?

void distanceGauge(long distance){
  int threshhold[] = {
    5,10,20,30,40,50,60,70,80  };
  int ledPins[] = {
    10,9,8,7,6,5,4,3,2  };

  for(int i = 0; i < 9; i++){
    if (distance < threshhold[i]){
      digitalWrite(ledPins[i], HIGH);
    }
    if (distance > threshhold[i]){
      digitalWrite(ledPins[i], LOW);
    }
  }
}

Pretty much, except that the two if statements should be an if / else statement.

It's only a minor point, but it would be better to replace the hard-coded number 9 with the calculated length of the array e.g.

const int threshholdCount = sizeof(threshhold) / sizeof(threshhold[0]);

This way if the size of the array ever gets changed, your code will automatically adapt.