Cover image
    Activity

    Code a micro:bit to Gradually Change Brightness

    Incrementally increase the brightness of an RGB LED.

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

    Code

    Gradually Change Brightness

    00:10

    Create a variable brightness to store a value and then continuously set the RGB LED A brightness to its value. Every time Button A is pressed, incrementally increase the variable by 4 units, and constrain it so that its value doesn't go over 100.

    input.onButtonPressed(Button.A, function () {
        position = Math.constrain(position + 4, 0, 100)
    })
    let position = 0
    position = 0
    basic.forever(function () {
        sb.setServoPosition(sb.servo(SBServo.ServoA), position)
    })

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