Digital Maestro Magazine digitalmaestro.org

Micro Bit

An Introduction Program buttons Use the motion sensor Develop a coin toss game Develop a dice game

August 1, 2018

Micro Bit: An introduction â&#x20AC;˘ Digital Maestro Magazine

I purchased a Micro Bit several months ago and though it was time to explore what this small device can do. It is amazing what this device does with a few lines of code. There is a lot students can learn with the built in components. I developed two applications that explore probability and randomness. Use the coin toss and dice roll apps to collect data. Graph the data and explore what it means. How does it relate to probability? How do the results relate to randomness? Alex Reyes

Page 2

Digital Maestro Magazine â&#x20AC;˘ Micro Bit: An introduction

4

Microbit simulator

6

The Basics

8

Start Vs. Forever

11

Input and Output

12

Button Input

12

Sensor Input

15

Motion Sensor

18

Logic and Loops

19

Multiple conditions

21

A Math Lesson

22

Efficient Dice Roll

26

Animated toss and roll

26

Page 3

Micro Bit: An introduction â&#x20AC;˘ Digital Maestro Magazine

The Introduction

The microcontroller on the Micro Bit is comparable to other microcontrolMirobit is a device that originated with lers. Skills learned on the Micro Bit are the BBC in the UK. This device is avail- applicable to other devices using similar able to students in the UK for computer microcontrollers. education. The goal is to provide an affordable way to teach students The Micro Bit uses a 32bit ARM computer technology and programming. processor. The processor has 16Kb of Learn more by visiting the website. RAM. The speed is clocked at 16KHz. These specifications are much less A Micro Bit is about the size of a credit than those of modern computers. By card. It measures 5 by 4 centime- comparison, a typical computer has ters. Don't let the size fool you. It is a RAM measured in Gigabytes. Typical powerful device. At the core is a micro- computers have 4, 8, or 16 gigabytes of controller. RAM. Instructions are processed in the Gigahertz range. A typical computer will I've covered microcontrollers in previous operate at 1.6, 2.1, or 2.7 gigahertz. issues. One issue covered the use of a microcontroller on the mBot to control The specifications of the ARM processor LED lights. Another covered the use remind me of my first computer. The of a virtual Arduino microcontroller specifications were almost the same. to control electronic components in It was much slower with an 8-bit TinkerCAD. processor. The state of the art at the time.

Page 4

Digital Maestro Magazine • Micro Bit: An introduction The goal of a microcontroller is not speed or storage. A microcontroller is designed to perform a set of repetitive tasks. Microcontrollers are found in almost any modern electrical device. There are microcontrollers in your watch, computer, thermostat, and car. Microcontrollers are becoming more common. They are part of devices that take advantage of IOT, Internet of Things. The Internet of Things is anything that is controllable through devices like a smartphone. An example of an IOT is a modern thermostat that is controlled by your smartphone. Devices like Alexa from Amazon control microcontrollers on devices like light bulbs. These devices interact with Alexa to receive instructions and respond to those instructions.

A closer look at the Micro Bit

The imprinted circuit board on the Micro Bit contains buttons to trigger events. It has an accelerometer and a compass. The accelerometer is used to detect

motion. The campus is used to determine the orientation of the Micro Bit. Bluetooth technology is included. Bluetooth is used to communicate with other Micro Bit devices. One side of the Micro Bit has a series of LEDs. These LEDs are arranged into rows and columns. The 25 LEDs are programmable for displaying information. The five by five grid is used to display scrolling information or simple designs. Programs are sent to the Micro Bit through a USB connector. The program executes each time the Micro Bit is activated or reset. New instructions must be uploaded if we want it to perform different actions. The Micro Bit is too small to have a battery. It does have a battery connector and USB option to supply power. The battery connector supplies 5 volts. The voltage provided by the USB is also 5 volts. The basic Micro Bit usually doesn’t include a USB cable. Kits come with a USB cable and other components. I recommend getting a kit whenever possible. The price of the Micro Bit is very affordable. A Micro Bit starts at about \$20 US dollars. It’s almost the price of some of those required school supply items. There are school sets that are priced around \$150 to \$200 for a set of 10. These kits contain several connectors and guides. Page 5

