The micro:bit contains an accelerometer. This sensor can be used to measure the orientation of the Finch. For example, you can detect when the Finch is upside down. The micro:bit accelerometer is very similar to the accelerometer that enables a smartphone or tablet to change the screen from portrait to landscape when the user turns the device.
Lesson 8 – Finch Accelerometer
Finding Finch Orientation
Use the getOrientation() method to find the position of the Finch. This method does not require any parameters. It returns a string that represents the orientation of the Finch. The possible values are “Beak up”, “Beak down”, “Tilt left”, “Tilt right”, “Level”, “Upside down”, and “In between”. “In between” is any orientation that is not one of the other six positions.
Program the Finch to turn in a wide arc until you pick up the Finch and turn it upside down.
When using the accelerometer, you often want to make your robot respond to multiple orientations of the accelerometer. In this case, you need an if-elif-else statement. The if-elif-else statement checks multiple Boolean expressions. For example, in this code, the first if checks whether the Finch is tilted to the left. If it is, the beak turns green. If it isn’t, then the program checks whether the Finch is tilted to the right. If it is, the beak turns purple. If the Finch isn’t tilted to the right or the left, the beak turns blue. You can add as many elifs as you want to check different Boolean expressions!
Try out the code above. Then modify it to add other colors when the Finch is in other positions. How many colors can you add?
micro:bit V2 Extra Challenge
If your Finch contains a micro:bit V2, you can also use a temperature sensor in the micro:bit to measure the temperature around the Finch in degrees Celsius. The getTemperature() method returns the value of this sensor. Room temperature is about 20°C. If you don’t have a micro:bit V2 in your Finch, you will see an error when you use this method.
Write a program that uses the Finch’s lights to tell you when the Finch is too hot, too cold, or just right.
Using the Accelerometer
The getOrientation() method calculates the position of the Finch based on measurements taken by the accelerometer in the micro:bit. The accelerometer measures any force that causes acceleration in the x-, y-, or z-direction. Because the Finch is usually moving pretty slowly, the accelerometer mostly measures the acceleration due to gravity. When the Finch is sitting on a level surface, the acceleration due to gravity points through the base of the Finch, along the negative z-axis. If you instead position the Finch so that its beak is pointing at the sky, the acceleration due to gravity points along the positive y -axis. The accelerometer enables you to measure these differences.
The getAcceleration() method returns a three float values that measure the acceleration in m/s2 in the x-, y-, and z-directions; these values are roughly between -10 and 10. The code below stores the three values in variables with informative names.
Write a program that repeatedly prints the x-, y-, and z-values for acceleration to the screen. Remember to read new acceleration values within the loop. The program should stop when the Finch detects that it is dark. Test your program by tilting the Finch in different directions. When is the z-acceleration close to 10? How can you detect that the Finch’s left wing is pointing at the ground?
Write a program that uses the x-, y-, and z-values for acceleration to set the amount of red, green, and blue in the Finch’s beak and tail. The program should run until you press button A.
Turn your Finch into a timer. Allow the user to input a number of seconds. When that time is up, the Finch should do whatever is necessary to get the user’s attention. The Finch should continue to act until the user shakes the Finch with an acceleration that has a magnitude greater than 20 m/s2 squared.
Note: Remember that the acceleration is each direction is between -10 and 10 m/s2 squared. You will need to combine the acceleration measurements in each direction to find the total magnitude of the acceleration: a = math.sqrt(ax*ax+ ay*ay + az*az). You will need to import the math library.
In this exercise, you will make the Finch climb a hill. You will need a board or table that you can tilt to make your “hill.” It should be tilted about 15°.
- Start by making your robot turn VERY SLOWLY until the x-acceleration is close to 0. When this part of your program is finished, the Finch should be pointed either uphill or downhill.
- Now your Finch is ready to drive, but you have to figure out whether it is pointed uphill or downhill. Which acceleration measurement can you use to determine that?
- If your Finch is pointed uphill, drive forward up the hill. If your Finch is pointed downhill, drive backwards up the hill instead!