0% found this document useful (0 votes)
11 views13 pages

08 Section5 CatAge Hot or Cold MicroPython EN

The document provides an overview of the accelerometer sensor in Micro:Bit, explaining how it detects motion across three axes (X, Y, Z) and introduces functions for programming gestures and acceleration values in MicroPython. It outlines two activities, 'Cat Age' and 'Hot or Cold', aimed at teaching students about the sensor's capabilities while incorporating mathematical concepts. The document includes detailed instructions for coding these activities, emphasizing the use of the accelerometer for interactive learning experiences.

Uploaded by

panda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views13 pages

08 Section5 CatAge Hot or Cold MicroPython EN

The document provides an overview of the accelerometer sensor in Micro:Bit, explaining how it detects motion across three axes (X, Y, Z) and introduces functions for programming gestures and acceleration values in MicroPython. It outlines two activities, 'Cat Age' and 'Hot or Cold', aimed at teaching students about the sensor's capabilities while incorporating mathematical concepts. The document includes detailed instructions for coding these activities, emphasizing the use of the accelerometer for interactive learning experiences.

Uploaded by

panda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Introduction

ACCELEROMETER SENSOR
One of the most interesting and beautiful sensors of Micro:Bit is the -accelerometer- sensor. Accelerometer will
give the value of the accelerations on the X-Y-Z planes of the Micro:Bit.

To explain this in a simpler way: when you move your Micro:Bit to the right and left, you make X plane motion. Y
plane motion is the plane where you move your Micro:Bit by tilting forward or backward. Finally, if you move the
Micro: Bit upwards and downwards, you will have made Z plane motion.

Since we don't live in a flat world, forces occur in three-dimensional space. If the motion of an object is not
exactly in an axis direction, we need a way to calculate the acceleration value from the values measured for all
axes.

If you put the Micro: Bit on a flat table and push it diagonally, you will gain acceleration in two dimensions (X, Y
axes). If you also decide to lift it from the table while doing this cross motion, another dimension will be added. In
other words, we also perform the motion in the third dimension (Z axis).

We can start using the defined functions in MicroPython for using the accelerometer sensor of Micro:Bit.

Code Functions That We Will Learn:


accelerometer.was_gesture("shake"): It rotates the true value when we shake the Micro:Bit. It runs
thanks to Micro:Bit’s accelerometer sensor perceiving the shaking
movement and giving result.

accelerometer.was_gesture("left"): It rotates the true value when we tilt the Micro:Bit to the left. It
runs by means of Micro:Bit’s accelerometer sensor perceiving the
movement on the right and left plane and giving result.

accelerometer.was_gesture("right"): It rotates the true value when we tilt the Micro:Bit to the right. It
runs by means of Micro:Bit’s accelerometer sensor perceiving the
movement on the right and left plane and giving result.
accelerometer.get_x(): It is the code block giving the acceleration value during the
horizontal movement of the Micro:Bit. It rotates a negative ( - ) or
pozitive ( + ) value according to the movement and direction of the
Micro:Bit.

accelerometer.get_y(): It is the code block giving the acceleration value during the vertical
movement of the Micro:Bit. It rotates a negative ( - ) or pozitive ( +
) value according to the movement and direction of the Micro:Bit.

abs(): It is the code block which returns the absolute value of the number
defined as parameter to itself.

© 2020 codementum.com
Targets/Learning Outcomes:
Students are informed about the movements of the Micro:Bit. They can observe that accelerometer
sensor makes Micro:Bit motion-sensitive and informed about the working of the accelerometer sensor.

Because of the content of the activity they can observe that there could be difficulties between the life span, life
phase (babyhood, childhood, youth, senility etc...)of the different living creatures.

In the Hot or Cold activity, they gain information and experience about absolute value calculation by abs ( )
function. At the same time, they can change the brightness of the Micro:Bit leds dynamically.

Activities:
 Cat Age
 Hot or Cold

Materials:
 Micro:Bit
 Micro Usb Cable
 Optional 3V battery slot

Entrance to the Lesson / Warm-up:


Before starting the lesson, you can ask your students to make a search about the comparison of age of
the different animals and age of human beings.

