All pages
Powered by GitBook
1 of 5

Loading...

Loading...

Loading...

Loading...

Loading...

A - Events

blocks, events and messages

In this section, we are learning different ways for the micro:bit to receive commands, or as they are called in coding, events. Events enable the interaction between us and the device (micro:bit). Some equivalent events in our daily life are:

  • Pressing a key on your keyboard

  • Starting a car by turning a key or pressing a button

  • Saying a command to enable your phone's digital assistant

The events we are exploring in the micro:bit are on start, forever, on shake and on button A pressed.​

Create a new project and clear the Workspace​

Create a new project by going to makecode.microbit.org and deleting the current blocks (on start and forever) from the Workspace by dragging them to the Block groups area.

​Events blocks

We will code our micro:bit to display the word "Hi", using different events. Go over the process and try to spot the differences and when would you choose one over the other

on start, forever, on shake and button A pressed

  • on start runs the code inside it as soon as the micro:bit is powered. A very useful place to set up your application.

  • forever executes the code inside it as long as the micro:bit is powered. Forever, as long as the micro:bit is running.

  • shake is lots of fun since it executes the code inside it when the user shakes the micro:bit. This is possible as the micro:bit can detect acceleration.

  • on button A pressed works just like your tv remote or the buttons on a game console. The device detects a button being pressed and runs the code inside it.

Did you notice that the blocks in the Block groups area are colour-coded? This makes it easy to look for blocks you see in the example activities.

We can trigger events with many different blocks in MakeCode as we saw in this section, but so far we only displayed text. In the next section, B - Animations, we are working with images.

02 - MakeCode

www.makecode.microbit.org

