Cycle through values in C++/Arduino
Increment or decrement by an integer value, (normally +1 or -1) but cycle between min and max constraints. Test code at gitHub
EX: dec by -1 4 3 2 1 4 3 2 1
inc by +2 -4 -2 0 2 -4 -2 0 2
int16_t cycleIncDec(int16_t x, int16_t dir, int16_t xmin, int16_t xmax) {
// inc/dec with constrained range
// supplied xmax must be greater than xmin
x += dir;
if (x > xmax) x = xmin;
else if (x < xmin) x = xmax;
return x;
}
No comments:
Post a Comment