Micro Bit: An introduction • Digital Maestro Magazine Before purchasing you might want to see what the Micro Bit is all about. There is a Micro Bit simulator at microbit.org. The similar if a good way to evaluate what the Micro Bit does. Most of the functions available on the physical device are in the simulator. The simulator is a way to evaluate programs developed for the physical device. In this week’s issue, we use the simulator to develop a variety of projects. These projects are aimed at giving you a foundational use of the Micro Bit and coding environment. The coding environment uses the familiar block coding format.

Microbit simulator The Micro Bit simulator and coding environment are found at https://microbot. org. The simulator and coding environment are web-based. There is nothing to install. Click the [Let’s Code] option in the menu.

The Python editor focuses on using text to develop code. Python is one of the programming language used to develop projects on the Micro Bit. We will use the Block Editor. Click the [Let’s Code] button. A page with the coding environment will load. This page has all the things we need to develop projects. The left side of the page has a panel with the Micro Bit simulator. The center panel has the block categories. Code blocks are organized in these categories. The right panel is the coding canvas where the code is assembled. The bottom of the page has a bar with several tools. The download button is used to download the code we develop. The code is then uploaded to the Micro Bit. The save icon next to the project name field does the same thing. Next to the download button is the project name field. The button bar at the top of the page has several useful functions. The Projects button takes us to projects saved under our account.

The share button provides a way to share a project. There are several options for The coding start page has a couple of sharing a project. options. We have the option of using the JavaScript Blocks Editor or the Python Two buttons in the center are used to Editor. The Blocks editor is the familiar switch between the block editor and the editor used in applications like code.org. JavaScript text editor. Page 6

Digital Maestro Magazine â&#x20AC;˘ Micro Bit: An introduction On the right, we have a gear icon. This is where we set project settings. There are a couple of very useful options to help students here too.

A Microsoft account is required. Microsoft accounts are free. Microsoft accepts any email address for the free account. Email addresses from Gmail and Yahoo are valid. Students with existing email The project code needs to be down- accounts will use those accounts to loaded to the physical Micro Bit. The create their Microsoft account here. code is copied onto the physical device. The device appears as a storage device Districts with Microsoft Office 365 inteon your computer. gration will be able to use their district account to login. This is especially useful Makecode Microsoft account for students that are twelve or younger. We need to create or activate our account to save projects in the Cloud. Click the Microsoft logo located on the far right top corner of the page.

Donâ&#x20AC;&#x2122;t return to the previous tab in your browser after logging in. Click the Micro Bit tile on the login page. This will take you to the Micro Bit playground for your account.

Clicking the logo will take us to the Microsoft Makecode page. Click the Sign in button.

Page 7

Micro Bit: An introduction â&#x20AC;˘ Digital Maestro Magazine

The Basics

Click and drag the first code block over to the canvas. The panel will hide as soon as The coding canvas has two code blocks we click and drag a code block away. to get us started. There is a [on Start] and a [forever] block. Code placed within the [on start] block will be run when the Micro Bit is connected to a power source or the reset button is pressed. The code in this block is executed before the code in the [forever] block. The [forever] block will run the code repeatedly until the Micro Bit is disconnected from a power supply. Drag the code block over the [forever] block. The connector below the word forever will highlight in yellow. This is informing us that the block we are dragging into place will be accepted. Release the block. The [show number] code block uses the LEDs on the board to display a number from the code parameter. The paramWe will use the [forever] block for most eter in the code block is currently set to of these exercises. Click the Basic blocks zero(0). Replace the value with another category. number like 5 or 7.

A panel will open with code blocks for The LEDs on the Micro Bit will update to this category. Most of these blocks work display the number. Enter a number like with the LEDs on the Micro Bit. 34 or 76.

Page 8

Digital Maestro Magazine • Micro Bit: An introduction The LED matrix displays only one number at a time. The Micro Bit scrolls the information like a marquee. This happens when there is more information that can be displayed at one time. The numbers will scroll from right to left.

A 5 by 8 grid of preset icons will display. Move the mouse over one of the icons. An information bubble will appear next to the mouse arrow. This information bubble identifies the icon name. Click one of the icons. Click the [show number] code block and drag it to the code panel on the left. The The Micro Bit will update with the newly code panel will become translucent and selected icon. show a trashcan icon. Release the code block.

