Dance:bit

This tutorial will guide you through how to make a dance machine using a micro:bit.

Objective:

  • Program a micro:bit to display instructions of dance moves for players to copy
  • Instructions should be displayed in a random order
  • Instructions should get faster as the game goes on

Learning Outcome:

  • Creating images as variables
  • Creating and retrieving items from arrays
  • Using random numbers
  • Iterating numbers

Level of Difficulty:

  • Advanced

Coding

Step 1

Start by opening the micro:bit editor.

You should see a blank editor. If not, create a new project.

Step 2

In the section, click .

Name the variable rest.

Step 3

Drag a  and connect it inside .

Click the item dropdown and change it to the variable rest.

Under the  section, from  drag in a  
and connected it to the
set rest to block.

Tick the boxes to create a shape of a resting body.

Step 4

Repeat Step 2 to create the following variables:


Step 5

Repeat Step 3 to create the following images:

You now have 7 variables, each representing an image of a dance position.

Step 6

Next, you will create a new variable, except this one will not be an image, instead it will be an array. An array is a list of items - you will use this later to select a random item from the list. That random item will be a random image.

Create a variable named listOfMoves and connect a set listOfMoves to under your other variables.
Under the
 section, find  and drag in a  and connect it to set listOfMoves to.

Click on  to see:
 

Drag in 6 more values into the array to make a total of 7.

Click
 again to hide the pop-out.

From drag in your 7 dance position variables and connect them to the array.

Step 7

Now you’re ready to display some images. From  add a  to the   section.

To choose an image from the listOfMoves array you need to tell it the number of the item in the list. The first item is number 0, the next is 1, up to 6 for the 7th item.

From  drag a  and connect it into the show image block. Change list to listOfMoves.

The simulator should show the rest position on the micro:bit. This image is the first value in the list (item 0).


If your micro:bit simulator is greyed out, check that all of your blocks are connected correctly and click the
 button under the micro:bit.

Step 8

Instead of always showing the rest position (it wouldn’t be a very interesting dance), you want to show random positions. For this you need a random number between 0 and 6 (one for each value in the array).

From the  section, drag in  and connect it to get value at but change the 4 to 6.
 

You should now see the micro:bit displaying different images of random dance positions.

Step 9

The images are probably changing a little too fast for a beginner. Let’s slow things down a little.

Create a new variable named pause and connect a set pause to under your other variables in the on start section. Change the value to 3000. The pause is timed in milliseconds, 1000 milliseconds is 1 second.

From the  section, add a  to your forever loop underneath show image. Change the value to your variable pause.

Step 10

Now the changes are slower, but we want them to get quicker as the game goes on. To do this you need to change the value of pause.

From drag in a . Connect this under forever after your pause. Set the value to -100. This will set pause to 100 less than it was before (3000, 2900, 2800, 2700… ) each time an image is shown.

Step 11

The last thing to add is a way to reset the timing. Simply add a  block. Within that, from add a  but change it to set pause to 3000.


Great Work!

Test this out on your micro:bit by clicking  and copy the file to your micro:bit using the USB cable. Grab some friends and get dancing.

Challenge

Try adding some new moves, maybe right arm up AND left leg out, or something completely crazy! Don’t forget to add the new variables to your listOfMoves and change the maximum random number.