Comparators

Comparators are used to compare values and create conditional logic; aka as Logical Operators or Relational Operators. For example,while (getSpeed() <= 80) setSpeed(getSpeed() + 1); causes Sphero to accelerate until its speed reaches 80.

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

!= Requires the left value to differ from the right value, compensating for different data types (example: "3.01" != 3.01 is false)

=== Requires the left value to be equal to the right value, does not compensate for different data types (example: "3" === 3 is false)

!== Requires the left value to be unequal to the right value, does not compensate for different data types (example: "3.01" !== 3.01 is true)

< 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

&& Requires all statements in a set of 2 or more to be true

|| Requires any statement in a set of 2 or more to be true

getConnectedRobotType Requires the given robot to be connected to execute some logic, where the robot names are Sphero, 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) to see the red LED channel, or spin it counterclockwise (positive) 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 "normaliziation" 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.