Creating 2 Sliders that Always Add up to 100

Hi,
As an exercise, I’m trying to create two sliders that always add up to 100.

So if:
Slider 1 = 50, Slider 2 = 50

If Slider 1 updates to 25, Slider 2 dynamically changes to 75.
If Slider 2 then gets changed to 40, Slider 1 dynamically changes to 60.

I’ve gotten close and Slider 2 will dynamically update based on Slider 1 values.

But I’m running into problems getting Slider 2 to drive Slider 1.

My solution was to create an index number that would tell me which Slider was selected.
So if I change Slider 1, the index = 1. If I change Slider 2, the index = 2.
Then, I use logic in the Panel Execute DAT to run different code for each index number.

That all worked fine and dandy and Slider 2 affects Slider 1.

But it reverts immediately, since there is now an infinite loop where Slider 1 thinks its value changed, and runs its own code again.

How can I get around this???
Am I going down the wrong path with using “OnValueChange” in the Panel Execute DAT?

Thanks in advance!
slidersReactToEachOther_budget.toe (5.56 KB)

I believe the math is wrong so it’s not adding up the way you want, but I think the logic here could work for you -

[code]def onValueChange(channel, sampleIndex, val, prev):

slider1 = op('slider1')
slider2 = op('slider2')
difference = val-prev
if int(slider1.panel.select) and channel.name == "v1":
	slider2.panel.u -= difference

elif int(slider2.panel.select) and channel.name == 'v2':
	slider1.panel.u -= difference[/code]

I think the math is working. Because if you change the value of slider 2 but don’t release click, it works.

When you release the click is when the slider 1 then thinks it’s value changed, and runs it’s code.

Should I be using something other than OnValurChange here?

So here’s a simplified approach to what you’re after - at least, I think it is.

base_always_1.tox (1.17 KB)

This takes the approach that you can do this by watching the channel values. The slider vals are merged into a single CHOP. Watching that chop, if we can see that channel index 0 (or slider 1) has changed, we set the panel.u value of the other slider (slider 2) to 1 - slider1’s val. If it’s slider 2 that changes we do the opposite.

Does that help?

Very similar to Mathews’ option but using a switch inside the slider. It also works well even if clicking to jump to a point of the slider as opposed to dragging the knob. It is always good to see different approaches :smiley:
TwoLinkedSliders.1.toe (4.37 KB)