Cover image
    Activity

    Code a micro:bit to Tilt and Change Color

    Tilt the micro:bit to change color.

    Coding
    Get Started
    8-14+
    40:00
    micro:bit Coding Cards

    Code

    Tilt to Change Color

    00:10

    Continuously store the value of the x axis of the accelerator in the variable movement. Since the accelerator gives readings in the range -1023 to 1023, use a map to convert it to the range 0 to 100 and store it in the hue variable. Set the hue of the RGB LED A to the value of the hue variable.

    let movement = 0
    let hue = 0
    basic.forever(function () {
        movement = input.acceleration(Dimension.X)
        hue = Math.map(movement, -1023, 1023, 0, 100)
        sb.setRgbLedColorHSB(sb.rgbLed(SBRgbLed.RgbLedA), hue, 100, 100)
    })

    If using micro:bit for the first time, follow this setup.