code check with PING))) sensor and LED

goal for Code:
turn on 1 led if 2 <= inches <= 5
turn on 2 led if 5 < inches <= 9
turn on 3 led if 9 < inches <= 13
however, my code turns on 3 led no matter what the distance is.

const int pingPin = 8;
const int ledPin1 = 3;
const int ledPin2 = 4;
const int ledPin3 = 5;

void setup() {

  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
}

void loop()
{

  long duration, inches;


  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);


  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  inches = microsecondsToInches(duration);
  
  delay(100);
  
  if (2 <= inches <= 5) {
    digitalWrite(ledPin1, HIGH); }
  if (5 < inches <= 9) {
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, HIGH); }
  if (9 < inches <= 13) {
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin3, HIGH); }
  else {
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW); }   
}

long microsecondsToInches(long microseconds)
{

  return microseconds / 74 / 2;
}
if (2 <= inches <= 5)

Doesn’t do what you think.

if (inches >= 2 && inches <= 5)

now it works for inches > 13 and 9 < inches <= 13, but it doesnt work for the other 2 intervals. Here is the new section of code:

  if (inches >= 2 && inches <= 5) {
    digitalWrite(ledPin1, HIGH); }
  if (inches > 5 && inches <=9) {
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, HIGH); }
  if (inches > 9 && inches <= 13) {
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin3, HIGH); }
  else {
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW); }

Perhaps it does, but you’re just not seeing it, because it happens so fast.

Assume “inches” has the value 3, and work it through.

actually they are turning on, its just very very dim. The led is bright when they are all on, but it becomes so dim when 2 or 1 led is on that you can barely see it.

, its just very very dim

Dim is fast.
Check your logic.

what to you mean? Is it something in the code thats not right?

Yes, check the logic of your comparisons.
The lights do come on, just not for a very long time, which is why they look dim.

i thought the led’s were simply controlled by the HIGH value in the digitalWrite function. And i dont have any delay functions, so should’t the led’s just turn on?

Also, I tried changing the code so it was only controlling two led’s and then it worked for 2, but then the 1 led was dim. It seems like it is working perfectly for the furthest distance interval, but not for the lower ones.

In reply #3, I suggested assuming “inches” has the value “3”.
Now, work through the code:

if (inches >= 2 && inches <= 5) {
    digitalWrite(ledPin1, HIGH); 
}
  if (inches > 5 && inches <=9) {
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, HIGH); 
  }
  if (inches > 9 && inches <= 13) {
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin3, HIGH); 
  } else {
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW); 
  }

3 is > 2 and 3 < 5. so light the LED.
Now, is 3 > 9 AND 3 <= 13?
What happens then?

so for inches = 3, ledPin1 should turn on only because 3 > 9 is false. Therefore ledPin2 and ledPin3 should stay off.

This part works, its just the brightness of the leds

You’re forgetting the “else” clause of the last comparison.

Got it to work!
I changed the else comparison to this:

 if (inches <2 || inches > 13) {
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW); }

Thank you so much for all your help. I really appreciate it.