Operators are mathematical actions that modify or create values. Several operators can be chained together to create complex logic in a single statement. A simple operation example is "heading = heading + 180" to add 180° to the current heading angle, like the program used in the Loop Forever example.
Remember to follow your "order of operations" when using multiple operators. Use the mnemonic "PEMDAS" to remember the rules; it stands for "Parentheses, Exponents, Multiplication, Division, Addition, Subtraction."
Random Integer:
Returns a random integer between 2 values, inclusive of the given minimum and maximum values
To return a random integer between 1 and 10, use the following examples:
JavaScript: getRandomInt(1, 10)
Python: get_random_int(1, 10)
Parameters:
min {number} - The minimum inclusive integer value
max {number} - The maximum inclusive integer value
Returns:
{number} - A random integer between 2 values
Random Float:
Returns a random float between 2 values, inclusive of the given minimum value and exclusive of the maximum value
To return a random float between 1 and 10, use the following examples:
JavaScript: getRandomFloat(1, 10)
Python: get_random_float(1, 10)
Parameters:
min {number} - The minimum inclusive value
max {number} - The maximum value (exclusive)
Returns:
{number} - A random float between 2 values
Returns a random color for use with any function that can accept a color value.
To randomize the main LEDs of your robot, use the following examples:
JavaScript: setMainLed(getRandomColor())
Python: set_main_led(get_random_color())
Returns:
{{ r: number; g: number; b: number }} - An object containing a random RGB (red, green, blue) color
JavaScript and Python use many of the same basic math operators:
+ adds two values
- subtracts one value from the other
* multiplies two values
/ divides one value by another
** exponent multiplies a value by itself a given number of times
% modulus returns the remainder after division of one value by another
For JavaScript:
++ or x += 1 increments a value by 1
-- or x -= 1 decrements a value by 1
For Python:
x += 1 increments a value by 1
x -= 1 decrements a value by 1
Both JavaScript and Python support these advanced operators:
Math.sqrt(x) returns the square root of x
Math.round(x) rounds a value to the nearest integer value (whole number)
Math.floor(x) rounds a value down to the nearest integer value
Math.ceil(x) rounds a value up to the nearest integer value
Math.abs(x) returns the absolute value, removing the sign
Math.sign(x) returns the sign of a number: +1 for positive, -1 for negative, and 0 for zero
Combines multiple values into a single string. Those values can be numbers (variables, parameters, or sensors), strings, booleans, or colors. This is useful for embedding in a speak command to do things like read out real-time sensor values.
Use the examples below to read out the yaw orientation every 5 seconds, and spin the robot around slowly like a top:
JavaScript:
async function startProgram() {
while (true) {
await speak(buildString('yaw orientation is', getOrientation().yaw), true);
await delay(5);
}
}
Python:
async def start_program():
while True:
await speak(build_string(['yaw orientation is', get_orientation()['yaw']]),
True)
await delay(5)
Parameters:
args {...any[]} - List of components to print and join
Returns:
{string} The concatenated string
Gets an individual color channel value of a color , from 0 - 255, where r is red, g is green, and b is blue.
To set your speed to the red channel of a purple color, use the following examples:
JavaScript: setSpeed({ r: 255, g: 0, b: 255 }.r)
Python: set_speed({'r': 255, 'g': 0, 'b': 255}['r'])
Returns a new color by modifying a single channel (red, green, blue) of a given color.
Use the examples below to set the red channel to zero for a white (255, 255, 255) main LED:
JavaScript: setMainLed({ r: 0, g: { r: 255, g: 255, b: 255 }.g, b: { r: 255, g: 255, b: 255 }.b })
Python: set_main_led({'r': 0, 'g': {'r': 255, 'g': 255, 'b': 255}['g'], 'b': {'r': 255, 'g': 255, 'b': 255}['b']})
Since the mixed channel in the color mixer takes a number, you can easily randomize or use sensor inputs to modify the channel. For example, to randomize the red channel between 50 and 100, use the following:
JavaScript: setMainLed({ r: getRandomInt(50, 100), g: { r: 255, g: 255, b: 255 }.g, b: { r: 255, g: 255, b: 255 }.b })
Python: set_main_led({'r': get_random_int(50, 100), 'g': {'r': 255, 'g': 255, 'b': 255}['g'], 'b': {'r': 255, 'g': 255, 'b': 255}['b']})
To match the red channel value to the speed of the robot use following:
JavaScript: setMainLed({ r: getSpeed(), g: { r: 255, g: 255, b: 255 }.g, b: { r: 255, g: 255, b: 255 }.b })
Python: set_main_led({'r': get_speed(), 'g': {'r': 255, 'g': 255, 'b': 255}['g'], 'b': {'r': 255, 'g': 255, 'b': 255}['b']})
Returns the smaller of the supplied numeric expressions:
JavaScript: Math.min(x, y)
Python: min(x, y)
Returns the larger of the supplied numeric expressions:
JavaScript: Math.max(x, y)
Python: max(x, y)
Enter 0-360° to get the ratio of sides in a right angle triangle. Notice that we convert radians to degrees for ease of use, but you could write your own trig functions to use radians if desired. To remember each formula try the mnemonic "SOH CAH TOA":
Returns the sine of x radians:
JavaScript: Math.sin(toRadians(x)
Python: Math.sin(to_radians(x)
Returns the cosine of x radians:
JavaScript: Math.cos(toRadians(x)
Python: Math.cos(to_radians(x)
Returns the tangent of x radians:
JavaScript: Math.tan(toRadians(x)
Python: Math.tan(to_radians(x)
Returns an angle between -PI/2 and PI/2, whose sine is x (x must be a number between -1 and 1):
JavaScript: toDegrees(Math.asin(x)
Python: to_degrees(Math.asin(x)
Returns an angle between 0 and PI radians, whose cosine is x (x must be a number between -1 and 1):
JavaScript: toDegrees(Math.acos(x)
Python: to_degrees(Math.acos(x)
Returns an angle between -PI/2 and PI/2 radians, whose tangent is x:
JavaScript: toDegrees(Math.atan(x)
Python: to_degrees(Math.atan(x)