Key Stage 3. Introduction to the. BBC micro:bit

Similar documents
Controlling the Robotic Arm using the BBC micro:bit

BBC micro:bit Cool Colours!

09/05/2014. Engaging electronics for the new D&T curriculum. Geoff Hampson Managing Director of Kitronik. Presentation overview

Grove Inventor Kit for micro:bit Project Guide

Piano for Micro:bit User Manual

NAME EET 2259 Lab 3 The Boolean Data Type

Programmable Control. Name Class Teacher. Ellon Academy Technical Faculty

OrbBasic Lesson 1 Goto and Variables: Student Guide

INTRODUCTION HARDWARE

BBC Micro:Bit. Year 7. Teacher Guide and Planning

OrbBasic 1: Student Guide

the NXT-G programming environment

Taskbar: Working with Several Windows at Once

Micro:bit - an Educational & Creative Tool for Kids

i wonder, therefore i am. Welcome to the Wonder League! This packet includes everything you need to know about Dash, Dot, and how to get started!

Discover Robotics & Programming CURRICULUM SAMPLE

Unit 2. Computer Control. PIC stands for PROGRAMMABLE INTERFACE CONTROLLER. A PIC chip takes in input signals and then controls output transducers

ipod classic User Guide

Don t Steal My BBC micro:bit Alarm!

Train the Trainer Resources Hex Files Here -

AN INTRODUCTION TO SCRATCH (2) PROGRAMMING

OrbBasic LESSON 1 Goto and Variables Student Guide

Introduction to microcontrollers

Getting Started Guide

Basic Computer and Mouse Skills Windows 10

FILE ORGANIZATION. GETTING STARTED PAGE 02 Prerequisites What You Will Learn

Theo. Miles. Olivia. Joseph. Nina. Kishan. The Richard Pate School, Cheltenham

Organist Palette Guide

Digital Devices in the Digital Technologies curriculum (F-10) Steve Allen VCAA Digital Coding Specialist Teacher

micro:bit Lesson 1. Using the Built-in Sensors

S3 Scratch Programming

Wireless Key fob, Key pad & Receiver Range

LME Software Block Quick Reference 1. Common Palette

COMP : Practical 9 ActionScript: Text and Input

Hello App Inventor! Android programming for kids and the rest of us. Chapter 2. by Paula Beer and Carl Simmons. Copyright 2015 Manning Publications

Section 1. System Technologies and Implications. Modules. Introduction to computers. File management. ICT in perspective. Extended software concepts

Creating Breakout - Part 2

What Is a Program? Pre-Quiz

Using the BBC micro:bit within D&T

Review and Evaluation with ScreenCorder 4

Lesson 2. Introducing Apps. In this lesson, you ll unlock the true power of your computer by learning to use apps!

DIGITAL GAME CAMERA. Model DC-6SS

mi:node User Manual Element14 element14.com/minode 1 User Manual V3.1

Computing & Control Trainer Teaching Guide

Get comfortable using computers

Version 1.1. COPYRIGHT 1999 Tufts University and Vernier Software. ISBN (Windows) ISBN (Macintosh)

University of Hull Department of Computer Science C4DI Interfacing with Arduinos

Circuit Playground Express: Piano in the Key of Lime

Inspiring Creative Fun Ysbrydoledig Creadigol Hwyl. Microbots Workbook

Procedures: Algorithms and Abstraction

Studuino Block Programming Environment Guide

Functional Skills. Entry 3 to Level 2. IT Basics Information

BBC micro:bit Emoji Bag

Chapter 5 Devices. About devices. Planning your devices

Excel Basics: Working with Spreadsheets

Chapter 4 Devices. About devices. Planning your devices

Want to add cool effects like rollovers and pop-up windows?

Inspiring Creative Fun Ysbrydoledig Creadigol Hwyl. App Inventor Workbook

HARMONi G3. Quick Start Guide for HARMONi G3. imac Processor/FireWire Upgrade

DELUXE STEREO AMPLIFIER KIT

Part 1: Understanding Windows XP Basics

Tips and Tricks BASICS. Slide to the three different panels of your Home screen. Check out notifications to stay informed

Earthwork 3D for Dummies Doing a digitized dirt takeoff calculation the swift and easy way

Introducing Cubelets. What do they do?

Meet the Cast. The Cosmic Defenders: Gobo, Fabu, and Pele The Cosmic Defenders are transdimensional

AppyBuilder Beginner Tutorials

Unit 9 Tech savvy? Tech support. 1 I have no idea why... Lesson A. A Unscramble the questions. Do you know which battery I should buy?

3.6 MONITORING AND CONTROL SYSTEMS

All Blocks of Scratch

Using Tab Stops in Microsoft Word

ipod nano User Guide

DAB/DAB+ Radio User Manual

Using the Olympus WS-110 Digi-Recorder. Part A - Setting it Up. Introduction. A1. Inserting the Battery. A2. Turning On and Off

ipod Tutorial Includes lessons on transferring music to ipod, playing music, and storing files on ipod

Note. The above image and many others are courtesy of - this is a wonderful resource for designing circuits.

