Archive | National Coding Week

NCW Day 5 – Game Time!

Welcome to our fifth and final blogpost from our National Coding Week special. If you’ve missed any of the other four, or want to find our more about what we’re doing, and why we’re doing it, then head over here to find out more.

This final post, as mentioned previously, is all about playing a game. We are going to combine together each of our previous elements, and turn them into something playable! To recap, we’ve got the Colour Picker, the Dice, the Letter Spinner and the Countdown Timer.

Before we get into the fundamentals of the game, we are going to combine together each of our individual elements, so that at the push of a button, our game runs! If you remember, each of our projects shared the fact that they were started at the push of a button, or when the I/O pad was HI. By tweaking each project to turn an I/O pad, connected to another Crumble, to HI at the right moment, we can cause a chain reaction! To get a better sense of what is going on programmatically, lets look at our new Colour Picker code.

Notice the addition at the end of the code. We’ve set C HI, which is connected to A on the next Crumble (running the Dice program). We then wait 100ms and set it LO again.

We’ve changed each program (apart from the Countdown Timer) so that it runs the additional code snippet after it’s finished it’s main purpose. We have changed the Countdown timer code so that it doesn’t wait for another input to reset the servo, rather it resets itself after a few seconds.

To get this to work correctly, we need to have common grounds (-ve). The easiest way to do this, like we have, is to use a single power supply.

This is only one of the ways to connect your projects up.

Lets look at the chain reaction in action!

Now we come to the game, which is played as follows. The aim is to come up with as many words as you can, which are as long as the number on the dice, within the time limit (one and two may be tricky, so you could remove them). The colour at the beginning relates to a bit of a forfeit which takes place during the round. These are as follows:

  • Red – Scattergories mode. If any of your words are the same as anyone else’s, they don’t count for anyone! You need to be unique to win.
  • Green – Hand switch. Swap your usual writing hand, for your non-dominant one.
  • Blue – Shout it out. Every time you write down a word, you have to shout it out.
  • Yellow – Normal. No bizarre rules, just write down as many words as you can.

Let’s look at an example.

Here we’ve ended up with green, five and G. This means that we need to write as many five letter words as we can, that start with the letter G, in 30s. But we have to do it with our non-dominant hand, so in my case I would swap to my left hand to write with.

And there we have our game ready to play! Clearly you aren’t limited to this version of the game. There’s a plethora of possibilities out there using these examples or combinations of your own.

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

NCW Day 4 – Countdown Timer

Welcome to our fourth 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 part of our game is going to be a countdown timer. The aim of this project is fairly self explanatory. Our game requires a time limit, so what better way to keep us within the time limit than with a timer not dissimilar to the one from Countdown!

For this you will need:

For the eagle-eyed amongst you, you may realise that we have already done a Countdown Clock Project. This is going to be an updated version of it, but we will go into less depth with the programming. If you are unsure of why we did something, go and check out the original post for help.

To begin with, 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.

Now we are going to connect our servo. This requires a +ve and -ve connection, which we can take from the right hand side of the Crumble, and then one I/O pad; we’ll use D, to keep the wiring neat!

To get the basic servo-powered timer working, we need to think about what we are trying to achieve. Let’s say that that we want the timer to last for 30 seconds. During this time, the servo needs to travel 180 degrees, from the top of the clock face, to the bottom. Therefore our servo needs to travel 180/30 degrees (=6) every second. We can achieve this using a variable and a loop. We set our servo to 90, then every second, we subtract 6 from the servo angle and re-set it.

To make this switch controlled, we can use a similar method to our previous NCW projects. Within a loop, we wait until A is HI before starting our timer code. We then have another ‘wait until A is HI’ after our main timer code, which works in such a way so that the servo resets and waits to be started again. You’ll notice the extra wait statement – this allows the servo to ‘reset’ before you press the button to start it again.

Given that it is unlikely that the players will be looking at the clock, we can also add in a buzzer to give us an audible cue as to how much time has passed. This will also help ramp up the pressure, making the game more exciting! We’ve connected the buzzer to B, and to the negative pad on the battery box. This means that when we set B HI, the buzzer will sound.

To use this within our program, we need to turn on the buzzer, and turn it off again in between setting the servo angle (our original timer code). The total amount of time we need to be waiting, still needs to be equal to 1 second (buzzer on for 0.2 seconds, and off for 0.8). Notice where we have turned the buzzer on towards the end. This sounds the buzzer continuously until the clock is reset. This is useful to know when the time is up!

We now have a working 30s timer! Although the servo isn’t very useful at the moment, so we are going to embed it with our Countdown Clock Template. For instructions on putting this together, head back to our original project page.

And there you have it, a 30s Countdown timer!

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

NCW Day 3 – Letter Spinner

Welcome to our third 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 part of our game is going to be a letter spinner. The aim of this project is to randomly choose a letter of the alphabet, spread around, by way of a large spinning arrow. We will borrow from the foundations of our Probability Spinner Project and tweak it.

For this you will need:

For starters, 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.

Now we are going to connect a motor, with the pulley attached. You can use either motor connection on the Crumble, but we’re using motor 2. Connect the red lead of the motor, to the + on the motor pad, and the black to the – .

