Now I have got the underlying parts of the code working I can start thinking about the overall structure of the program. Perhaps the best way to do this would be to write the user instructions for the game, this will give me a specification which I can them write a program to implement these.

User Instructions

When you turn the game on the screen displays the following:


Reaction Game
*************

After a few seconds this screen changes to:

Press the button
to start

Press and release the button to start the game running. After an interval the LEDs will light. As soon as you see them light you must press the button again. The faster you press the button the lower your score! When you press the button the screen changes to:

You scored 150

When you press the button again the game restarts.

Once we have established the instructions we can break them down into a sequence of actions and then write the code to do each one. Fortunately everything we need to do we have already done in a program at some point, it is just a question of putting each item into the program in the correct sequence. You can use the code we have already created as a resource kit.

I have decided to use four LEDs for output, PORTA 1-4 and PORTA bit 0 as the input. This means that my setup function will have to put TRISA to correct pattern.

The table on the right gives the sequence of the program, and some hints as to how to implement each part.

Requirement

Hint

Set up the hardware and get the interrupts running. Make just the bottom bit of PORTA an input.

Make some changes to setup_hardware to do this

Print out a banner hello message. Put your name in it.

We will need to assemble a string which contains the message, print it to the LCD and then pause for a few seconds so that the user can admire it!

Turn off the LEDs

These should be turned off ready for the next game. We just have to set the required bits in PORTA.

Print out a start message and wait for a keypress.

We need another string which contains the message to print and then we need to wait for a button press, preferably debounced.

Since we have to wait for a key pressed at other parts of the program it makes sense to use a function. Program 2.8 has a function called key which might be useful here, but it will need modifying to work with PORTA. We can repeatedly call it until it returns 1, or we could write a function called keywait which returns when a key is pressed.

Wait for a while

We need quite a long delay here, and the delay functions we have written so far only let us pause for up to around a second. To get more than this you must put one delay loop inside another, using the same kind of tricks as we used in Program 2.5.

Light a LED

You can to this by dropping the appropriate value into PORTA to turn the LEDs on

Start the timer counting.

At the moment the timer counts when the pin on PORTA is held down. Instead we could use a flag called "timer_active" which we set to 1 to indicate that the timing should take place. (of course we must remember to set the timer counter to 0 as well)

Wait for a keypress

We can call keywait here

Stop the timer

We clear the "timer active" flag at this point, leaving timer_counter with the count value

Work out the result and print it

We already have a way of printing decimal numbers, so we just need an appropriate string of text here

Wait for a keypress

This gives the user time to read the result

Go back to the start

We should put the code into a while loop to do this.