MakeCode is a platform that helps us create programs that the micro:bit can understand in any of three different computer languages, Blocks (KCJ's choice), JavaScript and Python.

Not only MakeCode is the ideal environment for us to teach code and interact with the micro:bit, but it also has a built-in micro:bit emulator, providing an easy and fast place to test our code.

In this section, we'll go over the main MakeCode platform concepts that your students need to be familiar with to comfortably start their coding journey with us. Start by visiting on your favourite browser the following site makecode.microbit.org.

Once on the site, click on the big purple button that says New Project to navigate to the main application. You will be prompted to give your project a name and then click on the Create button.

The process may take some time depending on the computer speed and internet connection, but once all is loaded we'll be in the main area of MakeCode.

MakeCode Work Area

Workspace

MakeCode can be divided into three different areas. the Workspace, the Blocks and the Emulator. Let's take a look at each of them:

1. Workspace

This is the empty area on the right side of the screen. This empty space is waiting for you to add blocks to provide instructions that the micro:bit will execute. By default, it has the forever and on start blocks.

2. Block categories

Block categories

The Blocks area is where all your available instructions are located, conveniently grouped into categories and colour coded.

3. Emulator

your own virtual micro:bit

This is our testing ground, the virtual micro:bit. This emulator has most of the functionality of the physical version and a few extra tricks that you'll learn at some point.

There are many more options to explore on the screen, but these three are the essentials that will enable the students to start using MakeCode

With a micro:bit and MakeCode, we could create interesting applications to visualize data, but we are introducing more tools that will add variety to our creations.

If you like to learn more about MakeCode and the micro:bit, continue to A - Events. If you are already familiar with micro:bits, continue to the section where we explore our first tool, the Servo Motor.

B - Animations

​Images are very useful to express complex concepts in a simple and fast way. They are also fun and creative! In this section we are going to experiment with images and animation, bringing our creations to life.

The micro:bit only has 25 LEDs to create text and images, but you'll be surprised at how much can be said with so few lights.

​Display a heart when the button B is pressed

We'll start simple and add complexity and concepts as we learn. First a big heart!

Test your progress using the micro:bit emulator!

​Beating Heart

The image looks great, but an animation is even better!

Always after each set of code, make sure to test on the Emulator.

It's alive! Well, at least the heart beats once. Now it is time for a mini-challenge!

Every time you see the red question mark​❓, it indicates a mini-challenge for you to try. After completing the challenge or giving it fair try, proceed to open the container and see how we solved it.

❓Have the heart animation repeat 4 times

We can achieve this effect in several different ways, but we'll take this opportunity to introduce the repeat block.

Test, test, test on your emulator!

Imagine how many other different animations we can create. Maybe you can tell a story using a micro:bit animation!

In the next section, we are getting serious about math. See you in Counting!​​​

🍎Teacher's Corner - LEDs
  • LED stands for light emitting diode. LED lighting products produce light up to 90% more efficiently than incandescent light bulbs. How do they work? An electrical current passes through a microchip, which illuminates the tiny light sources we call LEDs and the result is visible light. To prevent performance issues, the heat LEDs produce is absorbed into a heat sink. source

  • Some more information from MakeCode.

🍎Teacher's Corner - Loops
  • In computer science, a loop is a programming structure that repeats a sequence of instructions until a specific condition is met. source

  • More information about Loops from MakeCode

D - If Hot

​ ​

In this section, we are taking our code to a new level. As humans, we see, smell, touch, taste and hear the world around us, and we are going to use some of those senses to help the micro:bit do something similar, by utilizing different sensors built into it.

The first sensor we'll work with is the ability that the micro:bit has to detect temperature and along with it, we'll learn the concept of if statements in coding.

If statements provide computers with the ability to choose between different options or paths. It opens many different possibilities and makes our programs more interesting and diverse.

We don't need a physical micro:bit to test the temperature option. The emulator provides a way for us to see and test different temperature levels.

​Temperature

Getting a temperature reading from the micro:bit is very simple. We use the provided block temperature (C), wrapped by the show number block.

Don't forget to test your progress using the micro:bit emulator, or a micro:bit, if you have one available.

​If Statements - Conditionals

If-else statements are used to control what the code will execute when a particular condition is fulfilled. In a way, they provide options for our code to make choices.

  • Add the if block to our existing code

  • Add the comparison block located in the Logic section

  • Complete the if block by:

    • Adding the temperature block

    • Change the < (less than) sign in the middle to be > (greater than)

    • Add a number that would indicate a hot temperature

By now you know the routine: test the application in the emulator or on an actual micro:bit if you have one available!

These projects ​​​will help you when working on the main Activities as they all use micro:bits and MakeCode as the brains controlling the visualizations and servo motors. It is time to continue, learning about the other tools. See you in the next section 03 - Servo Motor.

🍎Teacher's Corner - If statements
  • The if statement executes a statement if a specified condition is truthy. If the condition is falsy, another statement can be executed.. MDN

More about if statements from MakeCode

​​​🍎Teacher's Corner - Logic operators

For more information about the provided blocks regarding logical operators visit:

  • Greater than link

  • Equality link

  • AND link

  • OR link

  • Not link

Some more information from MakeCode

  • logic link

  • compare link

🍎 Teacher's Corner - Battery pack

his and many other applications created for the micro:bit can be better explored using a battery pack. It is not necessary but gives mobility to the micro:bit.

micro:bit battery pack info

C - Counting

In this section, we are doing math with our micro:bit. Math is a key element in any human endeavour and after this lesson, you will be coding your own calculations in no time.

Let's start our Math and micro:bit exploration by counting. First, we will create a counter from 1 to 5 using a basic approach, and then we'll find a better way to do it, with more control.

Before attempting each of the following parts, try to create code that counts, while considering the following points:

  • What to display on the micro:bit?

  • When to change the count? What action on the micro:bit will trigger the count?

  • By how much are we counting? Are we counting up (adding, multiplying) or down (subtracting, dividing)

  • What blocks do we need to display our count?

See more information about these powerful concepts in the Teacher's Corner - Counting, below

Count from 1 to 5 when buttons A and B are pressed at the same time (A+B)

This initial method counts all the way from 1 to 5 as soon as we press A+B. As you can see, we are introducing a new block, show number. This block works just like show string, but it is used to display numbers and do mathematical operations.

As we said, this works, but we have no control over when to count, it just does it and it will never go past 5! Don't forget to test your progress using the micro:bit emulator!

Controlled count - PART A - introducing variables

Our code is about to get more powerful as we will introduce a variable, called counter that will keep track of the number of times we press A+B.

Variables are places in the micro:bit or computer where we can store information.

  • The first step is to create the variable

  • Give the variable a name, in our case Counter

  • Now we have access to new blocks!

    • Counter stores and returns the value. In this case, the count

    • set defines what is the value of the variable

    • change, lets us change the value of the variable

Controlled count - PART B - coding the counter

All that's left to do is to use our new blocks to count by 1, each time we press A+B

Reading the code.

When the application starts, the Counter variable is set to be zero (0), then when the user presses the A and B buttons at the same time, the variable Counter increases by 1 and then the new value of Counter is displayed.

Make sure you test it in the emulator to better understand the previous sentence!

​​Great work in getting the counter working! time for a mini-challenge.​​​

❓Count by 2, or by 5!

How did you do? I'm sure you did great!

Solving this problem opens many possibilities as you can count by any number you want. This is our solution to counting by 2.

Hint: we don't always need to count by adding. Changing our variable by -2 will result in subtracting

We have made lots of progress and in the next section If Hot!, we will explore some really exciting concepts in coding and the micro:bit!​​​

🍎Teacher's Corner - Counting - Computational Thinking

Asking your students to think about the challenge, to count, helps create deeper connections beyond the micro:bit and coding. It will help introduce Computational Thinking and how we use it in our daily lives.

Computational Thinking

Decomposition: breaking down something complex into smaller, simpler pieces.

Pattern recognition: matching new incoming information with information retrieved from memory.

Abstraction: identifying essential information while ignoring irrelevant details.

Algorithms: following simple steps to solve problems or tackle a challenge.

🍎Teacher's Corner - Counting - Variables
  • A variable is a container for a value, like a number we might use in a sum, or a string that we might use as part of a sentence. MDN

  • More about variables from MakeCode

  • Variable can have any name you want, but it is considered good practice to provide meaningful names that describe what the variable stores.

Algorithms and Data Literacy