Reaction Timer Project

reaction timer cover

Task: To create a reaction timer game

Difficulty: Intermediate

DSCF3748

For this project you will need:

  • One Crumble with USB lead;
  • A computer with the Crumble software installed;
  • A battery box with batteries (not rechargeable!);
  • A Sparkle Baton;
  • A push switch;
  • Seven croc-leads.
DSCF3445

First of all, we need to connect all of our components together.

We need to connect up the battery pack to the Crumble (keep it turned off for now). We need to take care with this as we must make sure that the + on the battery pack goes into the + input of the Crumble, and – on the battery pack connects to the – one on the Crumble.

DSCF3445

Now we need to connect our Sparkle Baton. We connect the + and – on the Crumble to the corresponding + and – on the Sparkle Baton (the left-hand side – the arrow will point to the right). Then we need to connect D on the Crumble to the D input on the baton.

hint: when connecting the Sparkle Baton, place it so that you can read the words along the top.

Finally, we just need to connect our switch, where one end connects to the + of the battery and the other end to A (or B or C).

Now we need to program our game. We are going to focus on the basic version of the reaction timer to begin with, and then make improvements later on.

We want it so that the program waits for a random amount of time, and then turns on the Sparkles sequentially, waiting 100ms each time. The player needs to press the button as soon as they see the lights, and then it will stop the game and show the user their score using the sparkles.

basic sequence2

Our first step is to get the Crumble to wait for a random amount of time, and then light up the Sparkles sequentially.

All of our code is within a ‘do forever’ loop, to allow the game to replayed an unlimited number of times. Then we have added a ‘wait random 1 to 10’ statement, so that the player doesn’t know when the Sparkles are going to turn on. Next we have set the variable ‘t’ to 0. This will act as our loop counter, and it allows us to light up the sparkles sequentially. Finally, we will light up a Sparkle (t), then wait 100ms, and then increase t by 1. This repeats until t = 8.

hint: we do until t = 8 as the loop stops when it reaches 8, so if we put 7 in the loop, the eighth Sparkle (7) wouldn’t light up!

basic sequence2
a is hi change

Now we want to make it so that the sequence stops when the button is pressed. We can do this simply by replacing the ‘do until t  = 8’ with ‘do until A is HI’.

basic game

We want our game to display the user’s ‘score’. After the player has pressed the button, we are going to turn off all of the Sparkles and wait for 250ms (to stop the program skipping the next bit). Then we are going to  subtract 1 from t (the user’s score). This is so that we display the Sparkle they stopped it on, and not the next one.

Now we’re going to show the user’s score in one of two ways, until they press the button to reset the game (A is HI). If they didn’t stop it in time,and their score is above 7, the whole baton flashes red. Otherwise, the Sparkle they stopped it on will flash blue.

Then when the button is pressed, the Sparkles will turn off again and the game will reset.

hint: the player’s score is the Sparkle number (not index – so starting from 1) x 100ms.

basic game
sequence countdown

Our final addition to this code adds a nice indicator at the end before the next game starts – this section of code has been added to the end of the previous.

First of all we will turn off all of the Sparkles and wait for a moment. Then, if the player scored above 7, the score (t) is set to equal 7. We then flash Sparkle(t), and subtract 1 from t – this repeats until t = 0. This creates a sequence of  flashing Sparkles from the player’s score. We then light the final Sparkle (0) green, as an indicator that the game is about to start.