Drag the [show icon] code block back to the code category panel. Release when Click the Basic category and select the the trashcan icon appears. [show icon] code block. Place the code block within the [forever] loop. Click the Basic category and select the [show string] icon. Place it within the The LED display will light up in the shape [forever] loop. of a heart. The [show icon] code block has a selector. Click the triangle next to The argument within the forever loop the heart icon. reads “Hello!”. The word Hello! will scroll from right to left. Replace the word with “Hello World!. Page 9

Micro Bit: An introduction â&#x20AC;˘ Digital Maestro Magazine Click the same LED selector again. This will turn the LED off. Click the LED selectors to create your own icon. Try making something like an umbrella. How about an alien invader?

Remove the [show string] code block. Click the Basic category and place the [show leds] code block within the [forever] loop. This code block has a five by five matrix that represents the LEDs on the Micro Bit. None of the LEDs will light yet. We need to select which LEDs will be lighted.

Click once on the first LED selector. The selector color will change from pale blue to red. The LED on the simulator will light up in the same location.

Page 10

Digital Maestro Magazine â&#x20AC;˘ Micro Bit: An introduction

Start Vs. Forever

and dragging the first code block is one way to select all the attached code Programs in the Start block will be blocks. All the code blocks attached executed first on startup or restart. They to the first code block should remain will run only once. Programs developed attached. in the [forever] block will repeat until the device is turned off. Programs are usually more elaborate than the ones we have developed at this point. Programs string a series of code block together. Drag the [show leds] code block back onto the codes panel. Get a [show number] code block and place it within the [on start] code block. Place a [show icon] code block below the [show number] block. Place a [show string] block below the [show icon] block.

Place the string of blocks within the forever loop. The instructions are executed from beginning to end. The instructions repeat after the last instruction. This is the purpose of a loop.

Drag the first code block back to the [on start] block. The instructions will run from beginning to end and then stop. The instructions repeat once when the Micro Bit is connected to a power source. The instructions also start when the reset The program restarts each time we place button is pressed. There is a reset button a code block within the [on start] block. on the simulator. The blocks of code are executed one by one in the order they are placed. This is how most programs work. The [on start] code block runs through the instructions until the last one. Letâ&#x20AC;&#x2122;s compare this with the forever code block. Click and drag the first code block away from the [on start] block. Clicking Page 11

Micro Bit: An introduction • Digital Maestro Magazine Click the cycle button below the simu- Input doesn’t always require human lator. It is between the stop and snail interaction. Input is also received from buttons. other computers or devices. Input is received from the environment. The The reset button is used to restart the temperature in a room is input for a therMicro Bit and begin executing code from mostat. A notification on your smartthe beginning. This includes any code in phone is input from a computer. the forever loop. We don’t have to wait for the lines of code to finish before The Micro Bit takes input from buttons pressing the reset button. Click the reset and sensors. Buttons are what we press button during the execution of code. to get the program to respond. Sensors It doesn’t damage the program or the are electronic components that respond Micro Bit. to our interaction or the environment.

Input and Output

The Micro Bit has a compass. This is a sensor used to determine north. It has All computers and microcontrollers react a motion sensor. This motion sensor to input. Most devices with microcon- detects when the Micro Bit is moved. trollers require input. They react to input with output. Okay, these might seem like The Micro Bit has another input and strange terms. Let’s look at an example. output option. This is the Bluetooth Taping the screen of your smartphone component. Bluetooth is used to send is input. Tapping your favorite game and receive instructions. Sending signals is input. The game that starts on your is output from one Micro Bit and input device is output. The output is a reac- for another. Receiving a signal is input. tion to your input. Input comes in many forms. Entering your username and Button Input password is input. Filling out a form on a website is input. There are two buttons on the Micro Bit. We will program each button to perform Input and output are constantly going an action. The simulator has buttons A on when we interact with computers. and B. Tapping your favorite game is just one input in a series of inputs. Each time you Click the Input category in the Code block tap, drag, or pinch something within the panel. Place the [on button A pressed] game, that is input. The game reacting to block onto the canvas. This code block these gestures is output. does not fit within the [on start] or [forever] loop.

Page 12

Digital Maestro Magazine â&#x20AC;˘ Micro Bit: An introduction

Go to the Basic category and select the [show icon] block. Place the block within the [A] button input block. Select an icon you would like to display. I chose the smiley face.

