Micromonsters
Published © MIT

Magic Button Trick

Build a magic trick to amaze your friends that uses the micro:bit’s compass to detect a nearby magnet!

BeginnerFull instructions provided6 minutes2,400
Magic Button Trick

Things used in this project

Story

Read more

Code

Full code

Typescript
Copy and paste this into the "JavaScript" view on pxt.microbit.org
let isSwitched = false
let force = 0
input.onButtonPressed(Button.A, () => {
    if (isSwitched) {
        basic.showString("B")
    } else {
        basic.showString("A")
    }
})
input.onButtonPressed(Button.B, () => {
    if (isSwitched) {
        basic.showString("A")
    } else {
        basic.showString("B")
    }
})
basic.forever(() => {
    force = Math.abs(input.magneticForce(Dimension.Strength))
    isSwitched = force > 100
})

Credits

Micromonsters

Micromonsters

0 projects • 2 followers
We are a Dad and Daughter coding superhero duo who love to write code and build thing with the microbit

Comments