Skip to content

Instantly share code, notes, and snippets.

@grkvlt
Created January 25, 2019 02:55
Show Gist options
  • Save grkvlt/8e262daef74ce73e9277e725152f18db to your computer and use it in GitHub Desktop.
Save grkvlt/8e262daef74ce73e9277e725152f18db to your computer and use it in GitHub Desktop.
Code for BBC micro:bit using gyroscope and compass
let heading = 0
let compass = 0
let roll = 0
let gyroscope = 0
let pitch = 0
input.onButtonPressed(Button.A, function () {
gyroscope = 0
compass = 1
basic.showLeds(`
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
`)
})
input.onButtonPressed(Button.B, function () {
gyroscope = 1
compass = 0
basic.showLeds(`
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
`)
})
pitch = 3
roll = 3
gyroscope = 1
compass = 0
heading = 0
basic.forever(function () {
if (gyroscope == 1) {
led.unplot(roll, pitch)
roll = Math.map(Math.constrain(input.rotation(Rotation.Roll) + 45, 0, 90), 0, 90, 0, 4)
pitch = Math.map(Math.constrain(input.rotation(Rotation.Pitch) + 45, 0, 90), 0, 90, 0, 4)
led.plot(roll, pitch)
}
if (compass == 1) {
heading = Math.map(input.compassHeading(), 0, 359, 0, 4)
if (heading > 3.5 || heading >= 0 && heading < 0.5) {
basic.showLeds(`
. . # . .
. # . # .
. . . . .
. . . . .
. . . . .
`)
} else if (heading >= 0.5 && heading < 1.5) {
basic.showLeds(`
. . . . .
. # . . .
# . . . .
. # . . .
. . . . .
`)
} else if (heading >= 1.5 && heading < 2.5) {
basic.showLeds(`
. . . . .
. . . . .
. . . . .
. # . # .
. . # . .
`)
} else {
basic.showLeds(`
. . . . .
. . . # .
. . . . #
. . . # .
. . . . .
`)
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment