Skip main navigation

Explore the code used for collecting data

Explore the code used to collect the data from the previous step.
Explore The Code Used For Collecting Data

Step Counter

Let’s look closely at the code that was used to collect the data from the previous step.

Like the juggling video, I used the accelerometer that’s on the micro:bit to gather data from the physical world. I wanted to store the number of steps I was taking so I created a variable.

Variables

Variables are places to store information in a computer program. They can be different types – words or numbers which can be called strings and integers.

You can have lots of different variables so you’ll need to name them.

In my code below, I created a variable called steps to store the number of steps. It’s a good idea to give your variables sensible names. steps is the perfect name for this variable. I could have called it number of steps but I felt that was too long. In some programming languages there are strict rules about the names of variables.

In this sample program below I created the variable Number of steps in blocks, but when I switch to JavaScript: you’ll notice it is now called number_of_steps. This is because in JavaScript you cannot have spaces in variable names. MakeCode fixed this error for us by putting underscores in place of the spaces. Number of steps became Number_of_steps.

VariablesVariables
(Click to expand)

Algorithm

Here’s the algorithm for the program:

  • When the micro:bit shakes – it increases the number of steps by 1
  • When I press A on the micro:bit – it shows the number of steps
  • When I press B on the micro:bit – it sets the number of steps to 0

Basic code

Using the algorithm as our guide, here’s the code in blocks:

Step counterStep counter
[(Click to expand)]

You could try recreating this code yourself or just have a look at it here: https://makecode.microbit.org/_4ju9DmHX42oV

I also set my variable steps to be 0 when the micro:bit starts. Steps will be 0 when it starts but it’s good programming practice to set up all your variables at the start of your program. Other programming languages require the variable to exist before you can change it.

When the micro:bit is shaken I change steps by 1 first, then show the number. This is an important sequence. If the micro:bit shows 0 after they have taken a step it will look like it’s not working.

Intermediate coding

When numbers go bigger than 9, the micro:bit screen scrolls them. This can be very slow to watch. For a more advanced program I used the grid of 25 LEDs to show me my progress. I wanted to reach a 100 steps so every time the micro:bit shakes I want to graph the number of steps out of 100.

Code: Intermediate step counterIntermediate step counter
(Click to expand)

Link to the code for you to edit: https://makecode.microbit.org/_7EhJ1rAP53fc

Advanced coding

Nothing happens when I get to 100! I’m going to use selection to check if steps is equal to a 100, celebrate then set steps back to 0.

I’ve also fixed the bug I spotted in the two previous programs by clearing the screen.

Why not have a go at this yourself before looking at the answer? https://makecode.microbit.org/_YegTuDXrtMm3

Matching theory to code

Did you spot the importance of sequence and selection in the examples above? Does the theory help explain the code better? Or does the code help explain the theory? How do you learn?

This article is from the free online

Build a Physical Computing Prototype

Created by
FutureLearn - Learning For Life

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education