Code

Add code to make the Milky Monster move.

Duration: ~30 minutes

Step 1: Calibrate servo

In order for the Milky Monster to move, the micro:bit needs to command the servo to go between 0 and 180 degrees at a certain pace. In the code below:

  • Press button A to switch the servo to 180 degrees (to close the mouth of Milky Monster).
  • Press button B to switch the servo to 0 degrees (to open the mouth of Milky Monster).
input.onButtonPressed(Button.A, () => {
    pins.servoWritePin(AnalogPin.P0, 180)
    basic.showNumber(180)
})
input.onButtonPressed(Button.B, () => {
    pins.servoWritePin(AnalogPin.P0, 0)
    basic.showNumber(0)
})
basic.showString("calibrate")

Step 2: Attach rotor

The servo should be positioned at 180 degrees before attaching the rotor to it. This will make sure the mouth of the Milky Monster is closed once the servo reaches 180 degrees.

You can use a philips screw driver to attach the rotor to the servo.

Step 3: Check calibration

When the user presses A the servo rotor should be in ‘up’ position.

Step 4: Connect cord to rotor

Connect the cord to the servo rotor while mouth of Milky Monster is closed.

Step 5: Connect cables to micro:bit

Connect cables from micro:bit to the servo and place the battery.

Step 6: Code light sensor

Code the light sensor on the micro:bit to control the servo.

basic.forever(() => {
    pins.servoWritePin(AnalogPin.P0, input.lightLevel())
    led.plotBarGraph(
        input.lightLevel(),
        0
    )
})

Step 7: Ready!

Your Milky Monster is ready!


NEXT: Connect