This challenge involves making a reaction timer game with a Sparkle Baton. The aim of the game is to press the button when you see a green light. We used:
- 1 laptop with Crumble software installed
- Crumble controller & Sparkle baton
- 1 Crumble-friendly battery box; 3 AA batteries
- Croc-leads and a micro-USB cable
- A switch (check out our new Crumble-friendly switches!)
Connecting the Crumble
The Sparkle baton is connected in the same way as a single Sparkle: `+’, `-‘ and `D’ on the right-hand side of the Crumble connect to their equivalent pads on the left-hand side of the baton. The switch is connected between the +ve terminal of the battery box and input B on the Crumble (see photo).
Programming the Crumble
The program was developed and refined in several stages. First, I wanted a lit LED to “travel” backwards and forwards along the Sparkle Baton: The variable “t” is the index number of the Sparkle. The variable “u” switches between +1 and -1 every 8 steps, so the “let t=t+u” command increments the Sparkle number from 0 to 7 then decrements it back to 0. At each step, Sparkle “t” is set to red for 100 milliseconds. This whole sequence is placed inside a “do forever” block.
Now, as the red light travels back and forth, we want just one of the Sparkles to turn green, instead. The “set sparkle ‘t’ to red” block is, therefore, replaced with an if-else statement, which changes Sparkle number 3 to green:The program now needs to respond to user input: the aim is to press the button when you see the green light. Inside the “do forever” loop, we nest a “do until B is HI” loop.
When the button is pressed, the program will pause until it is released (“wait until B is LO”). If the user has succeeded in pressing the button when the green LED is lit they have “won”.
In order to emphasize this, a “victory flash” is now inserted: if “t=3” when the button is pressed (i.e. if the Sparkle is green) then all the Sparkles flash green 3 times before the program continues.
In order to increase the challenge, a variable “time” was created, which determines how quickly the Sparkles turn on and off. Initially, this is set to 300ms and it decreases by 50ms for each successive “correct” push of the button. If the button is pressed at the wrong time, however, the time is reset.
Can you improve on the game? Ideas:
- Is there a potential problem if the variable “time” becomes negative? Would it be better to decrease it proportionally rather than by a fixed number of ms?
- Can you keep track of the “high score” by counting the number of successive, correct button pushes and flashing the same number of Sparkles?
- What if the green Sparkle is in a different position each time? (Hint: use the “random” operator.) Does this make the game more challenging?