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 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():
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
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() {}
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]
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