Let's put our reaction time to the test with this skill game for Micro:bit and GlowBit™ rainbow. The light bounces back and forth, press the button when it's in the middle - how high can you score?

Transcript

We're going to create a skill tester game using a rainbow and a micro bit. This game has the LED bouncing off each end of the rainbow and if we press the button when it's just in the middle we'll get a higher score, but the LED will also get a little bit faster each 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. And this is our MicroPython programming environment. This looks like the default project. I'm not going to need any of this code.

So I think there's two parts to this project. The first is to animate the dot moving back and forth across the rainbow. So we'll need some logic to bounce off the ends of the rainbow and then we need some logic to detect when a button is pressed and if it's pressed at the right time we can increment the score and get faster, but if it's not pressed at the right time we can just reset the Micro:bit and it'll start the program all over again.

I'll start with importing NeoPixel. Glowbit are compatible with the NeoPixel driver library and I know I'm going to need some random numbers later so from random I'll import grand int.

I'll set up my rainbow. Rainbow equals NeoPixel dot NeoPixel. We're on pin zero and we have 13 LEDs.

Now I'll create a variable to keep track of which LED should be active at any time and I'll call that active and for now I'll start that off with zero.

So we need to keep track of which LED is active and also which direction it's moving in. We're goingTo add one or minus one to move it forward or backwards. Equals one.

So the first thing we'll do is to clear the strip to reset it for the new game state and call rainbow clear and I found that it's often a good idea to include a sleep, a very short sleep here.

Next we'll illuminate the active LED. Rainbow active equals and I'm just going to use a pure red here. 60 red and zero green and zero blue and then we call rainbow show.

Now to animate the LED moving across the rainbow we're going to have to increment this active variable. We're going to increment it by the direction amount which will be either plus one or minus one so we can change directions.

Next I'll say active equals active plus direction and I'll include a sleep for 400 just so we can see what's going on.

Connect your Micro:bit and flash and we can see the LED moving across the rainbow until it hits the end. Maybe this plastic will help you see. It moves along the rainbow and when it hits the end the program actually goes into an error state because we're trying to address an LED that doesn't exist. We've gone off the end of the rainbow so we need some logic to bounce it back to move it backwards when it hits the ends.

Under that increment I can add some logic if active is greater than or equal to 12 which is the last which is the last LED on the rainbow and we can say direction equals negative one. So that will handle hitting one end of the rainbow but now that we're on the way back down to zero we need to handle hitting that end of the rainbow as well. So we can include another if statement statement.

Else if active is less than or equal to zero we can say direction equals one. Let's flash that and see what happens. We're moving up and we've bounced.Off the end and we're moving back down again, and we should bounce off this end too. Great, so we've animated our game. Now we need to include the game logic. Just add some comments here: "Bounce off the ends" and "This is to animate the rainbow".

So, after our sleep, we can implement the game logic. So, I'm going to use button b for this game. So, we can include an if statement: "if button b was pressed" and we want to check that the active LED is number six (that's the LED right in the middle).

So, if button b was pressed at any point during this part of the game, which is when the active LED is lit, if active is equal to six, that means we win that round. We're going to keep some score, and the score will also make the game get faster. So, I will increment score: "score equals score plus 25". But score doesn't exist yet, so up here in our initialization, we'll just start off with score equal to zero.

Now, if we press button b when LED six is active, then our score is going to increment. Great! Now, for every case where we press the button and it was not LED six that was active, that means we've lost. We've pressed the button on the wrong LED. So, we can say "else" and we can print to the console our final score: "scored" and that's our variable. So, we can say "scored some number of points" and we can insert the score with format.

So, this print statement will say "scored" and then this syntax will grab the value of variable score and include it in the print message, and it'll tell us how much we scored. We can then sleep for some amount of time. I'll say a second and then reset. This will reset the Micro:bit and the game will start all over again.

You know, I think we're nearly done with our game. I am just going to make the game get harder. So, every time you score a...New point: The game should get a little faster, otherwise you could just keep playing it forever. And a really easy way to do that is to take out our long game delay, that's the delay between every time the LED updates, and just subtract score from that. So the delay will start off at 400 milliseconds and then after we score a point it will go down to 375 milliseconds. Every point we score, the delay is going to get shorter by 25 milliseconds.

And we are ready to rock. I'm going to flash this. I'm going to poise myself ready to play. Open the serial so I can see my score. All right, I got one, two, three. I can see the LED is a little bit faster now. It's getting slightly faster each time. Oh, it's getting significantly faster now. That 25 millisecond difference really starts to matter. Oh, and I just lost. I just hit the button while the LED was one off and I can see in the console that I scored 250 points.

I'm just going to add one small feature to this game, and that is to change the colour of the LED every time there's a new game. I'll do that by creating some variables: red equals rand int 0 to 60, and I'll paste that twice. So I've created a random colour by mixing red, green, and blue. And now in our game, I can replace this hard-coded red with red, green, and blue. I'll flash that. Now our game is this kind of like bright white-blue color, and if I lose the game on purpose, we get this green color. Now a blue color. So every time we play a new game, we'll have a new colour LED. That just creates a little bit of interest. Every time we start a new game, we'll be playing with a new color.

There you have it, a very simple game using the rainbow and a micro bit. I'd love to see if you incorporate any new features into this game. JustSure, I can help you with that. Here's the closed-captions transcript formatted into paragraphs:

"Open a thread on the Core Electronics forums and 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.