Making a point
An empty gesture
Learn It
- We looked at the accelerometer in the previous lesson. This can also be used to pick up data about the movement of the Micro:bit, in the form of gestures.
- We can write programs to take advantage of these; my favourites are
face up
,face down
andshake
. Let's try using one now. - Note: You can read more about gestures here.
- Try this:
from microbit import * while True: gesture = accelerometer.current_gesture() if gesture == "shake": display.show(Image.HAPPY) sleep(2000) else: display.show(Image.ASLEEP)
Compass
Learn It
- The Micro:bit also has a built-in electronic compass.
- It reads the Earth's magnetic field, and uses this to give a bearing on which way the Micro:bit is facing, from 0 to 359 degrees. 0 degrees is North, 90 is East, 180 is South and so on.
- The compass will need calibrating first so that it knows which way to point.
- Once done, you can either show the bearing number on the screen, or be a little more ambitious and use an LED on the display to point out the direction.
- Let's start simple. Our pseudocode for this is:
CALIBRATE COMPASS WHILE True myHeading = READ_COMPASS_HEADING PRINT myHeading END WHILE
- Note: You can get more detail on the capabilities of the compass by looking through the documentation for MicroPython here.
Code It
- Let's write some code now…
from microbit import * compass.calibrate() while True: myHeading = compass.heading() display.scroll(str(myHeading))
- After you complete the calibration sequence (tilt and roll the Micro:bit around in a circle), you'll get readings showing which way is North.
- NOTE: If you're using this inside, nearby metal objects and equipment using magnets (e.g. speakers, motors) can affect the accurate of the compass quite significantly).
Learn It
- Using variables and
If
statements, we can make something a little more like a compass. - I'd like to make the Micro:bit show a letter to indicate direction as I rotate the Micro:bit round. It won't be super-accurate, but it'll give an indication for the user.
- From 46 to 135 degrees, we'll show E.
- From 136 to 225 degrees, we'll show S.
- From 226 to 315 degrees, we'll show W.
- Finally, let's say that for a bearing of less than 46 or greater than 316 degrees, the Micro:bit should say N.
- Our pseudocode will need to be something like this:
CALIBRATE COMPASS WHILE True myHeading = READ_COMPASS_HEADING IF myHeading < 46 or myHeading > 316 PRINT "N" ELSE IF myHeading < 136 PRINT "E" ELSE IF myHeading < 226 PRINT "S" ELSE PRINT "W" END IF END WHILE
Code It
- Let's write the program…
from microbit import * compass.calibrate() while True: myHeading = compass.heading() if myHeading < 46 or myHeading > 316: display.show("N") elif myHeading < 136: display.show("E") elif myHeading < 226: display.show("S") else: display.show("W")
- If you're struggling to understand where the numbers above come from, the video tutorial below goes through it in more detail:
Badge It
- Silver: Write a program to show a smiley face when the Micro:bit is pointing North, and a different image when pointing South.
- Gold: Add an emergency feature to your digital compass by making the display "SOS" across the display when the A button is pushed, and if B is pushed, flashing all the LEDs to spell out "SOS" in Morse code.
- Platinum: Use the LED display to make the Micro:bit always point out North as is is rotated. i.e. If the Micro:bit is pointing North, the LED located at (2, 0) should be on. If it's pointing West the LED at (4, 2) should be on, pointing North-West should turn on (4, 0) and so on.