If else you else my if else question. If else question

Hello, im currently somewhat new to arduino and I am trying to do a project that involves a HC-SR04 ultrasonic range finder. For testing purposes I currently have it set up so I can see when im within a specified distance in CM by LED lights. I currently use the serial monitor to see the distance that the range finder is detecting.

What I want to work-
When the distance is above 100 cms, turn the blue led on.
when the distance is below 99 cms turn the red led on
if it detects 0, no LEDs will light. ( any distance above “MAX_DISTANCE” results in 0 which is intended.)

What currently happens-
If it reads ANY distance it goes blue
if it reads 0 the red led goes on.

Im almost certain the issue is in the if statement. but after much experimentation I cannot figure it out.

I plan to increase the complexity of the if statements to become more accurate once i get the fundamentals working.

below is the code and a picture of my setup
The code is attached as well

Thanks in advance!

#include <NewPing.h>

#define ledred 6
#define ledblue 8
#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
pinMode(ledred, OUTPUT);
pinMode(ledblue, OUTPUT);

// Ideas for the future
//else if ( (uS > 100) && (uS <= 20) ) example of code I found on forum that says should fix my issue.
// (( uS > 100) && (uS <= (MAX_DISTANCE - 20) ) This is my own version, It needs tested.

}

void loop() {

unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).

if (uS > 100) { //If the distance is greather than X Turn the blue light on.
digitalWrite(ledblue,HIGH);
digitalWrite(ledred,LOW);
}
else if (uS < 99){ //If the distance is less than X Turn the Red light on.

digitalWrite(ledblue,LOW);
digitalWrite(ledred,HIGH);
}
else {
delay(10); // In any other circumstance no LEDs will light. This will be changed to red LED on after the previous if statements work correctly
digitalWrite(ledblue,LOW);
digitalWrite(ledred,LOW);
}

delay(150); // Wait X ms between pings (about X pings/sec). 29ms should be the shortest delay between pings.
Serial.print("Ping: ");
Serial.print(sonar.convert_cm(uS)); // Convert ping time to distance and print result (0 = outside set distance range, no ping echo)
Serial.println(“cm”);
}

Range_Finder_7_IF_failure.ino (1.74 KB)

uS is time, not a distance, so why are you comparing uS to your distance? if(uS > 100) ??? You may want to convert uS first then compare it.

There is no problem using uS as a proxy for distance - provided you use the correct values. I would be surprised in 1 uS corresponds with 1 centimeter, but I haven't studied the sonar library.

Write a short sketch that pings about once every second and displays the resulting uS value on the serial Monitor. That will allow you to learn how to relate uS to distance.

I don't immediately see anything wrong with your sketch, but please use code tags (rather than quote tags) as it makes it much easier to read code.

...R

RosDaddy:
When the distance is above 100 cms, turn the blue led on.
when the distance is below 99 cms turn the red led on
if it detects 0, no LEDs will light. ( any distance above “MAX_DISTANCE” results in 0 which is intended.)

You haven’t said what you want to happen for values 99 and 100. Do you want them to be treated the same as 0? (From the user’s point of view I can imagine that being a bit confusing.)

The uS must stand for micro seconds? So maybe I need to switch "uS" to "sonar.convert_cm"? Interesting, I will try this after work.

Currently the program displays distance in cms on the serial monitor(see bottom of code).

I'm not worried about 99-100. I will work out that kind of nitty gritty once the fundamentals are working.

Sorry about the code, how exactly do you insert it properly on the forum?

Thanks!

RosDaddy:
I’m not worried about 99-100. I will work out that kind of nitty gritty once the fundamentals are working.

Well, it seems to be pretty fundamental when the code doesn’t handle ranges the way you want, to understand what you want. At the moment your description does not cover one of the ranges so it’s impossible to tell you how to implement it. If you don’t care, make something up. For example you could say:

if distance > 100
turn blue LED on
else if distance > 98
turn red and blue LED on
else if distance > 0
turn red LED on
else
leave both LEDs off
endif