pxt-microbit
Version:
micro:bit target for Microsoft MakeCode (PXT)
122 lines (95 loc) • 3.02 kB
Markdown
# Compass
## {Introduction }
This tutorial shows you how to create a program that displays which direction the @ is pointing. Let's get started!

## {Step 1}
First, store the ``||input:compass heading||`` of the @ in a variable called ``||variables:degrees||`` in the ``||basic:forever||`` loop.
```blocks
basic.forever(function() {
let degrees = input.compassHeading()
})
```
## {Step 2}
``||logic:If||`` ``||variables:degrees||`` is ``||logic:less than||`` `45`,
then the compass heading is mostly pointing toward **North**. ``||basic:Show||`` `N` on the @.
```blocks
basic.forever(function() {
let degrees = input.compassHeading()
if (degrees < 45) {
basic.showString("N")
}
})
```
## {Step 3}
``||logic:If||`` ``||variables:degrees||`` is less than `135`, the @ is mostly pointing **East**. ``||basic:Show||`` `E` on the @.
```blocks
basic.forever(function() {
let degrees = input.compassHeading()
if (degrees < 45) {
basic.showString("N")
}
else if (degrees < 135) {
basic.showString("E")
}
})
```
## {Step 4}
Go to the simulator and rotate the @ logo to simulate changes in the compass heading.
## {Step 5}
``||logic:If||`` ``||variables:degrees||`` is less than `225`, the @ is mostly pointing **South**. ``||basic:Show||`` `S` on the @.
```blocks
basic.forever(function() {
let degrees = input.compassHeading()
if (degrees < 45) {
basic.showString("N")
}
else if (degrees < 135) {
basic.showString("E")
}
else if (degrees < 225) {
basic.showString("S")
}
})
```
## {Step 6}
``||logic:If||`` ``||variables:degrees||`` is less than `315`, the @ is mostly pointing **West**. ``||basic:Show||`` `W` on the @.
```blocks
basic.forever(function() {
let degrees = input.compassHeading()
if (degrees < 45) {
basic.showString("N");
}
else if (degrees < 135) {
basic.showString("E")
} else if (degrees < 225) {
basic.showString("S")
} else if (degrees < 315) {
basic.showString("W")
}
})
```
## {Step 7}
``||logic:If||`` none of these conditions returned true, then the @ must be pointing **North** again. Display `N` on the @.
```blocks
basic.forever(function() {
let degrees = input.compassHeading()
if (degrees < 45) {
basic.showString("N");
}
else if (degrees < 135) {
basic.showString("E");
}
else if (degrees < 225) {
basic.showString("S");
}
else if (degrees < 315) {
basic.showString("W")
}
else {
basic.showString("N")
}
})
```
## {Step 9 }
If you have a @, click `|Download|` and follow the screen instructions. You will have to follow the screen instructions to calibrate your compass.
https://youtu.be/IL5grHtz_MU