pxt-calliope/olddocs/lessons/compass/challenges.md
Tom Ball f4eca66648 move lessons out of web site
will move select lessons back to "educators" section
2016-06-14 11:49:58 -04:00

1.7 KiB

compass challenges

Display the direction that the micro:bit is facing using the compass

Before we get started

Complete the following guided tutorial, your code should look like this:

 let degrees = 0;
basic.forever(() => {
    degrees = input.compassHeading();
    if (degrees < 45) {
        basic.showString("N");
    }
    else if (degrees < 135) {
        basic.showString("E");
    }
    else if (degrees < 225) {
        basic.showString("S");
    }
    else {
        basic.showString("W");
    }
});

Challenge 1

Instead of displaying N when the BBC micro:bit is pointing North, display a star to indicate the north star.

 let degrees = 0;
basic.forever(() => {
    degrees = input.compassHeading();
    if (degrees < 45) {
        basic.showLeds(`
# . # . #
. # # # .
# # # # #
. # # # .
# . # . #`);
    }
    else if (degrees < 135) {
        basic.showString("E");
    }
    else if (degrees < 225) {
        basic.showString("S");
    }
    else {
        basic.showString("W");
    }
});
  • Run your code to see if it works as expected

Challenge 2

Instead of displaying just N, W, S, or E, display the full word.

 let degrees = 0;
basic.forever(() => {
    degrees = input.compassHeading();
    if (degrees < 45) {
        basic.showString("NORTH");
    }
    else if (degrees < 135) {
        basic.showString("EAST");
    }
    else if (degrees < 225) {
        basic.showString("SOUTH");
    }
    else {
        basic.showString("WEST");
    }
});
  • Run your code to see if it works as expected

Challenge 3

Display your own unique message for each direction.