S A M P L E C H A P T E R

Basic Concepts 1. Starting Powerpoint 2000 (Windows) For the Basics workshop, select Template. For this workshop, select Artsy

Lutheran High North Technology The Finder

MultiJAMMA Switchboard Installation and User s Manual

Basic ipad Use. Your ipad:

My First iphone App (for Xcode version 6.4)

LeakDAS Version 4 The Complete Guide

Experimental Procedure

The GENIE Light Kit is ideal for introducing simple lighting projects, such as an electronic die, a wearable badge or a night-time warning system.

Embrace YOUR Race 1 3E device

Micro USB Lamp Kit ESSENTIAL INFORMATION. Version 2.0 DESIGN A STYLISH LAMP WITH THIS

Shack Clock kit. U3S Rev 2 PCB 1. Introduction

7 For Seniors For Dummies

Please click double click on the icon labeled Internet Explorer, the picture is a blue letter E.

Programmable timer PICAXE programming editor guide Page 1 of 13

Instruction Manual for BE-SP3 Circuit. 10/21/07

Interface. 2. Interface Adobe InDesign CS2 H O T

Software Setup Instructions for the Foster Control System used in the Explora Dome Observatories

Robolab. Table of Contents. St. Mary s School, Panama. Robotics. Ch. 5: Robolab, by: Ernesto E. Angulo J.

seminar learning system Seminar Author and Learning System are products of Information Transfer LLP.

EE261 Computer Project 1: Using Mentor Graphics for Digital Simulation

Installing and Using Trackside Cameras Revised November 2008

Click Install View Touch. Installation starts. Click Next. Click Finish.

A Charging LED B Touchscreen C On/Off button D Speaker E External antenna connector F USB connector G RDS-TMC connector H Memory card slot

Interactive Powerpoint. Jessica Stenzel Hunter Singleton

Transcription:

Key Stage 3 Introduction to the BBC micro:bit

KS3 Introduction In 2016, every Year 8 pupil in the UK received a free micro:bit. The BBC micro:bit is a small, programmable computer. This project aims to develop a new generation of tech pioneers by inspiring young people and their teachers to explore digital creativity and coding. The micro:bit measures only 50 mm by 40 mm, but it s a powerful device. It has many cool features, including: + + twenty five individually programmable LEDs that form the micro:bit s screen, which you can use to display messages or images; + + two programmable buttons, A and B, that the micro:bit responds to, which allow you to control it; + + light and temperature sensors that allow you to write code that responds to light and temperature changes, for example you could use a micro:bit to control the climate in a greenhouse; + + an accelerometer that can detect if the micro:bit is moving, shaking, tilted or falling; + + a compass that can detect which direction the micro:bit is facing; + + a radio that allows you to communicate between micro:bits wirelessly; and + + bluetooth connectivity, so that you can connect your micro:bit to your phone and control it. Writing code for your micro:bit Before you can use the micro:bit, you have to write programs to control it. You can use the MakeCode Block Editor, JavaScript or Python to write code to program your micro:bit. In this booklet, you will use the Microsoft MakeCode Block Editor to design all your code. It is simple to understand, but will allow you to create complex projects using all the features of the micro:bit. If you are already familiar with Scratch, you will find using the MakeCode Block Editor to construct micro:bit code easy. If you use Python in your school, you can easily convert your code. There are plenty of websites comparing MakeCode blocks with their Python equivalents. If you are a keen programmer, you can even progress to writing your code in C and C++ using the online Arm Mbed programming environment. Arm is the company that makes the processor chip that controls the micro:bit. You can even create your own blocks and add them to the MakeCode editor. 1

Introduction to the BBC micro:bit Using the MakeCode Block Editor You can find the online code editors for the micro:bit at microbit.org To get started, click on Let s Code in the menu at the top of the screen. For the tasks in this booklet, use the JavaScript Blocks Editor, which is the first option. Click on the orange Let s Code button. An editing screen will open up. 1 2 3 The editing screen has three sections: 1. The simulator This allows you to test your code without downloading it onto a real micro:bit, so that you can quickly find faults and fix them. The buttons on the simulator work by clicking on them. The simulator can also simulate sensors such as temperature, light and the compass. It will also display output on the LEDs. 2. The blocks window The blocks window allows you to access all the different blocks you can use in your code. The blocks are grouped by type: + + basic controls basic micro:bit functionality; + + input responds to inputs and events from sensors; + + music for outputting sounds; + + LED controls the LED screen; and + + radio controls communication between micro:bits. 2

