Constrain() - warning: second operand of conditional expression has no effect

byte a = 20;

void setup() {
  Serial.begin(115200);
  constrain(a, 0, 250);  // 0-254 causes warning
  Serial.print("\n\na: ");
  Serial.print(a);
}

void loop() {

}

Any value 0 - 254 for the 3rd argument for Constrain() causes this warning

warning: second operand of conditional expression has no effect

Using a value higher than legal for a byte compiles without any warning. I'm trying to keep my code warning/error free but I want to constrain between 0 & 100.

I ate a fish sandwich today and I got a headache. Can you tell me what is wrong please?

Try:

a = constrain(a, 0, 100);  // 0-254 causes warning

I commend you on wanting to write warning-free code.

As outsider has pointed out, you are not using constrain() correctly. It doesn't modify the value of a. It returns the constrained value. See:

Even after that change you still have a warning:

C:\Users\per\AppData\Local\Temp\arduino_modified_sketch_524349\sketch_apr05b.ino: In function 'void setup()':

C:\Users\per\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\cores\arduino/Arduino.h:95:39: warning: comparison is always false due to limited range of data type [-Wtype-limits]

 #define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))

                                       ^

C:\Users\per\AppData\Local\Temp\arduino_modified_sketch_524349\sketch_apr05b.ino:5:7: note: in expansion of macro 'constrain'

   a = constrain(a, 0, 250); // 0-254 causes warning

       ^

I'm thinking the best solution in this case is to use min() instead of constrain():

a = min(a, 100);

Using min this way has not the same output As constrain...

EDIT: in general :slight_smile:

It's the exact same output. They want to constrain the value of the variable to be between 0 and 100. Since a is an unsigned type it can never be less than 0 so there's no reason to use constrain in this case. You only need to be sure that the value will not exceed 100, which min() does.

pert:
It's the exact same output. They want to constrain the value of the variable to be between 0 and 100. Since a is an unsigned type it can never be less than 0 so there's no reason to use constrain in this case. You only need to be sure that the value will not exceed 100, which min() does.

I had read 250
constrain(a, 0, 250);in the first post - but indeed I did not check the type of a

The code says 250 but the post says:

I want to constrain between 0 & 100

pert:
The code says 250 but the post says:

right... anyway the correct code that does not generate any warning is

byte a = 20;

void setup() {
  Serial.begin(115200);
  a = constrain(a, 0, 250);  // THIS IS HOW TO DO THAT
  Serial.print("\n\na: ");
  Serial.print(a);
}

void loop() {}

J-M-L:
does not generate any warning

Maybe for you, but I set mine to File > Preferences > Compiler warnings > All:

C:\Users\per\AppData\Local\Temp\arduino_modified_sketch_286226\sketch_apr05b.ino: In function 'void setup()':

C:\Users\per\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\cores\arduino/Arduino.h:95:39: warning: comparison is always false due to limited range of data type [-Wtype-limits]

 #define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))

                                       ^

C:\Users\per\AppData\Local\Temp\arduino_modified_sketch_286226\sketch_apr05b.ino:5:7: note: in expansion of macro 'constrain'

   a = constrain(a, 0, 250);  // THIS IS HOW TO DO THAT

       ^

pert:
Maybe for you, but I set mine to File > Preferences > Compiler warnings > All:

C:\Users\per\AppData\Local\Temp\arduino_modified_sketch_286226\sketch_apr05b.ino: In function 'void setup()':

C:\Users\per\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\cores\arduino/Arduino.h:95:39: warning: comparison is always false due to limited range of data type [-Wtype-limits]

#define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))

^

C:\Users\per\AppData\Local\Temp\arduino_modified_sketch_286226\sketch_apr05b.ino:5:7: note: in expansion of macro 'constrain'

a = constrain(a, 0, 250);  // THIS IS HOW TO DO THAT

^

right indeed - I was on "more" not "all".. Need coffee this morning :slight_smile:

I guess reading the complete reference page would've helped. constrain() - Arduino Reference
I'm on the "More" warning level so a = constrain(a, 0, 250); works but as pert says, on "All" it still throws some warnings.

yes the warning makes sense for the macro given that a byte type is unsigned so is never negative. if the macro had been written with <= instead of < then 0 would have been an acceptable value and the warning would not be there

</sub> <sub> #define myConstrain(amt,low,high) ((amt)[color=red] <= [/color] (low) ? (low) : ((amt)>(high)?(high):(amt)))</sub> <sub>

this does not give any warning

#define myConstrain(amt,low,high) ((amt)<=(low)?(low):((amt)>(high)?(high):(amt)))

byte a = 20;

void setup() {
  Serial.begin(115200);
  a = myConstrain(a, 0, 250);
  Serial.print("\n\na: ");
  Serial.print(a);
}

void loop() {}