I’m now porting arduino code to stm32(c/c++). Please help me with this function:
constrain(x, a, b)
Returns
x: if x is between a and b
a: if x is less than a
b: if x is greater than b
Example
sensVal = constrain(sensVal, 10, 150);
// limits range of sensor values to between 10 and 150
Here a template-version, including a complete program to demonstrate the use (you should be able to copy and paste that):
This can be used for any type that has an
operator<(this includes all built-in numeric types, such asintandfloat).