# Comparators

Comparators are used to compare values and create conditional logic; aka as Logical Operators or Relational Operators.

For example, the below code causes Sphero to accelerate until its speed is greater than 80:

async function startProgram() {
setSpeed(20);
while (!(getSpeed() >= 80)) {
setSpeed(getSpeed() + 5);
await delay(0.2);
}
}

## Comparators === Evaluates if the left value is equal to the right value, does not compensate for different data types (example: "3" === 3 is false)

== Evaluates if the left value is equal the right value, compensating for different data types (example: 3 == "3" is true)

!= Evaluates if the left value differs from the right value, compensating for different data types (example: "3.01" != 3.01 is false)

!== Evaluates if the left value differs from the right value, does not compensate for different data types (example: "3.01" !== 3.01 is true)

< Evaluates if the left value is less than the right value

<= Evaluates if the left value is less than or equal to the right value

> Evaluates if the left value is greater than the right value

>= Evaluates if the left value is greater than or equal to the right value

## Logic && Evaluates if all conditions in a set of 2 or more are true

|| Evaluates if any conditions in a set of 2 or more are true

## Robot Type getConnectedRobotType Evaluates if a given robot is connected to execute some logic, where the robot names are BOLT, Sphero (includes Sphero SPRK+, SPRK, and 2.0), Mini, Ollie, BB8, BB9E, R2D2, and R2Q5. For example, to set the speed based on the connected robot, use:

var botSpeed = 0;

async function startProgram() {
if (getConnectedRobotType() === RobotType.Sphero) {
botSpeed = 100;
}
if (getConnectedRobotType() === RobotType.BB8) {
botSpeed = 200;
}
setSpeed(botSpeed);
}

# Examples

### Spinning Top

This program uses an > operator combined with an if then, else statement to modulate color values based on the Gyroscopic Rotation. Spin the robot like a top clockwise (negative yaw degrees) to see the red LED channel, or spin it counterclockwise (positive yaw degrees) to see the green LED channel:

async function startProgram() {
setStabilization(false);
while (true) {
if ((getGyroscope().yaw > 1.0)) {
setMainLed({ r: 0, g: getGyroscope().yaw / 7.84, b: 0 });
} else {
setMainLed({ r: Math.abs(getGyroscope().yaw / 7.84), g: 0, b: 0 });
}
await delay(0.25);
}
}

The Spinning Top shows that the combining several programming fundamentals like Control Flow, Operators, Comparators and Sensors unleashes the creative power of programming. You also used a new tool called "normalization" in this program. Normalizing modifies a value so it fits within a different range, just like how percentages normalize any 2 numbers from 0 - 100%. getGyroscope().yaw / 7.84 ensures that gyro values will generate valid setMainLed values. This is needed because the gyro range is -2,000° - 2,000°, whereas the LED range is 0-255. Hence, our normalization rate can be calculated as 2,000 / 255 = 7.84.