 # Repeating / resetting value numbers

Hi there, just wanted to rack anyone who is willing to give a hands brain!

What I need to be able to do, is repeat a limited value (0-120 for example) of numbers (B) as the main variable ( A) rises beyond that limit etc-

Example -
A rises from 0 to 120,
B rises from 0 to 120,
A continues to rise from 121 to 240
B starts again from 0 to 120

Etc etc..
Any help or tips / pointers would be much appreciated!
Dan.

Should A and B change value at the same time or are the changes sequential ?

Yes they change together, completely linear. Value B just needs to reset to 0 once it reaches 120 etc.

``````a = a < 240 ? a+1 : 0;
b = a < 120 ? b+1 : 0
``````

Use the modulus operator

``````if (A % 120 == 0) {
B = 0;
}
``````

...R

gcjr:

``````a = a < 240 ? a+1 : 0;
``````

b = b < 120 ? b+1 : 0

The modulus operator is okay, as long as the integer values aren’t allowed to wrap. If they do then you’ll get a discontinuity unless the divisor is a power of two. Modulus can be inefficient as well, depending on divisor. I’d steer clear unless absolutely necessary.

pcbbc:

thanks for the correction

Hi all, appreciate the replies!
I haven't been able to create the desired outcome yet though.

I may not have been clear enough of my requirements.

( 0 and 120 are just an example)

The value of A has the potential to go upwards of 60,000, A is an unsigned int.

B needs to start at 0 with A, and increase its count with A, but wrapping back to 0 every time it reaches 120.
Ie when A reaches 120, B returns to 0.
When A reaches 240, 360, 480 etc etc.

I've tried things as such

"
int A = count;
B = A;
if ( B==120)
{
B=0;
}
^ as an example of what I'm trying to achieve.

I tried the two suggestions above, using the modulus version, B stayed at 0
Using the second suggestion, B continually counted upwards regardless of A.

Thanks for your time all, much appreciated.

when B wraps to 0 and A > 120, should it be incrementing?

gcjr:
when B wraps to 0 and A > 120, should it be incrementing?

Yes, B should keep incrementing to the next 120, when it will again return to zero

Using the second suggestion, B continually counted upwards regardless of A.

Yes, B should keep incrementing to the next 120, when it will again return to zero

``````A         118 119 120 121 122 .. 239 240 241
B         118 119 120   0   1 .. 119 120   0
``````
``````if (++B > 120)
B = 0;
if (++A > 240)
A = 0;
``````

The value of A has the potential to go upwards of 60,000, A is an unsigned int.

…R

Robin2:

...R

Hi Robin, yes I did, I'm not sure how B is supposed to increment with that code?
B stayed at 0.

Both A and B are supposed to increment with the variable " count" ( a potentiometer at the moment for testing purposes), with B looping 0 to 120.

I tried giving B the value of A,

A = count ;
B = A;
if (A % 120 == 0) {
B = 0;
}
and as had been predicted in post #5, it ended up returning odd values.
Is there a different way I should have written this?

Please learn to post code properly using code tags.

``````A = count ;
B = A;
if (A % 120 == 0) {
B = 0;
}
``````

Try

``````A = count ;
B = A % 120;
``````

Without seeing the rest of your code it’s hard to say. A lot of confusion being caused by not knowing where you are incrementing count, and if the code we are meant to be writing is supposed to be doing the actual incrementing or just relying on the previously incremented variable count. Note that count was not mentioned AT ALL in the original post.

You cannot just use modulus operation on count (or a copy of it) if count is continuously increasing and will therefore “wrap around” when it reaches a maximum value of whatever type it is. That’s because 120 and 240 are NOT powers of 2.

Remove count, and the code that is presumably doing…

``````count = count + 1;
``````

And replace with…

``````a = a < 240 ? a+1 : 0;
b = b < 120 ? b+1 : 0;
``````

Or, if you must retain count for some reason, this code…

``````inc = count - last_count;
a = (a + inc) % 240;
b = (b + inc) % 120;
last_count = count;
``````

Will track how you increment count, and apply the same increment to a and b (modulo 240 and 120 respectively) whenever you get to this point in the code.

I tried giving B the value of A,

``````A = count ;
``````

B = A;
if (A % 120 == 0) {
B = 0;
}

My idea was to set B back to 0 every time A rises to a multiple of 120.

Then increment B at the same time as you increment A

``````A ++;
B ++;
``````

...R

sterretje:
Please learn to post code properly using code tags.

``````A = count ;
``````

B = A;
if (A % 120 == 0) {
B = 0;
}

``````

Try

``````

A = count ;
B = A % 120;

This works perfectly, thank you...
And in the future I will post code correctly, apologies. Thanks heaps for your help, Dan.