The [A] button parameter is a selector. Click the triangle next to the letter A and select the letter B. The code block will change back to its normal color.

Place a [show icon] block in the [B] Click the [A] button on the simulator. The button block. Select a different icon for smiley face will appear after pressing this button. I chose a sad face. and releasing the button. The icon will remain on the board until it is reset. Click Click the letter [A] in the simulator to see the reset button and click the [A] button one icon. Click the letter [B] to see the again. other icon. Go back to the Input category and place another [on button A pressed] block onto the canvas. Place it next to the existing button block.

The icon on the board changes each time one of the buttons is pressed. Pressing one of the buttons is input. The icon displayed is output.

The code block is colored a little differently. This is because the code block is duplicated on the canvas.

Page 13

Micro Bit: An introduction • Digital Maestro Magazine

Clearing the display The icon from the last button we pressed will remain on the display until another button is pressed. We will add code to clear the display after a couple of seconds. Go to the Basic category and place a [pause (ms)] code block below the [show icon] block in the [A] button block.

Click the A or B button. The icon will display for a moment and then disappear.

The pause The pause code block waits 100th of a second before clearing the screen. Let’s see what happens when we don’t use a pause block. We will remove the pause block from the [A] button.

Repeat the process for the [B] button Code blocks need to be disassembled block. from the bottom up. Click and drag away the [clear screen] code block. Don't trash the block. Place it somewhere on the canvas.

Click the Basic category again. There are more code blocks in this category. Click More to display them. Detach the [pause] code block. Don’t Place the [clear screen] block below the trash the block. Place the [clear screen] [pause (ms)] block of button [A]. Repeat block back. Press the [A] button. the process for button [B]. The smiley face will display for about a second and then clear. We don’t need to attach a pause block but it is a good idea to do so when we want to pause an event. The pause block is useful when we need to increase the time between events. We can increase the display time from one-second to two or more.

Page 14

Digital Maestro Magazine â&#x20AC;˘ Micro Bit: An introduction Click in the project title field. Title the project Button Input. The project is automatically saved in the Cloud. The save button next to the project name is used to download the project. The project is saved in the Projects folder with the updated project name.

Time in milliseconds

Sensor Input The Micro Bit has a motion sensor. We will use this sensor for input. The sensor detects when the Micro Bit is shaken or tilted. Create a new project. Click the Projects button.

Computers have a built-in clock. This clock is necessary to keep everything in sync. Everything is measured in seconds. The processor speed is measured in Gigahertz per second. That is a thousand millions cycles per second. Computers process instructions much faster than one-second. Processes are measured in milliseconds. That is thousandths Click the New Project button. of a second. A second is divided into one-thousand segments. The 100 in the pause argument is one-hundredth of a second. That is very fast. To increase the pause time we increase the value. A value of 500 ms, milliseconds, is half a second. A value of 1,000 ms is one full second. Go to the input category. Add [on shake] Increase the values in the pause param- block onto the canvas. eter. Change the value for one button only. This makes it easier to compare the changes.

Page 15

Micro Bit: An introduction • Digital Maestro Magazine The simulator will update with an extra to the Micro Bit and the other end to a button. This button will simulate the computer. This cable is also used to send shaking of the Micro Bit. programs to the Micro Bit.

Go to the music category and place the [play tone…] block inside the [on shake] block. This is the first code block in the list.

The device doesn’t always need to be connected to a computer for power. It has an additional connection to draw voltage from a battery pack. The Micro Bit can then be used anywhere once a program is installed.

The Micro Bit takes voltage to operate. It also provides voltage to other devices or components. These devices receive power through the connectors. The voltage supplied is usually 3 volts. Pins 0,1,2, and 3 all supply this voltage. The The simulator will update with some last pin is the ground pin. extra items. A physical Micro Bit does not come with a speaker. External speakers Her is a basic review of electric current. need to be connected to the pins on the Electrical current is produced by elecdevice. There are two simulated wires trons moving across a conductor. The connected to the connectors. The first electrons in the Micro Bit flow through wire is connected to the connector the connectors, through a component labeled 0. The other wire is connected to like a speaker, and to the ground. a connector labeled GND. The wires in our simulator show a The Microbic receives electric current connection to another wire. This wire is from a connection. This connection is connected to a speaker. That part is not through a USB, Universal Serial Bus, shown in the simulator. cable. One end of the cable is connected Page 16

