Provide basic data, use basic commands, use systematic processes.
First plan, what code do you need?
This program uses a > 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.