KS3 3. The editing window To create code for the micro:bit, drag blocks from the blocks window and connect them together to form the set of instructions you want the micro:bit to carry out. The projects in this booklet show you how to create code to use many of the features of the micro:bit. Downloading code to your micro:bit Once you have written your code in the Block Editor and tested it using the on-screen simulator, you can download it to your micro:bit. The designers of the micro:bit have made this process really simple by making the micro:bit behave like a USB flash drive when connected to a computer. The steps below explain how to do this if you use a PC. 1. Click on the large purple button on the bottom-left of the MakeCode window. 2. Click the Save button in the pop-up window at the bottom of the screen. 3. Now click on the Open folder button in the new pop-up window at the bottom of the screen. 4. Windows Explorer will open and show the contents of the Downloads folder. You will see your newly-created micro:bit code file in the right-hand column. It will have a name in the form microbit-untitled (1).hex. You will also see your micro:bit listed as a drive in the left-hand column. Drag your code file on to this MICROBIT drive. An LED will start to flash on the micro:bit. When it stops flashing, your code will be loaded and will start to run. You can now start to use your newly programmed micro:bit. If you use a Mac, the process is almost identical. Code files are still downloaded to your Downloads folder. You can copy these to the micro:bit in the same way you copy files to a USB flash drive on your Mac. If you are using a tablet or phone app to write your code, the micro:bit will be connected via Bluetooth. Follow the instructions in the app to download your code. 3

Introduction to the BBC micro:bit Programming concepts and the micro:bit Solving problems involves writing a set of step-by-step instructions called an algorithm. Three basic building blocks make up an algorithm: + + sequence; + + selection; and + + iteration. Sequence You can connect individual instructions together to make a sequence of step-by-step instructions. For example: Selection Selection is made using IF statements. A basic IF statement in the Block Editor looks like this: A Boolean expression is a logical statement that is either TRUE or FALSE. In the image above, the block item=1 is a Boolean expression. You can expand the IF statement to provide a much wider range of choices. The little white gear icon opens an editor window that allows you to add a range of else if and else statements. For example: 4

KS3 Iteration Iteration allows you to simplify your algorithms by using loops that repeat sequences of instructions a set number of times or until something happens. The micro:bit has different types of loops, including REPEAT, WHILE and FOR. These give you more control over your code. The diagram below shows how you could use these loops. Basic Input and Output Input There are three sets of input sources on the micro:bit: + + user inputs; + + internal sensors; and + + external pins. User inputs These allow you to control your micro:bit by: + + using either the A or B Buttons, or both together; + + tilting it left or right; + + shaking it; + + placing it screen-down or screen-up; and + + dropping it (carefully) as it can detect being in free fall. 5

Introduction to the BBC micro:bit Internal sensors The micro:bit has built-in sensors that it can use to measure values in its environment. Sensors are available for: + + acceleration; + + light level; + + compass heading; + + temperature; and + + radio. External pins You can use the gold pins along the bottom edge of your micro:bit to connect to a range of devices as inputs, including: + + home-made connectors such as metal strips for moisture detection; + + variable resistors; + + simple keyboards; or + + sensors that measure, for example pressure, sound or movement. Output The LED display The main output is the 5 5 LED display on the front of the micro:bit. You can use this to display numbers, text or small images and to draw graphs. To display larger images and longer strings of text or numbers, the display can scroll from right to left. The pins The pins on the bottom edge of the micro:bit are useful outputs. To extend the micro:bit s capabilities, you can connect them to external devices such as: + + LEDs; + + buzzers or sounders; + + relays; + + servos; or + + motor drivers (the micro:bit can t drive a motor directly). Some of the projects in this booklet use these pins. 6

KS3 Using Input and Output Writing to the display The micro:bit can display numbers, text and basic images. The code below shows how each of these works. You can use the more advanced images block to display larger images. The code below creates a large image and scrolls it continuously from right to left. You can also use the display to plot a value as a graph. The code below shows how to plot the input from the light sensor as a graph. The size of the up to value depends on the sensor. For light, the scale is from 0 255. For the acceleration sensor, the scale is from 0 1023. 7

Introduction to the BBC micro:bit Writing to or reading from the pins The pins along the bottom edge of the micro:bit are two different sizes. The larger pins, P0, P1 and P2, are big enough to connect to small crocodile clips. GND (ground) and a 3V power connector are also available on large pins. This is enough for most projects. If you want to use the smaller pins, you will need to solder wires to them or buy one of the many expansion boards available. There are two types of data that you can write to or read from the pins on the micro:bit: analogue and digital. Analogue data Analogue data can vary in amplitude continuously. However, no electronic device could read the infinite number of values possible. Instead, a set number of bits is allocated to measure the value. This is the bit depth. The greater the bit depth, the greater the accuracy of the reading. The micro:bit uses a bit depth of 10, so it can read and write data to an accuracy of 1024 different values (0 1023). For example: A temperature sensor connected to Pin 1 is set so that the minimum value is output at 0 C and the maximum value is output at 100 C. This means the micro:bit can store 1024 different values between 0 and 100. The lowest temperature difference that it can measure is therefore 100/1024, which is approximately 0.1 C. Digital data Digital data are a series of pulses that are either 1 (ON) or 0 (OFF). If you want to write a value to a pin, use a Write block. If you want to read a value from a pin, use a Read block. The pins block set has a range of different blocks that allow you to create code to use on the pins. The table below shows each of the main Read/Write blocks and how they work. Read/Write Block What it does Writes a digital value 0 (OFF) to Pin 0 Writes an analogue value 1023 to Pin 0 Reads the digital value (0 or 1) from Pin 0 and stores this in the variable value1. Reads the analogue value from Pin 0 and stores this in the variable value1. 8

