If statement that tests if a value is in range?

Hi I am trying to write a code that tests if a value is within a range of values

For example, I record a value, lets say x=20
I want an IF statement that checks if X is between 10 and 30
FWIW, x is a value that is taken from a sensor.

IF 30 > x > 10 
   y=pass;
else
   y=fail;

However when I put the code in it fails, can anyone tell me what I am doing wrong?

Thanks

if (myVal > lowerBound && myVal < upperBound) {

}
else {

}

...R

Be careful to use < or <= depending if you want the borders of the range to be included.

If you use this range checking a lot you might put it in a function

bool inRange(int val, int minimum, int maximum)
{
  return ((minimum <= val) && (val <= maximum));
}

...
// example usage
int x = analogRead(A0);

if ( inRange(x, 0, 200) )
{
  ...
} 
else
{
  ...
}
3 Likes

Thanks for the replys, I have read the examples and I feel like the code is right now but it still doesnt display on the LCD. It does not throw errors it just doesnt display.

The code I am speaking about I put markers in the program to help identify. The markers look like this //---------------------------

Thanks

#include <LiquidCrystal.h>

//LiquidCrystal  RS, E, 4,  5,  6, 7);
LiquidCrystal lcd(8, 9, 10, 11, 12, 13);       // put your pin numbers here

const int button = 2; //Defines the button as output 11
const int handpiece = 4; // Defines the handpiece as output 13
const int valve = 3; //Defines the valve as output 12
const int delay1 = 5000; //time to leave the handpiece on before testing
const int delay2 = 5000; //time to leave the valve on
const int delay3 = 10000; //time to leave results on screen
int buttonstate = 0;


// the setup routine runs once when you press the button
void setup() {            //This function gets called when the Arduino starts
  lcd.begin(20, 4);                          // put your LCD parameters here 
  lcd.print("Press Button");
  lcd.setCursor(0,1);
  lcd.print("To Start Test");
  
  pinMode(button, INPUT_PULLUP);
  digitalWrite(button, HIGH); //Powers the button
  pinMode(handpiece, OUTPUT);
  digitalWrite(handpiece, LOW);    // Verifies handpiece is off
  pinMode(valve, OUTPUT);
  digitalWrite(valve, LOW);    // Verifies valve is off

}

double Thermister(int RawADC) {  //Function to perform the fancy math of the Steinhart-Hart equation
  double Temp;
  Temp = log(((10240000 / RawADC) - 10000));
  Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp )) * Temp );
  Temp = Temp - 273.15;              // Convert Kelvin to Celsius
  Temp = (Temp * 9.0) / 5.0 + 32.0; // Celsius to Fahrenheit - comment out this line if you need Celsius
  return Temp;
}

void loop() {
  buttonstate = digitalRead(button);
  if (buttonstate == LOW) {
  lcd.clear();
  //Display Test Running
  lcd.print("Test");
  lcd.setCursor(0,1);
  lcd.print("Running");
    digitalWrite(handpiece, HIGH);   // turn the handpiece on
    delay(delay1);               // time to leave handpiece on
    
    //Take Speed Reading
    int tach = analogRead(A0);
    float tachvoltage = (tach * (5.0 / 1023.0))*20000;
 
    //Take Sound Reading
    int sound = analogRead(A1);
    float soundvoltage = (sound * ((5.0 / 1023.0)) * 100)-13;
    
    //Take Temp Reading
    double temp;
    int temperature = analogRead(A2);
    temp = Thermister(temperature);
   

    digitalWrite(valve, HIGH);   // turn the valve on
  lcd.clear();
  //Display Test Running
  lcd.print("Check for A1");
  lcd.setCursor(0,1);
  lcd.print("Alert on Console");
    delay(delay2);               // time to leave valve on
    digitalWrite(valve, LOW);    // turn the valve off
    digitalWrite(handpiece, LOW);    // turn the handpiece off
    
  lcd.clear();
  lcd.print("Test");
  lcd.setCursor(0,1);
  lcd.print("Completed");
  delay(2000);
  lcd.clear();
 //Display Temperature
  lcd.setCursor(0,0);
  lcd.print("Temp");
  lcd.setCursor(6,0);
  lcd.print(temp);
  lcd.setCursor(12,0);
  lcd.print("Deg F");
  //Display Speed
  lcd.setCursor(0,1);
  lcd.print("Speed");
  lcd.setCursor(6,1);
  lcd.print(tachvoltage);
  lcd.setCursor(15,1);
  lcd.print("RPM");
  //Display Sound
  lcd.setCursor(0,2);
  lcd.print("Sound");
  lcd.setCursor(6,2);
  lcd.print(soundvoltage);
  lcd.setCursor(12,2);
  lcd.print("dB");
  delay(delay3);
  lcd.clear();
//---------------------------------------- 
if (tachvoltage > 79000 && tachvoltage < 81000) {
  lcd.print("Speed Result");
  lcd.setCursor(14,0);
  lcd.print("Pass");
}
else {
  lcd.print("Speed Result");
  lcd.setCursor(14,0);
  lcd.print("Fail");
}
//----------------------------------------
  lcd.clear();
  lcd.print("Press Button");
  lcd.setCursor(0,1);
  lcd.print("To Start Test");
  }
  else {
    digitalWrite(valve, LOW);    // turn the valve off
    digitalWrite(handpiece, LOW);    // turn the handpiece off
    
  }



}

