Upload
hoangcong
View
244
Download
10
Embed Size (px)
Citation preview
How to Program with the BBC micro:bit
2
Contents
Website………………………………………………………………………………………....... 3
Using Microsoft Block Editor……………………………………………............ 4
Your First Program…………………………………………………………………......... 5
Compiling your Program…………………………………………………………………. 6
Uploading to your micro:bit using a MAC………………………………….. 7
Uploading to your micro:bit using a Windows Computer………… 8
Display Letters and Words…………………………………………………………… 9
Display an Image………………………………………………………………………........ 9
Changing Images……………………………………………………………………………… 10
Loops………………………………………………………………………………………………….. 11
Brightness………………………………………………………………………………………... 12
Variables…………………………………………………………………………………………… 13
Random Number Generator…………………………………………………………… 14
IF / ELSE………………………………………………………………………………………….. 15
Display a Random Image using IF………………………………………………… 16
Multiple IFs………………………………………………………………………………………. 17
3
www.microbit.org
Click here
You have a choice
of programming
languages
We used
Microsoft Block
Editor
4
Microsoft Block Editor
How to start a program
Programming
Commands
Programming
Area
Micro:bit simulator
to see your
programs working
Clicking on one of the Coloured
Programming Areas will display
all the options for that Block of
Commands.
Simply click and drag the block
you want to use into the
Programming Area
5
Your First Program
This simple program creates the image of a Dog using the led’s (Light Emitting
Diodes).
Step 1
Give your program a name – “Dog” in this case.
Step 2
Drag the Purple Input block into the programming area.
Step 3
Drag blue/grey Basic block into the programming area. Drag this block inside
the purple block. Make sure it clicks into place.
Step 4
Press the RUN button at the top of the screen. This part is often forgotten,
especially after making a change to a program.
Step 5
Click the A Button on the simulator. You should see your program running.
6
How to compile your program
If you want to put your program onto a physical micro:bit, you will need to
compile it first.
Click on the COMPILE button at the top of the screen
The compile button will spin and you will see the following page for a few
seconds
The program will have been saved into your area as a “.hex” file. Giving your
program a name BEFORE you compiled it will make it easier to find
7
How to upload to a BBC micro:bit – MAC
The program will be in your “Downloads” folder
Plug your micro:bit into your MAC. It will show on your screen as a new drive
Reduce your Downloads window so you can see the micro:bit drive and simply
drag the program into the micro:bit
Your program is now on your micro:bit and ready to run
8
How to upload to a BBC micro:bit – WINDOWS
You choose where to save the file
I saved it in a new folder called Microbit Files
Plug the micro:bit into your computer. It shows as a new device
I can then simply drag it from the Microbit Files folder into the
Microbit that shows up on the left of my screen
Your program is now on your micro:bit and ready to run
9
Skill Builders
Display a message when button A is pressed
The message is called a “string” which means it can be letters, words, numbers
or a mixture of any of them.
The message will scroll sideways across the micro:bit
Display an Image when Button A is pressed
10
Display swaps between 2 images with a short pause when Button A is
pressed
You can add extra images
11
Display swaps between 2 images with a short pause when Button A is
pressed. This will be repeated 5 times using the “repeat” block.
This is called a LOOP
12
Changing the Brightness of the LEDs
You can change the brightness of the LED’s by using the SET
BRIGHTNESS block
13
Variables
Sometimes you will need to use a VARIABLE. This is a value that can
CHANGE. You will need to give a variable a NAME.
Notice that once you have created a variable and given it a name, it
appears in the VARIABLES set of blocks for you to choose.
14
Random Number Generator
This Block creates a Random Number between 0 and a number of your
choosing. REMEMBER that computers see the number 0 as the first
number in a list so the block above would pick 1 of 5 choices.
Either 0, 1, 2, 3 or 4.
This works well with a VARIABLE.
These blocks click together to create a VARIABLE called RANDOM
NUMBER which will produce a random number between 0 and 4.
15
IF / ELSE
This block is really useful, but you need to build it to use it. It is used
to choose between two different outcomes.
Step 1
Select the IF block
Step 2
Click on the Blue Square to see your
options
Step 3
Choose the ELSE block and click it
inside the IF block
Step 4
Click on the Blue Square again.
This is your new block
16
How to use the IF block with a VARIABLE and the RANDOM
NUMBER GENERATOR blocks
Every time the A Button is pressed, the number 0 or 1 will be chosen
randomly. I have called this variable “Heart” which describes the
image. Image 1 will be displayed if the Random Number is 0 and
Image 2 if the Random Number is 1. You DON’T have to actually use
the number 1, as it falls into the ELSE part of the LOOP.
i.e.
If the random number is 0, show the FIRST Image, ELSE, show the
SECOND image.
Random Number = 0
Random Number = 1
17
Lots of Ifs
If you want to have MORE than 2 results, you need to create an IF
statement that also includes an ELSE IF block
You can add as many ELSE IF blocks as you like. It is GOOD
PRACTICE to end with an ELSE. This means there will ALWAYS be an
output as ELSE covers every other option.
18
To allow 3 possible outcomes, the code looks like this