Cover image
    Activity

    Code a micro:bit to Move when Moved

    Trigger the servo's motion when moving the micro:bit.

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

    Code

    Move when Moved

    00:10

    Continuously monitor the acceleration strength for motion detection. If it goes over 1200, transition the Servo A position back and forth.

    basic.forever(function () {
        if (input.acceleration(Dimension.Strength) > 1200) {
            sb.transitionServoPosition(sb.servo(SBServo.ServoA), 100, 1, sb.easing(SBEasing.Linear))
            sb.transitionServoPosition(sb.servo(SBServo.ServoA), 0, 1, sb.easing(SBEasing.Linear))
        }
    })

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