The difference between the ages of human being and cat is presented in the table below. You can ask your
students to create a similar table for different animals by means of visual processing programmes.

They will use four mathematical operations in this activity. They will observe that the age of cats advance
disproportionately to the age of human beings until a certain age limit and then continue with a constant
difference. This will determine the operations that we will perform.

When we give the application the command to calculate the age of our cat, they will learn to realise an
animation for the “loading” message.

© 2020 codementum.com
Activities:

Cat Age:
Watch The Project Video

Let's start the activity by creating a variable to store the human age and another variable to store the cat year and
let's assign their values as 0.

Then let's display a question mark “?” on the led screen for being able to input our first value .

We wanted to make a “loading” animation in order to leave a visual impression while making the calculation
between cat age and human age. We should design a big image for displaying in this animation. We should use
the “create big image” code block from the “image” section for this.

We need to create the “loading” variable, then design the big image and define it to the variable.

We must input year value which corresponds to the cat age by tilting the Micro:Bit to the right and left. So
we will benefit from the accelerometer sensor of the Micro:Bit. We will change the value of our “year” variable
by +1 or -1 each time we check the “tilt right” and “tilt left” functions and get the true value.

We should perform a half second waiting right before the change of “year” variable in order not to experience an
extra change out of aour control during the tilting of the Micro:Bit.

After “year” value has changed we should display its value on the led screen so that we can follow up the value.

While reducing the value of the “year” variable, let's not overlook a detail.

In order the value of the“year” variable not to be a negative number, we should compare its value with "if".

Only if the “year” value is greater than 0, then the -1 number should change.

© 2020 codementum.com
Now it's time to calculate the value of the year we entered according to the age of the cats:

• Firstly let's create an animation by creating a “for” loop, using the “shift_left()” function and sliding the image
we stored in our “loading” variable.

Let's perform this when.

• If the A button is pressed;