KS3 Task 1: Experiments with input and output Learning intentions In this series of tasks, you will learn how to: + + use different types of input; + + get data from the micro:bit sensors; + + display data in different ways; and + + output data to the micro:bit pins. We are going to experiment with some of the micro:bit sensors, the display and the pins. This will help you to become more familiar with how everything works. Each task will show you how to use one sensor. You will then repeat these tasks using different sensors. You can run all the code created in these tasks using the on-screen simulator. You can also download the code to your micro:bit. However, you will not be able to see any outputs to the micro:bit pins. Experiment 1 Reading and displaying data from the light sensor The light sensor on the micro:bit outputs the light level as a number in the range 0 255. We will display this on the LED display. The code Enter the following code in the Block Editor. Run the code in the on-screen simulator. How does the display show numbers that are too large to fit on-screen? Now amend the code above to read data from the temperature sensor. Again, you can adjust the temperature in the simulator. Can you see how this works just the same as using the light sensor? Now change your code to show data from the compass. It is called the compass heading. The micro:bit logo in the on-screen simulator changes to include a little pointer representing a compass. You can spin the logo to point to any heading. Straight up is 0 degrees. Finally, try the data from the accelerometer. This time, you have to choose the direction. On the simulator, left and right is X and up and down is Y. Can you work out what Z is? Move the cursor rapidly over the on-screen micro:bit to simulate acceleration. 9

Introduction to the BBC micro:bit Experiment 2 Showing data as a graph Sometimes it s not appropriate to display data as a number. What is a light level of 128? Often it is better to display this type of data as a graph. This allows us to see how the brightness compares with complete darkness and full light. The code Enter the following code in the Block Editor. Now, slide the light level up and down and observe what happens. Simple, isn t it? Do you find it easier to judge how bright it is? Which of the other sensors have data that would work better as a graph? Try the accelerometer. This time, plot the graph up to 1023. You can also show the simulator data on-screen. Move the cursor rapidly from left to right over the micro:bit. How is this better than displaying the data as numbers? Experiment 3 Writing digital data to the pins When we write digital data to the pins, we are sending one of two values: 0 or 1. We are switching the output off or on. We can adjust how rapidly we switch the pin on or off, but it s always a 0 or a 1. The code Enter the following code in the Block Editor. Press Button A and see what happens. Now amend your code to write a 1 to Pin 1 when Button B is pressed, and a 1 to Pin 2 when Buttons A and B are pressed. You can see how the data is displayed on the simulator. If the output is a 1, then the pin changes from gold to red. The problem is the pins always stay on. Add a final input to your code for a shake and use this to set all three pins to 0. There are many other options for the pins, but most are beyond the scope of this booklet. However, you will output data to pins in some of these projects. 10

KS3 Task 2: Making a simple step counter Follow the instructions below to make a simple step counter. Use the on-screen simulator. + + Go to the microbit.org website. + + Click on Let s Code to get started. + + Create the code below in the Block Editor screen. + + Item is a variable, but it should be called steps. Click on the little arrow to display the drop-down menu, then select rename to change the variable to steps. + + To count the number of steps, add the code below to your screen. + + You can use Button A to show how many steps you have taken. Add the code below to your screen. + + Press the SHAKE button ten times. Now press Button A and see how many steps the display shows. + + You need to be able to reset the counter. Add the code below to your screen. Extension You can t adjust the sensitivity of the step counter. It may pick up simple movements or may not record anything less than stamping your feet. Experiment with the accelerometer to see if you can calibrate the step counter for normal walking. 11

Introduction to the BBC micro:bit Task 3: Rock, Paper, Scissors Learning intentions In this task, you will learn how to: + + use the Shake input; + + create and change the value of a variable; + + create a random number; and + + make a selection in your code. Introduction Rock, paper, scissors is a well-known game. In this task, you will use the micro:bit to simulate the game, using images for each of the hand gestures: rock, paper and scissors. Use the shake input to trigger the micro:bit to generate a random number. Then use an IF statement to select which image to display. Variables You need a variable to store the random number. Variables should have descriptive names, so you should call it RockPaperScissors. It s long, but it s meaningful. To make a variable, click on the Variables block set and click on Make a Variable. Then type in your new variable name and click OK. The code Now that you have created somewhere to store the random number, you can enter the code on the right. 12

KS3 The IF statement The standard IF statement from the Logic block set does not have enough choices for your game. You need to extend it. The diagram below on the left shows the basic IF statement. Click on the little gear symbol beside if to open the edit window. Drag the correct blocks from the left to make it into the IF statement below right. Best of three? Players of rock paper scissors often determine the winner as the best out of three. Amend your game so that pressing Button A adds 1 to your score and pressing Button B adds one to your opponent s score. Pressing both buttons will then display the scores in the format: Extension You scored 2 Your opponent scored 1 Extend your game to declare a winner automatically. Again, pressing Button A should increase your score and pressing Button B should increase your opponent s score. If your score reaches 2, then the message YOU WIN should be displayed. However, if your opponent s score reaches 2, then the message YOU LOSE should be displayed. 13

