 # variable to increase in steps

I have a variable that changes from 0 - 127. And I would like an option to set a step size so that it increases by 2's or 3's etc.

I guess it is an easy problem, but I can't work it out. It is to occur in an interrupt, so no loops. On a single pass it must be determined whether the value is between an upper and lower boundary, such as 0 - 9 for a step size of 10, and then display the lower boundary of zero. Or if it is within the next step, 10 - 19, then display the lower boundary of ten. etc

``````const byte StepSize = 2;

var += StepSize;
``````

???

But what do you mean upper and lower bound? You mean, don't overshoot?

For 127 as upper limit

``````const byte StepSize = 2;

if(127 - var >= StepSize){
var += StepSize;
}
``````

thanks, that is not what I mean. I have it now

result = (value / step_size ) * step_size;

but now, i need to work out how to take it up to 127 when it falls short, which is most of the time.
for example, if the step size is 32 then it will count 32, 64, 96, but I also want it to go to the max, which is 127.

got that now, probably not elegant, but it works.

``````int temp = value;
result = (value / step_size ) * step_size;
if(temp == 127) value = 127;
``````

What have I done here. This was an earlier attempt, it worked but it was like some sort of filter that let the value slowly wind its way up to the highest step.
I actually like that delay for another feature, if I can make that delay variable. Can you tell me why this code winds up slowly to the highest step. About 1.5 seconds with a 10mS sample time.

``````if(value >  prevValue + stepSize) {prevValue += stepSize; }
else if(value <= prevValue- stepSize) {prevValue -= stepSize; }
value = prevValue;
``````

So you want to round/crop a variable to multiples of StepSize? But if you use a stepsize of 32 than 127 isn't a valid step... 32, 64, 96, 12*8* would be the most logical next step...

``````value = constrain(value, 0, 127);