2016-03-26 00:47:20 +01:00
|
|
|
# compass activity
|
|
|
|
|
2016-11-02 01:44:37 +01:00
|
|
|
Display the direction that the @boardname@ is facing using the compass
|
2016-03-26 00:47:20 +01:00
|
|
|
|
2017-09-07 22:42:08 +02:00
|
|
|
## ~avatar avatar
|
2016-03-26 00:47:20 +01:00
|
|
|
|
2016-11-02 01:44:37 +01:00
|
|
|
Welcome! This guided tutorial will show you how to program a script that displays the direction the @boardname@ is pointing. Let's get started!
|
2016-03-26 00:47:20 +01:00
|
|
|
|
2017-09-07 22:42:08 +02:00
|
|
|
## ~
|
2016-03-26 00:47:20 +01:00
|
|
|
|
|
|
|
Create a loop that will continuously update the reading of the compass.
|
|
|
|
|
|
|
|
|
|
|
|
```blocks
|
|
|
|
basic.forever(() => {
|
|
|
|
|
|
|
|
})
|
|
|
|
```
|
|
|
|
|
2016-11-02 01:44:37 +01:00
|
|
|
Store the reading of the @boardname@ in a variable called `degrees`.
|
2016-03-26 00:47:20 +01:00
|
|
|
|
|
|
|
```blocks
|
|
|
|
basic.forever(() => {
|
|
|
|
let degrees = input.compassHeading()
|
|
|
|
})
|
|
|
|
```
|
|
|
|
|
2017-03-29 15:47:52 +02:00
|
|
|
If `degrees` is less than `45` or greater than `315`, then the compass heading is mostly pointing toward North. Display `N` on the @boardname@.
|
2016-03-26 00:47:20 +01:00
|
|
|
|
|
|
|
```blocks
|
|
|
|
basic.forever(() => {
|
2016-04-13 17:51:40 +02:00
|
|
|
let degrees = input.compassHeading();
|
2017-03-29 15:47:52 +02:00
|
|
|
if (degrees < 45 || degrees > 315) {
|
2016-03-26 00:47:20 +01:00
|
|
|
basic.showString("N");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
```
|
2017-03-29 15:47:52 +02:00
|
|
|
|
2016-11-02 01:44:37 +01:00
|
|
|
If `degrees` is less than 135, the @boardname@ is mostly pointing East. Display `E` on the @boardname@.
|
2016-03-26 00:47:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
```blocks
|
|
|
|
basic.forever(() => {
|
2016-04-13 17:51:40 +02:00
|
|
|
let degrees = input.compassHeading();
|
2017-03-29 15:47:52 +02:00
|
|
|
if (degrees < 45 || degrees > 315) {
|
2016-03-26 00:47:20 +01:00
|
|
|
basic.showString("N");
|
|
|
|
}
|
|
|
|
else if (degrees < 135) {
|
|
|
|
basic.showString("E");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
2016-11-02 01:44:37 +01:00
|
|
|
If `degrees` is less than 225, the @boardname@ is mostly pointing South. Display `S` on the @boardname@.
|
2016-03-26 00:47:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
```blocks
|
|
|
|
basic.forever(() => {
|
2016-04-13 17:51:40 +02:00
|
|
|
let degrees = input.compassHeading();
|
2017-03-29 15:47:52 +02:00
|
|
|
if (degrees < 45 || degrees > 315) {
|
2016-03-26 00:47:20 +01:00
|
|
|
basic.showString("N");
|
|
|
|
}
|
|
|
|
else if (degrees < 135) {
|
|
|
|
basic.showString("E");
|
|
|
|
}
|
|
|
|
else if (degrees < 225) {
|
|
|
|
basic.showString("S");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
|
|
|
|
2017-03-29 15:47:52 +02:00
|
|
|
If none of these conditions are true, then the @boardname@ must be pointing West. Display `W` on the @boardname@.
|
2016-03-26 00:47:20 +01:00
|
|
|
|
|
|
|
```blocks
|
|
|
|
basic.forever(() => {
|
2016-04-13 17:51:40 +02:00
|
|
|
let degrees = input.compassHeading();
|
2017-03-29 15:47:52 +02:00
|
|
|
if (degrees < 45 || degrees > 315) {
|
2016-03-26 00:47:20 +01:00
|
|
|
basic.showString("N");
|
|
|
|
}
|
|
|
|
else if (degrees < 135) {
|
|
|
|
basic.showString("E");
|
|
|
|
}
|
|
|
|
else if (degrees < 225) {
|
|
|
|
basic.showString("S");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
basic.showString("W");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
2017-09-07 22:42:08 +02:00
|
|
|
## ~avatar avatar
|
2016-03-26 00:47:20 +01:00
|
|
|
|
2016-04-13 17:27:45 +02:00
|
|
|
Excellent, you're ready to continue with the [challenges](/lessons/compass/challenges)!
|
2016-03-26 00:47:20 +01:00
|
|
|
|
2017-09-07 22:42:08 +02:00
|
|
|
## ~
|
2016-03-26 00:47:20 +01:00
|
|
|
|