Cover image
    Activity

    Code a micro:bit to Change Color with Button Press

    Switch between red and blue lights.

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

    Code

    Change Color with Button Press

    00:10

    Use a loop to continuously check if Button A is pressed, and then set the color of the RGB LED A to either red,when pressed, or blue,while not pressed.

    basic.forever(function () {
        if (input.buttonIsPressed(Button.A)) {
            sb.setRgbLedColorRGB(sb.rgbLed(SBRgbLed.RgbLedA), 100, 0, 0)
        } else {
            sb.setRgbLedColorRGB(sb.rgbLed(SBRgbLed.RgbLedA), 0, 0, 100)
        }
    })

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