• “display.show(loading.shift_left( )” our image in the “loading” variable will start to slide in the intervals of 200
miliseconds to the left of the screen for each value of the for loop variable.

• When the animation is over, the cat age which corresponds to the human age value in the “year” variable will
be calculated and defined to “age” variable. Then the value in the “age” variable will be displayed on the led
panel by means of display.show( )”function.

• We need to make a calculation according to the table showing the differences between the human and cat ages
above.

© 2020 codementum.com
When the cat age is 1, the human age is 15

When the cat age is 2, the human age is 15 + 9 = 24

The human age for each age after the cat age of 2 differs by 4 years of age.

 For this reason, we must compare the value of “year” using the “if” check expression. When the “year” value
is equal to “1”, we need to equal the “age” variable to 15 and display it on the led screen.
 When the “year” value is equal to “2”, we need to equal the “age” variable to 15 + 9 and display it on the led
screen.
 When the “year” value is bigger than “2”, we need to equal the “age” variable to 15+9+(4x year)-2’ value and
display it on the led screen.

 After displaying the age conversion on the Led screen, we should restart all the values after shaking the
Micro:Bit.

© 2020 codementum.com
MicroPython Codes:
from microbit import *
year=0
age=0
display.scroll("?")
loading=Image("0000000000:0000000000:9090909090:00000
00000:0000000000")
while True:
if accelerometer.was_gesture("left"):
if (year>0):
year=year-1
display.show(year)
elif accelerometer.was_gesture("right"):
year=year+1
display.show(year)

elif button_a.was_pressed():
for item in range(0,5):
# TODO: write code...
display.show(loading.shift_left(item))
sleep(200)
if (year==1):
age=15
display.scroll(age)
elif (year==2):
age=15+9
display.scroll(age)
elif (year>2):
age=15+9+(4*(year-2))
display.scroll(age)
age=0
year=0
elif accelerometer.was_gesture("shake"):
year=0
age=0
display.scroll("?")

© 2020 codementum.com
Hot - or - Cold:
Watch The Project Video

What about making a version of the Hot – or – cold game adapted to the Micro:Bit?

We will determine a random point on the Micro:Bit led screen and try to find the hidden point by moving the lit
led by means of tilting the Micro:Bit to the right and left.

When we come closer to the target point by 2 units the brightness of our lit led will increase a little bit, and when
we come closer by 1 units it will increase a little bit more. And when we get to the target point "heart" emoji and
music voice will be welcoming us.

 First, let's start by adding the MicroPython libraries that we need to use while programming the application.
import random and import music

 The “accx and accy” variables will store the coordinates of the led which we will move according to the values
Micro:Bit gets from the accelerometer sensor simultaneously during the application. Let's create these
variables and enable them to get the value 0 for the start.

 We need two more variables for the Micro:Bit to select a point on a random coordinate. We should assign
random values between 0-4 to these variables.

 The “randomDotx” and “randomDoty” variables will be storing the coordinate of this random point. We
assign random numbersto these variables by the “random.randint(0,4)” function.

 In this section, finally let's declare the “brightness” variablein order to store the brightness of the led that we
will move and let's define the value 2 for a low brightness value.

© 2020 codementum.com
Let's arrange the “display.set_pixel (0,0,brightness)” function and enable the led on the 0,0 coordinate to
light up at the brightness value 2.

 Let's write the functions we want them to run as long as the Micro:Bit runs in this infinite loop with While
True:

 We determine whether the Micro:Bit is rotated right - left or up - down by checking the values we get from
the accelerometer sensor simultaneously.

 In the first “if” control block, we compare the value we receive from the “accelerometer.get_x()” function
with the value 800.
 If it is greater than 800, we need to change the value of the “accx”variable by +1.
 We know that the maximum sequence number of Micro:Bit leds is 4. We have to make another comparison
here.

 If the value of "accx" is greater than 5, we should assign the new value of “accx” as 0. Thus, after the moving
point disappears to the right of Micro:Bit led screen, it will start to be displayed again from the left.

 In the same logic order, we must also add the necessary codes for the movement of the moving led in the y
plane.
 This time, we should also compare the value we get from the “accelerometer.get_y()” function. We should
change the “accy” value and provide its checking by the value 800.

 As the next step, we need to define the values of the “differenceX” and “differenceY” variables. We will use
these variables to determine how close or far the led we move to the target led.

 We can determine the value of these variables by taking the absolute value of the result of the “accx –
randomDotx” operation.

 In order to get absolute value, we should use the abs ( ) function.

Ex. When accx = 0, it can be randomDotx = 4. In this case, the result of the “accx-randomDotx” operation will
be -4. However, we use this operation only to determine the distance. So by taking its absolute value, we
avoid of getting a negative value.

© 2020 codementum.com
 We should do the same for the “accy – randomDoty” operation.

 After this step, we should change the brightness of the moving led according their state of getting closer to
the target.

 We have to check the values of the “differenceX and differenceY” variables using the “if” block. If the value
of both is less than 2, let's define the “brightness” value as 8; if not; if it is less than 3, let's define the
“brightness” value as 5;
 If not; let's define the “brightness” value as 2 this time.

Thus, if the difference is less than 2 units, that is, if we are actually close by 1 unit, we increase the brightness,
if the difference is less than 3 units, that is, if we are actually close by 2 units, we increase the brightness
slightly. If there is the brightness of more than 2 units, we return the brightness to the one at the moment
that Micro:Bit starts running.

 Now it is time to edit our codes that light up our moving led and adjust its brightness.

 According to the values in the “accx, accy and brightness” variables, we can light up the moving led by using
our variables by the display.set_pixel( accx, accy, brightness) function after clearing the screen first and then
waiting for 500 milliseconds.

 Let's define 500 milliseconds waiting time between each movement of the moving led by using the sleep(500)
function. Thus, we prevent the led from getting out of our control by moving very fast.

 During the game, our distance may increase, as it may decrease. The brightness of the moving led may also
increase or decrease according to this. At the end of an endured game, we may encounter the heart icon and
the victory music.

© 2020 codementum.com
 Finally, here we need to check whether we are in the right position on the x axis and y axis at the same time
within if. So let's not forget to use the “and” operator.

 If the x coordinate of the moving led; “accx” is equal to the x coordinate of the hidden led; “randomDotx”
and if the y coordinate of the moving led; “accy” is equal to the y coordinate of the hidden led;
“randomDoty”;

 Let's light up the led on the “accx” and “accy” coordinates by using the “display.set_pixel( ) function at the
brightness of the value we store in the brightness variable.

 Let's play the “power up” melody by using the Music.play( music.POWER_UP) function and display the
“heart” emoji on the Micro:Bit led panel.

 Since we reached the target, let's equalise the value of the “accx” and “accy” variables to 0 and clear the
screen.

That is all. Have fun.

© 2020 codementum.com
MicroPython Codes:
from microbit import *
import random
import music
accx=0
accy=0
brightness=2
randomDotX=random.randint(0,4)
randomDotY=random.randint(0,4)
differenceX=0
differenceY=0
display.set_pixel(0,0,brightness)
while True
if (accelerometer.get_x()>800):
accx=accx+1
if (accx>4):
accx=0
if (accelerometer.get_y()>800):
accy=accy+1
if (accy>4):
accy=0

differenceX=abs(accx-randomDotX)
differenceY=abs(accy-randomDotY)
if (differenceX<2 and differenceY<2):
brightness=8
elif (differenceX<3 and differenceY<3):
brightness=5
else:
brightness=2
display.clear()
sleep(500)
display.set_pixel(accx,accy,brightness)
sleep(500)
if (accx==randomDotX and
accy==randomDotY):
display.set_pixel(accx,accy,brightness)
music.play(music.POWER_UP)
display.show(Image.HEART)
sleep(1000)
accx=0
accy=0
display.clear()

© 2020 codementum.com
Developing Options
 You can create the Cat Age application for different animals as well.
 You can make Cat Age application to calculate according to the values of human being before getting older.
For examplewhen the human baby is 3 months old, how old is the cat?
 In the Hot – or – cold application you can tilt the Micro:Bit towards the 4 directions and move the moving led
towards the 4 directions as well.
 In the Hot – or – cold application you can make it count in how many steps you reached the target and you
can make score calculation.
 You can limit the number of steps for reaching the target in the Hot – or – cold application. If thetarget is not
reached within a limited number of steps you can make it play a different music tone.

Evaluation Questions:
1- Which is the result of the abs(3-4) function of the Micro:Bit?

A. -4
B. 12
C. 1
D. -12

2- Which of the code blocks below should we use in order the (4,2) coordinate of the led screen to be displayed when
we tilt the Micro:Bit to the right?

A) if accelerometer.was_gesture("left"): B) if accelerometer.was_gesture("right"):

