Add some colour to your games night with this dice project. We'll display two six-sided dice rolls on the GlowBit™ rainbow and program the Micro:bit to select random numbers.

Transcript

If you want to add a bit of colour to your games of chance, then we're going to make this electronic dice. This is two independently rollable dice that can count from one to six independently, and I can choose to roll the orange dice or the fuchsia, the purple dice, independently, or I could roll them both at the same time.

Let's get started. We'll start by wiring our rainbow to our micro bit. I'll use black for ground, ground on the Micro:Bit, ground on the rainbow, red for three volts, and yellow for data. That's pin zero on the Micro:bit and data on the rainbow.

Connect your Micro:bit to your computer with a USB lead, and with your browser navigate to python.MicroBit.org. This is our MicroPython programming environment. This looks like the default project. I'm not going to need any of this code. I, of course, need to import neopixel. Glowbit is compatible with the neopixel driver library, and of course, we're making some dice, so I'm going to need some random numbers from random import randint so we can generate random integers, that's whole numbers.

We need to set up our rainbow on pin zero, and we have 13 LEDs. So this project's all about displaying two separate pieces of information on the rainbow. We have our like dice A and dice B, so that means we're probably going to need to use two different colors. I'm going to define orange to be 51, 33, and 0, and fuchsia to be 51, 0, and 51. Fancy purple.

So the way our dice works is that depending on which button we press, we can get a dice roll on each half of the rainbow. So I'll check first up if button A was pressed, and that's going to be roll the left die. Oh, I forgot my colon.

So we're going to need some value, some dice value, and that's going to be a random integer.Between 0 and 5. It could be 1 to 6 but we'll pick 0 to 5 because we can index our LEDs very easily that way.

So now we just need to eliminate that number of LEDs. I'll run a for loop for I in range 0 to 6. And how can we check if we need to light that LED up? Well if we roll a 3 we want to illuminate LEDs 0, 1 and 2. So that really means anything less than the number we roll.

If I is less than, and actually it's going to be less than or equal to because we are off by one because we're standing at zero. If I is less than or equal to value, rainbow I equals, and we'll use our first colour orange.

So for every LED we check if it's less than or equal to the value that we rolled and if it is we set it to some color. And that means that we set it to some colour and that means that if it isn't else, if we rolled less than the current LED then we'll set that LED to all zero which just means off.

And then the final thing we can do is execute rainbow dot show. Let's connect to our Micro:bit and flash the script.

Okay I'll hold this plastic over maybe you can see it a bit easier. I've rolled a 2, a 4, a 1, a 2, a 4, a 1, a 4, a 5 and I want to roll a 6. There we go. Am I going to roll anything else? So it looks like we have one of our dice working.

Now since the functionality of the second dice is pretty similar I'm going to start by copying all of that and pasting it again. I'll just zoom in, maybe we can see the differences a little easier.

I'm going to roll the second die so I want to check if button B was pressed and that's the right die. And now we went from 0, 1, 2, 3, 4, 5. So we went from 0 to 5.

So here I want to go from 7 to 12. And I could leave this random integer because you know dice have six faces so I could roll.0 to 5 or 1 to 6 but I think it's still going to be easier to match it to match the range of values to straight to the index that I'm working with. So I could make this 7 to 12. So in fact it's like our second dice has a bottom value 7 and a top value 12. But when we read it we'll just see 1 to 6 LEDs. We'll use the same logic again and we'll make this fuchsia. So this would this will give us our second working die.

Let's flash the code. Okay we've got the left die and we've got the right die. But at the moment the right die is working just like the left die. It's counting from left to right. I think this would look nicer if it moved in symmetrically from the outsides. That's quite easy to do. All we have to change is this if condition. We can turn this to a greater than, greater or equal to, and now my right die and my left die both work from the outside in. Pretty cool.

Now you might have seen something that's a little bit confusing. Here we're generating numbers, let's let's use the left die as an example. Here we're generating integers from 0 to 5. So that's 0 to 5 inclusive. So why do we run our for loop from 0 to 6? Well if I open up the REPL, if I write a loop for I in range, and I don't actually need that first that first value. I can just say in range 6. By default it will be 0 to 6. I can print I and then I'll hit enter a few times for it to execute. And you can see that a for loop of this nature, even though we have range 6, that's not inclusive. We have 0, 1, 2, 3, 4, 5. So we actually do count from 0 to 5 in this loop. You can think of this number as being the total number of times that a loop would execute, because if we start from 0 we have 1, 2, 3, 4, 5, 6 loop.Iterations. So if you find that your code is sometimes just off by one, that's very common and it could have something to do with the way that your loops work.

So there's a little bit going on in this project. We have some random number generation for games of chance. We're displaying two separate pieces of information on our rainbow just by using two different colors. And we're monitoring for independent button presses.

In any case, if you make something cool out of this we'd love to see it over on the Core Electronics forums.

I'll see you in the next project.

Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.