Having a range for greater or equal to

Feel like this is probably a very simple answer but i am just stuck. I have two sensor (ultrasonic) with a variable outputs. I want to a motor to active when both sensors are equal or with in plus or minus two. I have written it out but there must be a more concise way of doing this with equal, greater or lesser than fuction but i cant figure out how.

if(R_DisCM == L_DisCM || R_DisCM == L_DisCM+1 || R_DisCM == L_DisCM+2 || R_DisCM == L_DisCM-1 || R_DisCM == L_DisCM-2 || R_DisCM+1 == L_DisCM || R_DisCM+2 == L_DisCM || R_DisCM-1 == L_DisCM || R_DisCM-2 == L_DisCM){
  //run motor:

}

Hint: use abs() or maybe fabs().

1 Like

only if the variables are integer type you can replace your expression by

You can verify that all your expressions are covered.

if  ( ( L_DisCM -2 ) <= R_DisCM)  ||  ( R_DisCM <=  L_DisCM + 2))

if the variables are float type it depends on how intermediate values need to be handled.

Maybe something like

if ((R_DisCM - L_DisCM) < 2 || (L_DisCM - R_DisCM) < 2) {
  // run motor:
}

1 Like

That actually a great idea thank you, just one miner change

if ((R_DisCM - L_DisCM) <= 2 || (L_DisCM - R_DisCM) <= 2) {
  // run motor:
}

How about taking note of post #2 ?

if (abs(R_DisCM - L_DisCM) >= 2)
{
//run motors
}

2 Likes

So i think i figure it out - i think maikarg was really close but it would had issue with the negative numbers.

if((R_DisCM - L_DisCM) <= 2){
  if((R_DisCM - L_DisCM) >= -2){
  void FORWARD();
  void STOP();

I think i got it now. it has to be less or equal to 2 to pass, then for the second line it needs to be greater or equal to -2 to pass.

You are right.
Change or for and.

if ((R_DisCM - L_DisCM) < 2 && (L_DisCM - R_DisCM) < 2) {
  // run motor:
}

@Adi_Soday

One question is not answered yet. Are the variables of integer or float type?
If they are float the original expression gives false
if e.g. R_DisCM = 3.5 and L_DisCM = 2.75 as the difference is not an integer.

Strictly speaking, two numbers cannot be "within" a negative distance: just imagine them on the number line. Calculating by subtraction can result in a negative number of course, if it's in the "wrong order". So

if ((R_DisCM > L_DisCM ? R_DisCM - L_DisCM : L_DisCM - R_DisCM) <= 2)

expresses that idea and does not repeat the magic number of interest. It

  • is correct for unsigned integers
  • has maybe a pathological case with float/double
  • can fail with signed integers, more likely on AVR where int is only 16 bits
  int a = 19000;
  int b = -18000;
  Serial.println(a - b);  // -28536 !!
  Serial.println(abs(static_cast<float>(a) - static_cast<float>(b)));

Overflow like that is undefined behavior, so it is not guaranteed to be detectable after-the-fact, as in this example.

(Bonus reading as I was poking around: On finding the average of two unsigned integers without overflow - The Old New Thing (microsoft.com).)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.