IfThenÂ comparesÂ input values to a set number or text, and outputsÂ one of two values depending on the comparison result.Â This makes it easy to test to seeÂ ifÂ a sensor or other input value matches your criteria, andÂ thenÂ cause some action to occur.
For example, you can set IfThen to makeÂ a comparison of “> 200”. If input sensorÂ values are greater than 200, the widget couldÂ output 180 to move a servo all the way to the right. If it doesn’t, then the widget couldÂ output 0, moving a servo all the way to the left.
In a text example, you can set IfThen to make a comparison of “contains” “right,up” where if text coming from the speech recognizing widget SpeechIn contains a either the word “right” or “up” the output will tell a servo to move to the 180Â position.
- Numbers: Near Equal “~=”, defines a range
If you want an input number within a range instead of just greater or less than, use the “~=” comparison. With the ~= comparison, any number within a range, centered around the comparison value, will be a match.
- Numbers: Greater Than “>”, Less Than “<”
The Greater Than “>” and Less Than “<” comparisons simply compare the input value to the set value. For “>” if the input is more than the set value, the right hand output value is sent, otherwise (i.e. input less than or equal to the set value) the left value is sent. For “<” if the input value is lower than the set value, the right hand output is sent, otherwise the left value is sent.
- Text: “equals”, inputÂ text matches completely
WhenÂ processing text, the input text matches the set comparison text exactly (ignoring case and with leading and trailing spaces removed).
- Text: “contains”, inputÂ text contains a partÂ that matches
A match is made if any part of the input text matches the comparison text. If you want to test for multiple comparisons then separate each possible match by a comma. For example, if the comparison text is set to “right, up” then any input phrase that contains the word “right” or “up” will result in a match.
- Text: “part”, inputÂ text matches any part
A match is made if the input text matches exactly any part of the comparison text. For example, ifÂ Â the comparison text is set to “my favorite is the red balloon over there” and the input text is “red balloon” there will be a match. “my favorite” input will also match.
- Wait true, wait false, for a delayed reaction
TheÂ wait trueÂ andÂ wait falseÂ options are useful when you want something to react only when an incoming value has met your IfThen criteria for a certain amount of time.Â For example, if you are using a proximity sensor to make a sound when someone steps near, you could use aÂ wait trueÂ time of 2000 msecs to make sure the person is staying in the right place for more than two seconds.Â Similarly, if you wanted the same sound to keep going even if the person steps away from the sensor, you can set theÂ wait falseÂ time to say, 20,000, so the sound has a full 20 seconds to play out.
1 Configure IF comparison
Set the input typeÂ to Numeric orÂ Text
Set the comparison type
- >Â – all values greater than comparison number
- <Â – all values less than comparison number
- ~=Â – all values near equal to comparison number. For example, if the comparison number is 200, numbers from 125 to 275 will be true (assuming the defaultÂ ~= range set in the more tab to 150).
- equals – text that exactly matches the comparison text
- contains – text that has a part that matches the comparison text (multiple options can be comma separated)
- part – text that exactly matches any part of the comparison text
Set the value to compare to
- Number input: Enter a number for the comparison (default 512)
- Text input: In the more section, enter the text to compare to. For the “contains” type comparison, multiple matches can be entered separated by commas
2 Set true/false output values
Next to the output of the widget, set the values for true and false comparisons
- Left/False ( default 0) – the value output if the input value does not match the comparison
- Right/True (default 1023) – the value output if the input value matches the comparison
In the more tab, adjust the optional settings.
- ~= range – sets the total range of values accepted with the near equal comparison (~=). For example, if the comparison value is 200,Â and ~= range is set to 150, input values in the range of 125 to 375 are a match.
If you want and exact equals match, set this number to 0
- wait true – the number of milliseconds (thousands of a second) that the input value must match the comparison before the output switches to the true value from the false value
- wait falseÂ – the number of milliseconds (thousands of a second) that the input value must not match the comparison before the output switches to the falseÂ value from the true value.
- text comparison value – this field contains the text that will be compared to the incoming text when the comparison type is set to text
The wait true and wait false options are useful when you want something to react only when an incoming value has met your IfThen criteria for a certain amount of time.
For example, if you are using a proximity sensor to make a sound when someone steps near, you could use aÂ wait trueÂ time of 2000 msecs to make sure the person is staying in the right place for more than two seconds.Â Similarly, if you wanted the same sound to keep going even if the person steps away from the sensor, you can set the wait falseÂ time to say, 20,000, so the sound has a full 20 seconds to play out.