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:
- a Crumble Controller
- a battery box with batteries (not rechargeable)
- crocodile leads
- a Sparkle
- a push switch (optional)
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!