Showing results for 
Search instead for 
Did you mean: 

How to constrain the input values to meet desired condition?

Suppose x and y are two controls on front panel

Constraint: P<= x+y <= Q

How to program such that user cannot avoid this constraint.

0 Kudos
Message 1 of 5

Hi gkartik


Use an Event Structure to detect changes of x and y, then evaluate against P and Q and action based on that.


X and Y.png

I have chosen to reset the value of x to 0 to show where the action would go but your requirements on how to modify x and y when x+y is outside of your P & Q limits would replace that.


0 Kudos
Message 2 of 5

It depends a bit on what you want to do.  I'm going to assume P and Q are fixed, and just for the sake of argument, I'm going to assume that all the quantities are integer (as there are issues involving coercing Floats).  I'll express the constraint as [P, Q], the interval from P to Q, including P and Q.


So suppose you have controls X and Y, and have initialized them such that X+Y is in [P, Q].  If you want to constrain X and Y in this way, I would create an indicator with the message "X+Y must lie in the interval [P, Q]" (you'd put the values for P and Q in this message, of course).


Now you want two Event Cases, with almost the same code in each.  The Value Changed case for X uses the In Range and Coerce function (on the Comparison Palette), with P, X+Y, and Q as the inputs, and then set X to the  Output - Y.  You want to set this up to include both upper and lower bounds in the comparison.  The code for Y is similar, except Y = Output - X.  Do you see how this works?  In the X case, X+Y is coerced to be in [P, Q], so to get the (coerced) value of X, we just subtract Y from "coerced X+Y".


Bob Schor

Message 3 of 5

It's likely to be better code to stick with the single event case.  Watch for both value changes.


Instead of that silly OR logic, replace with two case structures: one for over and one for under.  Handle the appropriate logic.


Quick, clean, all of the logic in one place.

0 Kudos
Message 4 of 5

There are many ways to handle the restriction.


  1. Given any x, limit the range of y accordingly, and vice versa
  2. if x or y is moved outside the valid range, adjust the "other" control so the condition is still met.

Which option do you want?


Another option would be to use two controls that are a transform of the current x and y, e.g.


Control A (defined as x+y and restricted with fixed boundaries.)

Control B (defined as x-y and not restricted in any way)


You could even create an xcontrol.

0 Kudos
Message 5 of 5