Simple "rollover menu"

Hi, first time Rule Machine user and while I got it working it's probably not ideal so I'm looking to learn the best way to do this in rule machine and hopefully pick up a few tricks along the way. I couldn't find any other posts trying to do something similar although there probably are and I just couldn't find them.

I have 2 switches (left and right) and with them I want to either increment or decrement a variable based on which switch is on and I would also like the value of the variable to rollover after I get to 3. Similarly I don't want the variable to go negative.

Below is how I ended up implementing it, but it doesn't seem elegant at all.
I used the values 1 to 3, but I could just as easily use 0 to 2 if doing that makes the rule simpler.

I tried using the modulo (%) operator and while it worked when the value increased beyond my limit, the problem with the modulo operator is that it allows the variable to have a negative value

Thanks very much guys

I donā€™t think there is a ā€˜more elegantā€™ way but I am curious what other will say.

I was using a variable in one case and I didnā€™t want it to go negative so I switched to using a virtual dimmer since a dimmer can not go negative but does not roll over like you want either.

You almost want to be able to define a fixed size array and for that I think you are going to be told to move to groovy.

image

sometimes you just need to hardcode it in yourself

1 Like

Yeah that's more or less what I do except I can roll around either way. I roll back to 1 if the channel gets too high, and if I get to 0 I roll back around to 3.

If this was C/C++ I'd use unsigned integers and the modulo operator