Constrain()

From Microduino Wiki
Revision as of 02:08, 17 August 2016 by Fengfeng (talk) (Created page with "<pre style="color:green"> #define constrain(amt, low, high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt))) </pre> Adjust to the interval.<br> If amt is less than low,, retur...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
#define constrain(amt, low, high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))

Adjust to the interval.

If amt is less than low,, return low; if amt is bigger than high, return high; otherwise, return amt . It is generally used to constrain a value to a certain range.

For example:

sensVal = constrain(sensVal, 10, 150);
// limits range of sensor values to between 10 and 150 

[Return to Arduino Syntax Manual]