micro:bit (Part 1)
W10 micro:bit
Part 1
this week i explored the "magic button trick" project.
the coding is very simple and allows you to play a magic trick (as long as you have a piece of magnet in your hand).
the coding is as follows:
translating the blocks into javascript:
this trick is interesting because it uses a measurement of "force" - which tells the micro:bit if a magnet is close by.
if it senses a magnet, it will automatically switch its A and B buttons.
the javascript:
let is_switched = false
let force = 0
input.onButtonPressed(Button.A, () => {
if (is_switched) {
basic.showString("B")
} else {
basic.showString("A")
}
})
input.onButtonPressed(Button.B, () => {
if (is_switched) {
basic.showString("A")
} else {
basic.showString("B")
}
})
force = input.magneticForce(Dimension.Strength)
is_switched = force > 100
basic.forever(() => {
force = Math.abs(input.magneticForce(Dimension.Strength))
is_switched = force > 100
})
Comments
Post a Comment