NLTK > Reference > Widgets > IfThenElse

IfThenElse

The IfThenElse widget allows the user to test for a certain condition (e.g. is a sensor value greater than 500?) and output something if this condition is TRUE, and output something different if this condition is FALSE. For example, if a light sensor has a reading of less than 500, the widget can output “0″, and if the reading is greater than 500 the widget outputs 500 – this way, if the brightness is high enough, a SoundControl widget will play a sound.

In addition to the behavior described above, IfThenElse widgets can be combined to create multiple decision making conditions. For example, say there are two proximity sensors. The design calls for playing a sound only if a person is in front of each proximity sensor. In this case, set up two IfThenElse widgets, each listening to a separate proximity sensor. One of the IfThenElse widgets can be made to add on the other IfThenElse widget with an AND setting. This way, only if both widgets have their condition TRUE will the first IfThenElse widget output its TRUE value. Further, as many widgets as you want can be cascaded together (1st to the 2nd, 2nd to the 3rd, 3rd to the 4th, etc), allowing for very complex decisions.

EXAMPLE OF USING TWO IfThenElse WIDGETS IN COMBINATION

Download this example: IfThenElse_Example.fla, IfThenElse_Example.xml (note, the xml file must be in the same directory so that the settings for the example are correct)

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 user can also set the value that will be output in both the TRUE and FALSE conditions. These output values can either be a specific number, or they can be the incoming value added to or multiplied by some set value.

On Screen Features

  • IN: The value received from the inputSource
  • OUT: The value set by the widget
  • ADD IF THEN: By default, set to ONLY, which means the condition tested is only in this widget. If set to AND, OR, or XOR, allows the user to combine two widgets and test of 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
  • USER SET NUMBER: Used in the above comparison
  • LEFT TRUE/FALSE: The outcome of the condition test for this widget
  • RIGHT TRUE/FALSE: 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.
  • TRUE NUMBER: A user set number that determines the value output if the condition test is TRUE
  • TRUE BEHAVIOR: Determines how the user set TRUE number is treated. ONLY means that the user number will be output only, +IN means the input value is added to the user number, *IN means the input value is multiplied by the user number, NONE means that no value is output when the condition is TRUE.
  • FALSE NUMBER: A user set number that determines the value output if the condition test is FALSE
  • FALSE BEHAVIOR: Same as for TRUE BEHAVIOR

Parameters

  • INVISIBLE: if set to “yes”, the widget will disappear when the Flash movie is run
  • INPUT SOURCE: the instance name of the source the widget listens to, e.g AnalogInput
  • ADD IF THEN: 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.
  • NEAR EQUAL RANGE: 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.

Last modified May 22nd, 2010