Digital Maestro Magazine • Micro Bit: An introduction Turn up the volume on your computer Melodies and click the [Shake] button on the simulator. You will hear a tone that lasts for I don’t know how to read or play music. one beat. This is why I find the melody block very useful. Remove the [play tone…] block The code block has two parameters. and add the [start melody…] block. They are used to create a variety of tones. Click the [Middle C] code block. A Click the melody selector. Select the piano keyboard will appear. Click one of entertainer melody or anything you like. the keys on the keyboard to select and play a tone. The keyboard will remain visible. Click on different keys to find a tone you like.

Click the shake button and listen to the melody. Name the project \[Shake to the music]. The beat option determines how long the tone is played. Click the selector arrow for the beat. There are fractional beats that range from one-half to one-sixteenth. The whole notes include 1, 2, and 4. Play with the different tones and beats. Add more play tone code blocks and create some music to play.

Page 17

Micro Bit: An introduction â&#x20AC;˘ Digital Maestro Magazine

Motion Sensor

Click the LED buttons to create an arrow pointing to the left.

The shake program we developed uses the motion sensor. We will use the same motion sensor to react to something more subtle. We will detect the tilt of the Micro Bit. The sensor is oriented to help determine the direction of a tilt. Click the Projects button in the menu and create a new project. Make sure to title your previous project before creating a The simulator helps by simulating a tilt. new project. Move the mouse pointer over the Micro Bit simulator. Move the arrow to the left Place a \[on Shake] bock onto the canvas. side. It will tilt in the direction where the Change the sensor option from shake to pointer is moved. LEDs will light up to [on tilt left]. display the arrow we created.

Go to the Basic section and place [show Go to the Input category and place leds] within the tilt block. another [on tilt] block on the canvas. The block will read [on shake]. Change the parameter to [on tilt right]. Add a [show LEDs] block. Create an arrow pointing to the right. Test the program by tilting the simulator left and right. Letâ&#x20AC;&#x2122;s add more directions. We'll add up and down. These options are labeled Page 18

Digital Maestro Magazine • Micro Bit: An introduction differently. Add a [on shake] block to times a series of steps is executed. This the canvas and change the parameter to is called an iteration. Loops have two [logo up]. or more iterations but are not typically forever. In these lessons, we will learn how to use logic to create conditions. These conditions will determine what the Micro Bit will do next. Conditions are tied to input. Create a new project. Make sure to title Place a [show leds] block within the [logo your previous project. Go to the Logic up] block. Create an arrow pointing up. category. Place a [if…then…] block within the forever loop. Move the mouse pointer over the simulator. Move the arrow to the bottom of the Micro Bit. This simulates tilting the Micro Bit up.

The [if...then…] block is used to determine if something is true or false. This is tied into how computers evaluate everything. Computers use binary information. I think you can do the last one on your Binary is the use of ones and zeros. One own. Create an instruction to point down. represents something that is on or true. Title the project \[Tilt and point]. Zero represents something that is off or false.

Logic and Loops

At the core of most programs lies logic and loops. The forever block is a loop. It repeats the instructions for as long as the Micro Bit is on. There are other loops. Loops that are limited in the number of

Go to the Input category and find the [button A is pressed] block. This is different from the [on button A pressed] block. The block fits in the space provided for the true or false condition. Place the [button A is pressed] block over Page 19

Micro Bit: An introduction • Digital Maestro Magazine the [true] block and release. The block will be used as the condition. If the [A] button is pressed then something will happen next.

Go to the Logic category and place a [if… then…else] block within the forever loop. The [else] part of this block helps identify what happens if the condition is true or false. Pressing the [A] button is a true statement. Not pressing the button is false. Think of pressing the button as On and not pressing the button as Off.

What happens next is placed in the [then] position. Go to the Basic category and place a [show icon] block into the then section. Change the icon to a smiley face. Go to the Input category. Get the [button A is pressed] block and attach it to the [If] parameter. Go to the Basic category. Place a [show icon] in the [then] portion of the statement. Place another [show icon] block in the [else] portion. Select a smiley for the first icon and a sad face for the other Press the [A] button in the simulator. A icon. smiley icon will appear. This isn’t very different from the [on A button pressed] code we used earlier. What happens when the button is no longer being pressed? What should the Micro Bit do? This is where a condition is valuable. Remove the [if…then…] code blocks we The simulator will show a sad face. This just created. Drag them to the code panel goes with the logic of our condition. and into the trash. The button is not pressed so the icon Page 20

