IfThenElse

IfThenElseTest input value condition, and output different values accordingly

The IfThenElse widget allows the user to test for a certain condition (e.g. is a sensor value about 500?) and output a number if this condition is TRUE, and a different number if this condition is FALSE.

For example:

  • IF a light sensor has a reading near 500 (the condition is TRUE), THEN the widget can output “500”
  • ELSE if the sensor reports a value outside a range near 500 (condition is FALSE) IfThenElse outputs “0”.

This way, if the brightness is the right amount, a SoundControl widget connected to the IfThenElse will play a sound.

Conditions

The IfThenElse widget allows the user to set the kind of condition (e.g. less than, equal, near equal, greater than or equal, etc.), as well as the value to make the comparison to. The near-equal setting (~=) is especially useful in practice, because it tests TRUE for any value that is near what the user enters. For example, by default, the widget shows TRUE if the input value is between 475 and 525 because the comparison value is set to 500, and the nearEqualRange parameter is set to 25.

Output Values & Gating

The output values can either be a specific number as set in the interface for THEN or ELSE, or they can switch or “gate” values coming into the widget from sources defined in the inputGateTrue or inputGateFalse parameters. This gating behavior makes it possible for the IfThenElse widget to have its inputSource switch the output between two other sources of values.

For example, the user could set the inputSource to a DigitalIn listening to a switch. The inputGateTrue parameter is set to listen to an AnalogIn attached to a motion sensor, and inputGateFalse is set to an AnalogIn attached to a pressure sensor. The THEN behavior is set to inputGateTrue (the drop down actually shows the instance name set in the parameter) and the ELSE behavior is set to inputGateFalse. When the the switch is on (TRUE), THEN values from the motion sensor would pass through to the output of the IfThenElse widget. ELSE if the switch is off (FALSE), values from the pressure sensor would pass through to the output.

Combining IfThenElse Widgets

IfThenElse widgets can also be set up to combine multiple conditions into a single TRUE/FALSE decision. For example, say there are two proximity sensors in a room. The design calls for playing a sound only if there are people in front of both a person proximity sensors. In this case, set up two IfThenElse widgets, each listening to a separate proximity sensor. One of the IfThenElse widgets can be added on the other IfThenElse widget with an AND setting (as in the left IfThenElse in the example below). With the two IfThenElse widgets combined, both widget conditions must be TRUE for the first one to output its THEN value.

For more complex combinations, you can cascade as many IfThenElse widgets as you want – 1st to the 2nd, 2nd to the 3rd, 3rd to the 4th, etc.

IfThenElse_cascade

On Screen Features

  • in: The value received from the inputSource
  • out: The value set by the widget
  • add if then: By default, set to inputSource ONLY, which means the condition tested is only in this widget. If set to AND, OR, or XOR, allows the user to combine two IfThenElse widgets and test if both are TRUE (AND), if either is TRUE (OR), or if only one of the two is TRUE (XOR). If the user also sets an AND, OR, XOR condition on the second IfThenElse widget, a third widget can be part of the decision making. This kind of cascading can be extended to additional widgets for as many as desired.
  • comparison test: This drop down menu sets the comparison for the input number to the user set number to its right. The possible comparisons to the user set number resulting in a TRUE output are
    • <   input number is less than
    • <= input number is less than or equal to
    • == input number is the same as
    • ~= input number is near equal, within a range set by the nearEqualRange parameter. For example, if the user set number is 500, and the nearEqualRange is 10, input numbers between 490 and 510 will result in TRUE
    • >= input number is greater than or equal to
    • >   input number is greater than
  • comparison number: Set by the user and used in the above comparison
  • left T/F: The outcome of the condition test for this widget
  • right T/F: The outcome of the condition test for the added IfThenElse widget. Only shown if the ADD IF THEN drop down is set to something other than ONLY.
  • THEN drop down: Determines what is output if the test is TRUE. “set value” means that the user set “THEN number” to the right will be output. If the second item (the instance name in inputGatedTrue parameter) in the drop down is selected, then the values from that source will be output. If the third item (“none”) is selected, no number will be output in the TRUE case.
  • THEN number: A user set number that determines the value output if the condition test is TRUE, and the drop down “set value” If behavior is set to a gate input, the value from the gated source is output.
  • ELSE drop down: Determines what is output if the test is FALSE. “set value” means that the user set “ELSE number” to the right will be output . If the second item (the instance name in inputGatedTrue parameter) in the drop down is selected, then the values from that source will be output. If the third item (“none”) is selected, no number will be output in the FALSE case.
  • ELSE number: A user set number that determines the value output if the condition test is FALSE, and the drop down “set value”. If behavior is set to a gate input, the value from the gated source is output.

Parameters

  • invisible: if set to “yes”, the widget will disappear when the Flash movie is run
  • inputSource: the instance name of the source the widget listens to, e.g AnalogIn
  • addIfThen: The instance name of the IfThenElse widget to be combined with this widget – only used if the ADD IF THEN drop down on-screen control is set to something other than ONLY.
  • inputGatedTrue: An input source (different from the main inputSource) whose values will be passed through if the widget reads TRUE.
  • inputGatedFalse: An input source (different from the main inputSource) whose values will be passed through if the widget reads FALSE.
  • nearEqualRange: If the on-screen COMPARISON TEST is set to “~=”, this number sets the range around the user set number that will be considered near equal and TRUE. For example, if the user sets the comparison number to 300, and the NEAR EQUAL RANGE parameter is set to 100, input source values from 200 to 400 will result in a TRUE condition.
  • waitTimeTrue: The widget to outputs the THEN value only after the input has been in the TRUE range for the specified time, in milliseconds (e.g. 1000 = 1 second). If set greater than zero, when the input value enters the TRUE range, TRUE will blink until the waitTime has been achieved. The waitTime parameters are very useful because they can prevent something happening if a sensor value only briefly enters the true range. For example, if a proximity sensor is used, the person would have to be in the correct range for the waitTimeTrue (say 2000, 2 seconds). Only then will a sound be played, where a soundControl is connected to the IfThenElse.
  • waitTimeFalse: If you want the widget to output the FALSE value only after the input has been in the FALSE range for a specified time, set this value to the time to wait in milliseconds (e.g. 1000 = 1 second). If set greater than zero, when the input value enters the FALSE range, FALSE will blink until the waitTime has been achieved.

Last modified October 15th, 2013