My gues would be, that you never get a tachvoltage value within your range.

Try widen the range to a much larger interval, to test if you get something, just for debugging.

First, your Thermister() function is designed to return a double, which really doesn't exist for the Arduino, so you may as well make it a float. Second, any time you have a complex expression using floating point numbers, you may as well make them all floating point constants:

    float tachvoltage = (tach * (5.0 / 1023.0))*20000.0
;

While it probably doesn't make any difference here, if that calculation was in a tight loop, if would be more efficient to write the statement as:

    float tachvoltage = tach * 97.75171;

since there are two constants and a divide operation is relatively slow.

Anders53:
My gues would be, that you never get a tachvoltage value within your range.

Try widen the range to a much larger interval, to test if you get something, just for debugging.

The value is outside the range currently, but shouldnt it show "Fail" when its outside the range?

Spoolx:
The value is outside the range currently, but shouldnt it show "Fail" when its outside the range?

YES it should, your are quite right !

I take it thats exactly your problem that it does not write ANYthing in that if statement ?
I would presume it is a problem with your cursor setting then.

Not to be pedantic - does your LCD works / display simple "hello word"? ( Or your setup text?)

econjack:
While it probably doesn't make any difference here, if that calculation was in a tight loop, if would be more efficient to write the statement as:

    float tachvoltage = tach * 97.75171;

since there are two constants and a divide operation is relatively slow.

No, that will make absolutely no difference whatsoever. All the math on the constants is done by the compiler, leaving only the one multiply by tach to be done at run-time. The code that actually ends up on the Arduino will do exactly what you have written, without having to modify the source code.

Regards,
Ray L.

Vaclav:
Not to be pedantic - does your LCD works / display simple "hello word"? ( Or your setup text?)

Everything works with the exception of the code I segregated in the above post.

Basically it displays all my results but does not display anything in this segment.

//---------------------------------------- 
if (tachvoltage > 79000 && tachvoltage < 81000) {
  lcd.print("Speed Result");
  lcd.setCursor(14,0);
  lcd.print("Pass");
}
else {
  lcd.print("Speed Result");
  lcd.setCursor(14,0);
  lcd.print("Fail");
}
//----------------------------------------

Could it be the multiple if statements?

double post

...and it's the "little stuff" marked with fancy comments lines that gets missed most often.
Don't feel bad - been there , done that.
And this snippet is not the only one - check the end and start of the loop().

@Ray: Is that always the case? I thought I remembered looking at the assembler a few years ago and saw that the constants weren't factor. I'm getting old, so I could be wrong.

Delta_G:
Expand your vision a little...

What does the very next line, executed a few microseconds after that stuff get printed tell the lcd to do?

if (tachvoltage > 79000 && tachvoltage < 81000) {

lcd.print("Speed Result");
      lcd.setCursor(14,0);
      lcd.print("Pass");
    }
    else {
      lcd.print("Speed Result");
      lcd.setCursor(14,0);
      lcd.print("Fail");
    }
    //----------------------------------------
    lcd.clear();

ouch... guess I need a delay, thanks I will try that asap