Getting our motor to move is simple, as is getting it to stop, but we want to make it spin for a random amount of time. To do this, we can use the ‘random _ to _’ block; a common occurrence throughout this blog series. When the Crumble receives power, we will give ourselves 2 seconds to move our hand away, then we will turn the motor forwards. We will wait a number of milliseconds, between 3000 and 9000, and then stop the motor.

To make the spinner easier to use, we can use a push switch to trigger the spinner. This connects to a + connection, and an I/O pad (A, B, C or D).

As with our previous posts we are going to ‘pause’ the program and wait until the input (D) is HI’. Once the switch is pressed, the program continues and our spinner code will run. All of this will then be inside a loop, to allow for infinite letter choosing! Or until the batteries run out, or we get bored.

Although this spinner is perfectly adequate, to make it more realistic, we want it to slow down gradually. We are going to add a variable called ‘Speed’, which will start at 100 (%). When the spinner is due to stop, we will run a loop which subtracts varying amounts from ‘Speed’, bringing the motor to a gradual-ish stop.

Hint: we use the condition ‘if speed < 1’ as the speed value may not equal 0. If we didn’t, the value could become negative and the motor would begin speeding up in the other direction!

And there we have a our letter spinner ready to rock!

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

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.

NCW Day 1 – Colour Picker

Welcome to our first 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 first aspect of our game is going to be a colour picker. The aim of this project is to set a Sparkle to a random, but predetermined colour. Or in other words, choose from a set choice of colours (that we have programmed). This could then go on to be used to pick an option from a list or a team to take part etc.

For this you will need:

First up, 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, connect your Sparkle. Connect the remaining +ve and -ve on the Crumble to the ‘in’ on the Sparkle (the D arrow should be pointing to the right). Then connect D from the Crumble to the Sparkle, taking care to get the direction correct, as previously mentioned. Use the diagram below if you’re unsure.

Now we come to writing our code. To begin with, we are going to randomly choose between two colours. To do this, we want to think about assigning each of our colours a number – starting from zero e.g. Red = 0, Green = 1.

At the the beginning of our program, we want to choose a random number within the range of our list. So either 0 or 1. Once we have chosen the number and ‘remembered’ it (by assigning it to a variable), we can work out what colour to set the Sparkle.

This is achieved by a series of selection (or ‘IF’) statements – basically questions. In English, it would be ‘If the random number is 0, then make my Sparkle red’. Notice the wait statement – this just allows us time to get our hand away from the switch, but it also gives the illusion that the Crumble is ‘thinking’. Also notice where we turn the Sparkle off at the beginning. Whilst the Sparkle has power, it remembers the colour it was last told to be. Although at the moment we are using the battery pack to switch on and off, we will need this in the future, so it’s good to put it in now.

We could repeat this for the other colour, changing the numerical value and the colour of the Sparkle, and this works just how we want it to.

However, there is a bit of redundancy within our code. As our two conditions (if RandomNumber = 0, and if RandomNumber = 1) are mutually exclusive (they can’t both be true), we don’t need to check the second condition. If the ‘RandomNumber’ doesn’t equal 0, then it has to equal 1 (as long as the random number generation is correct). Therefore, the best way of writing this code is as follows.

From this point on, it should be easy to see how you would extend this code to choose between even more colours! See if you can choose between 4 different colours?

We have a working colour picker, but at the moment it only works once. So, instead of triggering our program by powering the Crumble on, we can add a push switch instead. This way, we can repeatedly generate our colour! If we connect up our switch to A (and a +ve connection), it’s really simple to add in the code to start our colour picker.

We have used the ‘wait until’ block, which halts the program until A is HI (the button is pressed). Our wait statement now also gives us time to remove our finger from the switch. If it wasn’t there, we could repeatedly choose a random colour, and it would be impossible to use. We’ve also put our code inside a ‘do forever’ loop. This runs the code indefinitely, whilst the power is on. Turning off the Sparkle is now really useful! Not only can we see the new colour when the Sparkle lights up, there is no confusion if the colour picked is the same as last time.

And there we have our colour picker!

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.

National Coding Week – Blog Special

Next week is National Coding Week, and we thought to celebrate this, we would release a series of blogs dedicated to a project theme. This time we have chosen ‘games’. Each day there will be a new blog post detailing how to create a simple game-related project. Then at the end of the week, each one of these projects will connect together to form a game, which we will go into more detail on Friday.

As it stands, here is our current plan of action: Monday – colour picker using a Sparkle; Tuesday – dice using a Matrix display; Wednesday – letter spinner; Thursday – audible countdown; and Friday – Playing the game!

Each individual blog (apart from Friday) aims to provide you with a small project that you can do discretely, and then if you complete each one, you will be able to combine the smaller projects into a form of game.

Like other initiatives, National Coding Week is designed to encourage people of all ages to try something new, around the theme of coding/programming. There is a massive emphasis that not just schools get involved, but individuals and businesses too! You can find out more on their website.

If you have a go at any one of these projects, we’d love to see! Get in contact with us via email, FacebookTwitter or our Forum.

NCW Day 1 – Colour Picker

Welcome to our first 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 first aspect of our game is going to be a colour picker. The aim of this […]

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. […]

NCW Day 3 – Letter Spinner

Welcome to our third 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 part of our game is going to be a letter spinner. The aim of this project […]

NCW Day 4 – Countdown Timer

Welcome to our fourth 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 part of our game is going to be a countdown timer. The aim of this project […]