Introduction to the BBC micro:bit Task 4: Using the micro:bit to Control Temperature In this task you will learn how you can use a computer, such as a micro:bit, to control the temperature of a room. Most modern offices have air conditioning to keep people comfortable. If the temperature is too low, heaters come on and circulate hot air around the room. If the temperature is too high, special refrigeration units cool the air and circulate it. The system constantly monitors the room temperature and circulates hot or cold air to keep the room at the desired temperature. It s a simulation In this task, you will simulate the heating and cooling systems coming on using the micro:bit display and write code to control the temperature of your imaginary room. To simulate heating and cooling, use two different pictures on your display (Figure 1). Cooling On Figure 1 Cooling Off Solving the problem Break the problem down into simpler parts. This is called decomposition. + + IF the temperature is greater than our set temperature, THEN you need to turn on the fan. + + IF the temperature is less than our set temperature, THEN you need to turn on the heater. Repeat these two steps constantly to monitor the temperature and turn on heating or cooling as needed. Cooling down Using the Block Editor, create the code shown in Figure 2 below. The code will automatically start running in the simulator (Figure 3). Figure 2 Figure 3 14

KS3 Use the temperature slider to increase the temperature slowly. What happens? Now decrease the temperature. The fan does not go off again. Why not? Adding some heat Add a second IF statement to your code to make sure that the heat comes on if the temperature drops below 24 C. Download Before downloading your code to your micro:bit, change the desired temperature to 30 C. Now you can use the method in the paragraph below to heat the room. Changing room temperature The microcontroller in the top corner of the micro:bit (Figure 4) has a built-in temperature sensor. You can simulate the room temperature rising by putting your finger on the microcontroller to warm it up. To simulate the room cooling down, take your finger off the microcontroller. Extension Figure 4 Air conditioning systems allow people to set a temperature they find comfortable. Change your code to increase the set temperature when Button B is pressed and decrease it when the A Button is pressed. Hint: You will need to use a variable to store the set temperature. 15

Introduction to the BBC micro:bit Task 5: Using the micro:bit as a compass A compass is an instrument used for navigation. The cardinal directions, or points, are North, South, East and West. The diagram below shows a compass rose with the cardinal points clearly displayed. As well as the cardinal points, the compass shows angle markings in degrees. North corresponds to zero degrees and the angles increase clockwise: East is 90, South is 180 and West is 270. The micro:bit has a special sensor that allows it to be used as a compass. Its output is in degrees, from 0 to 359. In this activity, you will work with a partner to turn your micro:bit into a compass that displays the cardinal points: North (N), South (S), East (E) or West (W), depending on the direction it is pointing. Exploring the micro:bit compass Open the Block Editor and create the following code: Run this code on the micro:bit simulator. Press Button A and watch how the micro:bit displays the compass heading. Turn the compass dial to change the heading and press Button A to see what happens. Converting to cardinal points Reading the heading in degrees is a bit awkward on the micro:bit. Also, many people don t want this level of accuracy. They just want a compass to display N, S, E or W. In this part of the activity, you will use computer logic to convert the heading in degrees to the nearest cardinal point. Create the following program in the Block Editor and run it. Image (top) mas0380 istock/thinkstock 16

KS3 Turn the compass dial to different values and see what happens. With your programming partner, discuss how this code works. Extend your program to include South. To do this, you need to add more parts to your if statement, by clicking on the button. Now drag an else if part into the if function. Click the button to get back to your code. Extend your program to include South. Extend your code to include the extra detail below. The headings for South (S) are still at 0. Working with your partner, use the compass rose above to work out the range of headings for South. You should have created a compass that displays two of the cardinal points. Complete your compass by extending your program further to include West and North. You will have to calculate the heading ranges for each. North is slightly trickier, so work out the heading ranges for North with your programming partner. Now download your code to your micro:bit and test it. Extension Your compass is very basic. When sailors use a compass, they use the intercardinal or ordinal points: + + Northeast (NE), 45, halfway between North and East + + Southeast (SE), 135, halfway between South and East + + Southwest (SW), 225, halfway between South and West + + Northwest (NW), 315, halfway between North and West Work with your programming partner to change your code to include the intercardinal points. 17

Introduction to the BBC micro:bit Task 6: Using the micro:bit as electronic dice Learning intentions In this task, you will learn how to: + + create and use variables; + + generate random numbers; and + + edit JavaScript code. Many board games use dice to decide how many places you move along the board. In this project you will program the micro:bit to generate two numbers when it is shaken. The numbers can be displayed by pressing Buttons A and B. The flowchart on the right shows how this program might operate. On start-up, the micro:bit waits until it detects being shaken. The program then generates two random numbers between one and six. If either of the buttons is pressed, then the micro:bit displays the number for that button. If the micro:bit is shaken at any time, then two new numbers are generated. Creating variables You will need two variables to store the random numbers generated when you shake the micro:bit. Call these variables numa and numb, so you know which one to display when you press the A and B Buttons. Follow these steps: + + Click on the Variables block set, then click on the grey Make a Variable button. + + Type the name of your new variable, numa, in the window that opens and click OK. + + Repeat the first two steps to create a numb variable. + + You have now created a new variable. If you want to use this variable, you can find it in the Variables block set. 18

