NCW Day 2 – Dice

Welcome to our second blog post, in a series of five, in celebration of National Coding Week. If you haven’t already, take a look here to find out more about what we’re doing, and why we’re doing it.

The next aspect of our game involves a random number selection, and in our case, digital dice. There are a few ways you could go about this, but we are going to use the Matrix display to represent the dots, as it is already in a nice grid.

For this you will need:

First of all, we are going to connect together the components. Connect the +ve and -ve on the battery box to the respective connections on the Crumble (left hand side). Make sure to keep the batteries switched off for the moment.

Next, we need to connect the Matrix Display. Connect the remaining +ve and -ve on the Crumble to the corresponding connections on the Matrix board. Looking at the Matrix board with the text the correct way up, it’s the pads on the left-hand side. Then connect D from the Crumble to the board – notice the direction of the D arrow, it should point away from the Crumble. If you are using the Matrix Pixelator, you may need to look carefully at which way around your board is in the holder.

Before we come to write our code, we need to work out what our numbers (or spots) will look like. It can be a good idea to plan this to save confusion later on.

Now we come to writing our code. This program is similar to the colour picker from yesterday, but instead of assigning a single colour to a value, we are going to assign an ‘image’; or multiple Sparkles. Lets start, as before, with two choices. We’ll look at choosing either 1 or 2. First of all, lets get our variable set up.

Now we can add a condition in. Remember that we can use the ‘IF ELSE’ statement as our options are mutually exclusive; our ‘RandomNumber’ cannot be one and two at the same time! If the generated number is one, then we want the Matrix display to show one, otherwise the generated number must be two and so we want to display two on the Matrix. We’ve also turned off all of the Sparkles at the beginning, which makes sure that our Matrix is ‘reset’, and we’ve got a wait statement, which gives us time to move our hand away from switching on the power.

Now we want to extend our code, as before, to have as many options as we require (6), and so that we can choose our number at the push of a button. Wiring in a switch is simple. One side connects to a free +ve connection, and the other to an I/O pad on the Crumble – we’ll go for A.

Using the switch within our program is easy. Put your code within a ‘do forever’ loop, so that it runs indefinitely whilst the Crumble is powered, and then we can use a ‘wait until’ block. This behaves like a pause in our program, so that it won’t move on until the condition is met, which in our case is when the button is pressed and A is HI. The wait statement from earlier comes in useful here too, as does clearing the Sparkles.

And there we have our digital die, ready to roll (excuse the pun)!

If you have a go at this project, or any other, we’d love to see! Get in contact with us via email, FacebookTwitter or our Forum.

Comments are closed.