Cover image
    Activity

    Code a micro:bit to Change Position with Button Press

    Press and hold a button for maintaining a position.

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

    Code

    Change Position with Button Press

    00:10

    Use a loop to continuously check if Button A is pressed, and then set the position of the Servo A to either 0 (when pressed) or 100 (while not pressed).

    basic.forever(function () {
        if (input.buttonIsPressed(Button.A)) {
            sb.setServoPosition(sb.servo(SBServo.ServoA), 0)
        } else {
            sb.setServoPosition(sb.servo(SBServo.ServoA), 100)
        }
    })

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