KS3 The code Now that you have created both variables, you can enter the rest of your code shown below: About the random numbers If you look carefully at the code, you will see that the random number generator on the micro:bit always starts at 0; we can t change it. As you want numbers between 1 and 6, you need to change each variable by 1 (add 1 to it). If you didn t, players could roll a 0. It doesn t look real! The code works fine and displays two random numbers every time. However, it doesn t look like real dice. The LEDs could display those dots really well. You can create realistic dice pictures, but you have to do some work to store all those pictures first. Creating the dice images If you were writing this program on a computer, you would probably create an array of six pictures and then ask for the one you want by number. The micro:bit can work with arrays, but in the Block Editor these can only be arrays of numbers. The solution is to use a big image. If you go to the Images block set (click the arrow beside Advanced to find it), you will see a create big image block. This looks promising, but it is not big enough! There is only enough space for two images. To solve this, you need to go to the JavaScript behind your code. This is the code your programs are really written in. The Block Editor just makes it easier for you to understand your code. 19

Introduction to the BBC micro:bit First, change your block code. + + Create a new variable called diceimages. + + Then, create a new on start block and set diceimages to a big image as shown here. You already know that this is not big enough. Click on JavaScript at the top of the window and see if you can change it there. The JavaScript code should look something like this. Don t worry if your line numbers are different. The five lines of dots from line 17 to 21 are the important part. Each dot matches one of the boxes on your big image. You can change this code and add as many dots across as you want. If you want a pixel on the display to be red, change the corresponding dot to a #. 20

KS3 The completed big image for all six dice faces is shown below. Copy it carefully, because making mistakes will mess up your dice. Now change the rest of the code in the code editor as shown below. Remember, you no longer need to add 1 to the random numbers. This is because the offset to find the first pattern (a 1) is zero, the start of the big image. The other patterns are found by increasing the offset in steps of 5. This is why we multiply by 5 in the code below. Warning When you return from the JavaScript to your block code, your diceimages variable will have disappeared. Make sure you create a replacement before trying to enter the code below. Extension Yahtzee is a game played with five dice. Amend your code to generate five random dice patterns. Hint: There are two possible ways you could approach this: 1. Create five variables to store the five values and then display their patterns. 2. Create an array with five elements and store the values there. In both cases, use Buttons A and B to step forward and back through the five patterns. 21

Introduction to the BBC micro:bit Task 7: Using the micro:bit to control digital output There is only so much you can do with the micro:bit on its own. Knowing the temperature in the room is too low and showing that on the display will not make the room warmer. You need an output from the micro:bit to control a heater that can actually make the room warmer. The micro:bit has pins that you can use as outputs. These are the gold pins on its long edge. Pins 0, 1 and 2 are wide enough to attach crocodile clips. This makes connecting other devices to the micro:bit really easy. To use the smaller pins, you need to solder wires to them or use one of the special expansion boards available for the micro:bit. Controlling a pedestrian crossing In this task, you will use the micro:bit to control three LEDs to simulate the traffic lights at a pedestrian crossing. To solve this problem, look at all the states the traffic lights can be in. There are four: + + red + + red and amber + + green + + amber The traffic lights can only be in a single state at any one time and will only move to the next state when the right conditions occur. The diagram on the right shows all the states and what condition is needed to move to the next state. The code In this task, you will use subroutines (called functions Block Editor) to control each of the states. On start-up, the RedState function is called to set the lights to red, a safe starting point. The function for the next state will only be called when the right conditions are met. Open up the Block Editor and enter this code: 22

KS3 Can you explain why there is a 10-second delay in the green state before it will look for Button A being pressed? Simulation You can simulate the code in the micro:bit simulator. Just watch for the output pins turning red for each step in the sequence. A Real Simulation If you have some coloured LEDs, you can make your simulation more realistic. The diagram below shows you how to connect the circuit on a breadboard. The resistors limit the current through the LEDs. Further Activities This code only controls the traffic lights. It does not turn on the green or red figures for pedestrians. Extend your code to add two more outputs for these pedestrian lights. Hint: you have used P0, P1 and P2 for the traffic lights. You can use the other 16 pins for additional outputs. In the simulator, these smaller pins will also turn red when they are written to. If you want to run this code on a real micro:bit, you will have to find a way to add more LEDs to it. Special expansion connectors, such as the one below, are available that enable you to do this. Search online for microbit expansion board for examples. Extension 1. The green figure starts to flash to warn pedestrians that the lights are about to turn amber and that they should not start to cross. Can you extend your code so that the figure flashes green for a few seconds before it changes to red? 2. Most pedestrian crossings also make a sound when the figure is green. Can you extend your code to add this feature? You could use some of the Music blocks. 23

