pxt-calliope/docs/projects/compass.md

103 lines
2.1 KiB
Markdown
Raw Normal View History

2017-12-07 05:23:17 +01:00
# Compass
2016-06-12 03:12:08 +02:00
## ~avatar avatar
2016-06-12 03:12:08 +02:00
2017-12-07 05:23:17 +01:00
Welcome! This guided tutorial will show you how to program a script that displays which direction the @boardname@ is pointing. Let's get started!
2016-06-12 03:12:08 +02:00
## ~
2016-06-12 03:12:08 +02:00
2017-12-07 05:23:17 +01:00
![](/static/mb/projects/a5-compass.png)
Display the direction that the @boardname@ is facing using the compass.
2016-06-12 01:43:54 +02:00
## Step 1
2016-06-12 03:12:08 +02:00
Create a loop that will continuously update the reading of the compass.
2016-06-12 01:43:54 +02:00
```blocks
basic.forever(() => {
2016-06-12 03:12:08 +02:00
})
2016-06-12 01:43:54 +02:00
```
## Step 2
2016-11-02 01:44:37 +01:00
Store the reading of the @boardname@ in a variable called `degrees`.
2016-06-12 01:43:54 +02:00
```blocks
basic.forever(() => {
2016-06-12 03:12:08 +02:00
let degrees = input.compassHeading()
})
```
## Step 3
2017-06-05 06:28:11 +02:00
If `degrees` is less than `45` or greater than `315`,
2017-12-07 05:23:17 +01:00
then the compass heading is mostly pointing toward **North**. Display `N` on the @boardname@.
2016-06-12 03:12:08 +02:00
```blocks
basic.forever(() => {
let degrees = input.compassHeading();
2017-06-05 06:28:11 +02:00
if (degrees < 45 || degrees > 315) {
2016-06-12 01:43:54 +02:00
basic.showString("N");
2016-06-12 03:12:08 +02:00
}
2016-06-12 01:43:54 +02:00
});
```
2016-06-12 03:12:08 +02:00
## Step 4
2017-12-07 05:23:17 +01:00
If `degrees` is less than `135`, the @boardname@ is mostly pointing **East**. Display `E` on the @boardname@.
2016-06-12 01:43:54 +02:00
```blocks
basic.forever(() => {
2016-06-12 03:12:08 +02:00
let degrees = input.compassHeading();
2017-06-05 06:28:11 +02:00
if (degrees < 45 || degrees > 315) {
2016-06-12 01:43:54 +02:00
basic.showString("N");
2016-06-12 03:12:08 +02:00
}
else if (degrees < 135) {
2016-06-12 01:43:54 +02:00
basic.showString("E");
2016-06-12 03:12:08 +02:00
}
2016-06-12 01:43:54 +02:00
});
```
2016-06-12 03:12:08 +02:00
## Step 5
2017-12-07 05:23:17 +01:00
If `degrees` is less than `225`, the @boardname@ is mostly pointing **South**. Display `S` on the @boardname@.
2016-06-12 01:43:54 +02:00
```blocks
basic.forever(() => {
2016-06-12 03:12:08 +02:00
let degrees = input.compassHeading();
2017-06-05 06:28:11 +02:00
if (degrees < 45 || degrees > 315) {
2016-06-12 01:43:54 +02:00
basic.showString("N");
2016-06-12 03:12:08 +02:00
}
else if (degrees < 135) {
2016-06-12 01:43:54 +02:00
basic.showString("E");
2016-06-12 03:12:08 +02:00
}
else if (degrees < 225) {
2016-06-12 01:43:54 +02:00
basic.showString("S");
}
});
```
2016-06-12 03:12:08 +02:00
## Step 6
2016-06-12 01:43:54 +02:00
2017-12-07 05:23:17 +01:00
If none of these conditions returned true, then the @boardname@ must be pointing **West**. Display `W` on the @boardname@.
2016-06-12 01:43:54 +02:00
```blocks
basic.forever(() => {
2016-06-12 03:12:08 +02:00
let degrees = input.compassHeading();
2017-06-05 06:28:11 +02:00
if (degrees < 45 || degrees > 315) {
2016-06-12 01:43:54 +02:00
basic.showString("N");
2016-06-12 03:12:08 +02:00
}
else if (degrees < 135) {
2016-06-12 01:43:54 +02:00
basic.showString("E");
2016-06-12 03:12:08 +02:00
}
else if (degrees < 225) {
2016-06-12 01:43:54 +02:00
basic.showString("S");
2016-06-12 03:12:08 +02:00
}
else {
2016-06-12 01:43:54 +02:00
basic.showString("W");
}
});
2016-06-12 03:12:08 +02:00
```