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