IfThen

Compare input to a number or text to decide what to output

Overview

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.

Features

  • 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.

 


Instructions

1 Configure IF comparison

Set the input type to Numeric or Text

localhost_9001

Set the comparison type

localhost_9001

 

  • > – 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)
    OR
  • 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

3 More

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.

4 Example

This example patch shows how two IfThen widgets can be used to provide different responses depending on the text input. It uses the SpeechIn, Mix, and Servo widgets.

Download: ifthen_text.ntk

localhost_9001

NETLabTK - Tangible IoT