Digital Maestro Magazine • Micro Bit: An introduction displayed is sad. Press the A button. The LEDs light up to show a happy face. This lasts as long as the button is pressed. Releasing the button returns the sad face.

Multiple conditions

The [else…if…then] portion looks similar to the first part of the code block. Click the gear icon to close the bubble. Go to the Input category. Attach a [button A is pressed] block to the second [if] statement. Change the button parameter to B.

In the previous lesson, we used one button to display either a smiley or sad face. In this lesson, we will include the second button to give us variety. Use the code from the previous lesson. To add multiple conditions we need to add another if statement. Click the gear icon next to the [if] statement. Move the sad [show icon] block into the [then] statement. Go to the Basic category. Select a [show icon] block and attach it to the last statement. Leave the icon set to the heart.

Drag an [else if] block between the current [if…else] blocks.

Press the A button on the simulator for a smiley. Press the B button for a sad face. Release both buttons for a heart. Title the project \[Button condition].

Page 21

Micro Bit: An introduction â&#x20AC;˘ Digital Maestro Magazine

A Math Lesson Randomness and Probability

Place the [set item to 0] block within the [on button A pressed] block.

We are going to use the skills learned in the lessons to build a project. In this project, we will program a coin toss and a dice game. Use these games to teach randomness and probability.

Coin Toss

Click the triangle next to the item name The coin toss script will be assigned to and select [toss]. the [A] button. Go to the Input category and place a [on button A pressed] block onto the canvas. We will use a variable for the first time in these lessons. Variables are used to store information. This information is used to evaluate a condition in the program. Go to the Variables category. Click the We use a math function to randomly [make a variable] button. select a number for our coin toss. Go to the Math category. Place the [pick random 0 to 4] block in place of the [0] parameter for the variable value.

Name the variable [toss] and click the OK button. A toss variable will be created. Other blocks are also available in the variable Change the value of 4 to 1. This random category. These blocks are used with the number selector will select either zero variable. or one. Page 22

Digital Maestro Magazine â&#x20AC;˘ Micro Bit: An introduction A zero or one will be selected at random Go to the Logic category. Place a [0 = 0] each time we press the [A] button. This is block in the True parameter. the coin toss part.