Introduction to the BBC micro:bit Task 8: Using the radio Introduction One of the most exciting things about the micro:bit is the built-in Bluetooth radio. This allows you to pair your micro:bit to your phone or tablet. You can find lots of information on how to do this on the micro:bit website. The micro:bit can also send open broadcasts using its radio chip. This is what you will explore in this section. There are different projects you can try to explore the features of the radio. Before you start Follow these two steps before you start using the radio feature. 1. Pick a radio group If all of your class are using the radios on their micro:bits, it is important that each group uses a different channel. Fortunately, there are 256 channels to choose from. 2. Set the transmit power There are eight power levels, from 0 to 7. The default is 0, which is fine for communication over short distances. If you need a longer range, for example if you are controlling a robot, then you will need higher power. The higher the power, the faster your batteries will drain. Both the radio group and the transmit power should be included in an on start code block. Sending data The micro:bit can send both numeric and text data. The table below shows the blocks used to send different date types. Block What it does Using the radio send number block, you can send a number or numeric variable. This could be useful if you wanted to send data such as temperature or acceleration from a remote micro:bit. Using the radio send text, you can send a string or a string variable. This could be useful if you wanted to send a plain text message. 24

KS3 This block allows you to send a string and a number in the same transmission. This is particularly useful if you need to label the data sent. For example, you might want to send acceleration and temperature data from a radio-controlled car. Receiving data There are three basic blocks for receiving data. These pair with the sending options, so the receiving micro:bit should use the radio received block with a data type matching the radio send block that is sending the data. The table below shows the three basic radio received blocks. Block What it does Using the on radio received received number block, the micro:bit will receive a number sent by another micro:bit in the same group. You can then display this number on the screen or use it in your code. Using the on radio received received string block, the micro:bit will receive a string of text sent by another micro:bit in the same group. You can then display this on the screen or use it in your code. Using the radio received name, value block, you can receive labelled data from another micro:bit in the same group. The label can be used to identify the micro:bit sending the data. Customising how you receive data You can customise any of the on radio received blocks. If you click on the little gear icon in the topleft corner of the block, an editing window opens. The left column lists the types of information that can be read. You have already seen that a micro:bit can send numbers, strings of text or a combination of the two. Each transmission includes three additional pieces of information: time, serial and signal. If you want to receive this information, drag it into the Packet bracket in the right column. 25

Introduction to the BBC micro:bit By customising a radio received block, you can decide exactly what information you want to use. This allows a lot of flexibility when using the radio to communicate between micro:bits. The table below lists each of the types of information that you can send, along with a brief description. Information receivednumber receivedstring time serial signal Description This is a single number sent by a radio send number block. This is a string of text sent by radio send string block. This is the system time of the micro:bit (from when it is connected to power). It is sent in every data transmission. It isn t very accurate. Tests have shown it is about 3.5 minutes out in 24 hours. However, it is perfectly good if you want to measure the time between signals sent from two micro:bits. You could use this to time projects using remote micro:bits. If you want greater accuracy, there are some great tricks online to calibrate your micro:bit clock. This is the serial number of the micro:bit. It is sent in every data transmission. It is a convenient way to identify the micro:bit that sent the message. This could be useful if there are a lot of micro:bits in the group. You could even build a network. This is the signal strength. You can use it to measure the distance between two micro:bits or to stop a buggy, if it looks like it is about to go out of range of the controlling transmitter. The micro:bit will continually receive data that is sent. It is important to make sure that your code is capable of handling this possible continual stream and not lose important data. Practice tasks Learning intentions In this task you will experiment with the micro:bit radio to find out how to: + + send number data; + + send text data; or + + send mixed text and number data. Complete each of the following tasks and observe what happens. 26

KS3 Sending a number Enter the code below onto two micro:bits. Power up both micro:bits and press Buttons A and B. Observe what happens. Now try to explain to your teacher or group how this program works. Sending a string Enter the code below onto two micro:bits. Run this code on both micro:bits. How is this code different from Task 1? Sending string and number pairs Enter the code below onto two micro:bits. Run this code on both micro:bits. What do you see this time? What advantage is there to sending both text and number data in the same message? 27

Introduction to the BBC micro:bit Task 9: A micro:bit weather station Learning intentions In this task, you learn how to: + + send and receive data using the micro:bit radio; + + use name and value pairs to request data type; and + + select correct output depending on the data type. Many people have a digital weather station that tells them about the weather in their locality. Information might include outside temperature or air pressure. In this project, you will create your own weather station, using two micro:bits. One will be inside the house. It will allow you to choose the information you want to see and then display it in the most appropriate way. The other will be outside, transmitting back the requested data. The code This time use different code on each micro:bit. Label them indoor and outdoor. Download the code below onto your indoor micro:bit. 28

