Comparators

Comparators are used to compare values and create conditional logic; aka as Logical Operators or Relational Operators. For example, "loop strobe LED until heading >= 270" executes strobe blinking until the heading reaches 270°.

== Requires the left value to be equal to the right value

!= Requires the left value to be unequal to the right value

< Requires the left value to be less than the right value

<= Requires the left value to be less than or equal to the right value

> Requires the left value to be greater than the right value

>= Requires the left value to be greater than or equal to the right value

and Requires all statements within to be true so you can combine =>2 conditions.

or Requires one statement within to be true, such that you exclude conditions.

Spinning Top Example

This program uses an > operator combined with an if then, else statement to modulate color values based on the Gyroscopic rate of rotation. Spin the robot like a top clockwise (negative spin rate), and notice that the redLed gets brighter the faster it spins, whereas if you spin it counterclockwise (positive spin rate) the greenLed gets brighter.

float _abs(float value)
{
   if (value < 0)
   {
     value = -value;
   }
   return value;
}

void startProgram()
{
    controlSystemIsOn = false;
    while (1)
    {
        if (((gyroSensorYaw * 25) > 0))
        {
            greenLed = (gyroSensorYaw / 8.6);
        }
        else
        {
            redLed = (_abs(gyroSensorYaw) / 8.6);
        }
    }
}

The Spinning Top shows that the combining several programming fundamentals like Control Flow, Operators, Comparators and Sensors unleashes the creative power of programming. We also leveraged some new tools in this program that are now yours to use:

Magnify: gyroSensorYaw * 25 exaggerates the real-world gyroscope value so that even slow spins generate noticeable LED color changes.

Normalize: gyroSensorYaw / 8.6 ensures that gyroscope values will generate valid redLed and greenLed values. This is because the gyroscope range is -2,000° - 2,000°, whereas the RGB range is 0-255. Hence, our normalization rate can be calculated as 2,000 / 255 = 8.6.

Absolute Value: _abs(gyroSensorYaw) / 8.6 removes negative sign from the the negative gyroscopic values that qualify the else condition. This normalizes the values to generate valid redLed values within the 0-255 (positive only) range.