display.set_pixel(4,2) display.set_pixel(4,2)

C) if accelerometer.was_gesture("right"): D) if accelerometer.was_gesture("left"):

display.set_pixel(4,2) display.set_pixel(4,2)

3- The code function below can rotate which value below when we tilt the Micro:Bit to the left?

accelerometer.get_x():

A. 1023
B. 0
C. -700
D. 700

4- The code block below rotates value after which movement of the Micro:Bit?

accelerometer.get_y():

A. When A buton is pressed


B. When the Micro:Bit is tilted forward
C. When the Micro:Bit is tilted to the left
D. When reset button is pressed

© 2020 codementum.com
USA Standards
CSTA: 1B-AP-08 - 1B-AP-11 - 1B-AP-12 - 1B-AP-15
ISTE: 4a- 4c- 4d – 5a – 5b – 5c - 6b – 6c
CSCA: 3-5.AP.10 - 3-5.AP.11 - 3-5.AP.12 - 3-5.AP.13 - 3-5.AP.14

UK Standards
 Design, write and debug programs that accomplish specific goals, including controlling or simulating physical
systems; solve problems by decomposing them into smaller parts
 Use logical reasoning to explain how some simple algorithms work and to detect and correct errors in
algorithms and programs
 Understand computer networks, including the internet; how they can provide multiple services, such as the
World Wide Web, and the opportunities they offer for communication and collaboration
 Use technology safely, respectfully and responsibly; recognise acceptable/unacceptable behaviour; identify a
range of ways to report concerns about content and contact

© 2020 codementum.com

You might also like