Go to the Variables category and place We need to know the result of the coin the [toss] variable into the left side of the toss. That is the next part of the program. equal sign. Place an [ifâ&#x20AC;Śthenâ&#x20AC;Śelse] condition after the toss variable. Hint, you will find it in the Logic category.

If the result of the random selection is zero we want to display a sad face. Go to the Basic category and place a [show icon] block for the [then] parameter. We need to evaluate the result of the Change the icon to a sad face. There is coin toss. To evaluate the result we need only one other result of the coin toss. to know if the random number is either Place a smiley icon in the Else parameter. zero or one. We need to compare the result. To compare a result we need to know if it is equal to one or zero. This equal parameter is different from the math equal. In this context, we are doing a comparison. Is the random number generated the same as zero? Page 23

Micro Bit: An introduction â&#x20AC;˘ Digital Maestro Magazine

Reset the display A coin does not have enough values to see changes from one toss to the next. A clear option helps to see the individual tosses. Go to the Input category and place a [on shake] block onto the canvas. Go to the basic category and place a [clear screen] block into the [on shake] Place the [set item to 0] block in the [on block. The [clear screen] block is in the button B pressed] block. Change the item more section of the category. variable to the roll variable. Go to the Math category and look for the [pick random 0 to 4] block. Place this block on the right side of the variable. A dice has six sides. Change the number to 5. Remember that zero is one of the numbers selected in the random number selector. Press button A to toss the coin. Press the Go to the Logic category and place a [ifâ&#x20AC;Ś shake button to clear the coin toss for the then] condition block after the set varinext. able block. Name the project \[coin toss and dice]. Go to the Logic category and place an [0 Keep the project open for the next part. = 0] block to the right of the [if] parameter.

Dice Game

Place the roll variable from the Variables We will program the throw of one dice. category in the left side of the equal sign. Go to the Input category and place a [on button A pressed] block onto the canvas. If the dice value is zero then we want Change the parameter from A to B. to display the number 1. Place a [show number 0] block from the Basic category We need to create another variable to into the [then] parameter. Change the store the value of our dice roll. Go to the value from 0 to 1. Variables category and create a variable. Name the variable [roll]. Page 24

Digital Maestro Magazine â&#x20AC;˘ Micro Bit: An introduction Click the gear icon next to the if param- Duplicate the [show number 0] block and eter. Place an [else if] block below the place it in the [then] parameter. Change [if] block. Click the gear icon to close the the value to 2. bubble.

Click the gear icon and place another Instead of recreating some basic blocks [else if] block after the last [else if] block. we will duplicate what we have. Rightclick the [roll = 0] block. Select the dupli- Repeat these steps until you reach the cate option from the contextual menu. [show number 6] block. The last part of the condition statement has one more \ [else] parameter. Place a \[show string] clock. Show the text \[Roll the dice!].

Place the copy in the [else if] parameter. Change the value from 0 to 1. Press the [A] button to play the coin toss game. Shake the Micro Bit to reset the display. The string at the end of the condition statement will never show because the random number selector will never not select a number between 0 and 5.

Page 25

Micro Bit: An introduction • Digital Maestro Magazine Now you have a tool to teach students about random numbers and probability. This tool is far more valuable than one created for us. When students create their own instruments they have a deeper understanding of what they are learning.

Efficient Dice Roll All the if…then…else condition statements are tedious. There is a more effective way to code the dice game. So, why did you just code all those condition statements? So you can learn to use conditional statements.

Place a [roll] variable on the left side of the addition symbol. Change the 0 to a 1 on the right side of the addition. This will take the value of the dice roll and add one to the value. For example, a roll of 0 will become a value of 1. Place a [show number…] block below the [set value…] block. Place the [value] variable in place of the number. Were done. Press the [B] button a few times.

Drag the first if…then…else blocks out of the [on button B pressed] block. We need to create another variable to display the result. Go to the Variable category and create a variable. Name the variable [value]. Yes, this is more practice in creating variables. I like using this example. It shows the value of using math to solve probPlace a [set item to…] bock below the lems. A simple math algorithm replaced [set roll to…] block. Change the variable several lines of if…then…else. I use this to name to value. demonstrate that there is more than one way to solve a problem. Go to the Math category and place an addition block to the right of the value Animated toss and roll variable. The coin toss and dice roll don’t seem very exciting. In this lesson, we will add some code to spice up the coin flip and dice roll.

Page 26

Digital Maestro Magazine â&#x20AC;˘ Micro Bit: An introduction Go to the Loops category. Select the more numbers or icons. [repeat 4 times] loop and place it at the beginning of the code blocks for the coin Brighten things up flip. It is a little difficult to know which is the final icon or number. To help, we will decrease the LED brightness for the tosses and rolls. We will increase the brightness of the selected icon or number.

Place the other code blocks within the loop. The set and if condition will be placed within the loop. To move all the blocks, grab the first code block. The rest will follow.

Go the LED category and click the more section. Place the [set brightness 255] bock before the loop in each game. Change the brightness value from 255 to 100. This will dime the LEDs by a little more than half.

Duplicate the [set brightnessâ&#x20AC;Ś] block or Repeat the process for the dice game. get another block. Place the block after Make sure to place the set values and the loop in each game. Change the value show blocks within the loop. to 255 if you duplicated the block. v Press the A or B button. Sad and smiley faces will cycle four times. The last icon will be the result of the coin toss. Numbers will cycle in the dice game four times. The numbers might be different or they might repeat. The last number will be the result of the dice roll. Increase the number of repetitions if you want to see Page 27

The goal of education is not to increase the amount of knowledge but to create the possibilities for a child to invent and discover, to create men who are capable of doing new things. Jean Piaget

Micro Bit An Introduction

In this issue, I dust off my Micro Bit and take it for a spin. You will learn how to use the online simulator to develop basic projects. The...

Micro Bit An Introduction

In this issue, I dust off my Micro Bit and take it for a spin. You will learn how to use the online simulator to develop basic projects. The...