KS3 Now download the code below onto your outdoor micro:bit. Power up both micro:bits. Press Buttons A and B on the indoor micro:bit and observe what happens. How well does your weather station work? What improvements could you make? Things to try 1. The micro:bit displays the received temperature and light levels, but it does not tell you what it is displaying. Amend your code so that it will tell you what it is displaying. For example, it could display temp is and then the temperature followed by C. 2. Extension activity It might be better to display the outside temperature constantly. Amend your system so that: + + the outside temperature is constantly displayed on the indoor micro:bit; and + + the outdoor micro:bit updates the temperature every 30 seconds and only sends the light level when it is requested. 29

Introduction to the BBC micro:bit Task 10: Not in the office Learning intentions In this activity, you will learn how to: + + use an array to store data; and + + send data using the micro:bit radio. If you work in an office, people will often knock the door or simply walk in. This can be annoying if you are busy and it may even disrupt an important meeting. In this task, you will create a messaging system to tell visitors whether they can come in or if you are busy or away from your desk. This task requires two micro:bits: one to request entry and display the response and the other to send the message. There will be three possible messages: + + Enter + + Busy + + Not in office To keep things simple, we will select the required message using the buttons, as follows: + + Button A Enter + + Button B Busy + + Buttons A and B Not in Office If you think about it, you can t use the third option if you are in the office because you won t be there! So, you will have to work out how to lock the system to give this response every time you are out of the office. Arrays An array is a sequence of storage locations in memory that holds data of the same type. You can find individual items in the array using the array name followed by its location. For example, the first element in the array will be array [0]. As arrays start at zero, because computers count from zero, the fifth element in the array will be array [4]. Microsoft MakeCode editor has two ways to make arrays. The image on the right shows the block instructions. The first instruction creates a blank array with the required number of elements. The second block instruction allows you to pre-fill an array with strings. This is the option to use in your project. 30

KS3 The code You will have to use two different programs on two micro:bits. Label one micro:bit door and the other office. The code for the office micro:bit is listed below. Download it to a micro:bit. Notice that we created three variables that have constant values: free, busy and out. These map to the elements in the array. Why didn t we just use 0, 1 and 2? The answer is because these constants make the code easier to read. We often call these numbers that just appear in code magic numbers, because other programmers looking at the code have no idea where they came from. Now download the code below to the door micro:bit. It s much simpler. Did it work as expected? Did you spot a problem? Things to try There is one major problem in the code on the office micro:bit. When you press a button to say that you are in, out or busy, nothing is displayed to tell you what you have set. Change the code and add a forever loop to display the state constantly. You can get it from the array using the same code: The value returned will be a string, so you will then need to display this string. Extension Make it your own Why don t you change the code so that you can use it for your bedroom? What messages will you send? 31

Introduction to the BBC micro:bit Task 11: Radio Transceiver Learning intentions In this task, you will learn how to: + + connect two micro:bits using the Bluetooth radio; + + send codes between two micro:bits; and + + output analogue signals via micro:bit pins. You have already learned how to transmit numbers and text between micro:bits. This has limitations because you have only a limited number of preprogrammed choices. What if you need to send a message between micro:bits? Speech would be good, but that s not really possible. Instead, your best option is to use codes to represent the characters of the alphabet. Morse code was first used in the nineteenth century for telegraphy. The code uses combinations of dots and dashes for each letter or number. You can see them in the table opposite. In this project, you will use Button A to send dots and Button B to send dashes. Morse Code A.- N -. 1.---- B - O --- 2..--- C-.-. P.--. 3 -- D -.. Q --.- 4.- E. R.-. 5.. F.._. S 6 -. G --. T 7 -- H. U..- 8 ---.. I.. V - 9 ----. J.--- W.-- 0 ----- K -.- X -..-..-.-.- L.-.. Y -.--, --..-- M -- Z --..?..--.. Connecting a Speaker You need to connect a speaker to the micro:bits to allow you to hear the code received. Connect a specific type of buzzer (a piezo transducer; your Technology teacher will have these) between Pin P0 and GND, using miniature crocodile clips as in the photograph below. The volume should be loud enough for you to hear. 32

KS3 The code Use the same code on both micro:bits. As they are transceivers, they can transmit and receive at the same time. However, you have to use your ears to decode the messages and your thumbs to send messages. Can you do both at the same time? Enter the code below and download it to two micro:bits. Important: if other pairs are using the radios, make sure each pair sets a different radio group in the radio set group block or it could get messy! You can pick any group between 0 and 255. Extension The speed of the code is fixed. This may be a problem, if you can t decode it quickly enough. Can you change your code to adjust the speed of the dots and dashes? Hint: You could use the tilt left and tilt right inputs. Remember the rules Follow these rules to make the code intelligible: + + dashes are three time longer than dots; + + the space between dots and dashes is the same length as a dot; and + + the space between characters is the same length as a dash. 33

Micro Bit images released under a Creative Commons Attribution-NonCommercial 4.0 International license. Images Copyright Micro:bit Educational Foundation

CCEA 2018 COUNCIL FOR THE CURRICULUM, EXAMINATIONS AND ASSESSMENT 29 Clarendon Road, Clarendon Dock, Belfast BT1 3BG Tel: +44(0)28 9026 1200 Fax: +44(0)28 9026 1234 Email: info@ccea.org.uk Web: www.ccea.org.uk