September 13, 2005
Programming the PIC: Memory Game
The goal of the lab for this week was to program the PIC (Programmable Interface Controller). I created a simple game: There are 7 yellow LEDs, one red LED, and two buttons. One of the buttons is for gameplay, and the other is a sort of meta-button, used for starting/resetting the game and for advancing from level to level while playing the game.

The switch labeled "built switch" above is simply two bare wires that can be tapped together to "press" the switch. The gameplay starts as soon as the player pressed the built switch. The yellow LEDs all blink twice and then they blink in succession, from the first to last. Each LED blinks a random number of times between 1 and 3 inclusive. As soon as the last yellow LED has blinked, the game goes into a ready state while it waits for the player.
The player plays the game by pressing the momentary switch. Each time she presses it, the first yellow LED lights up. When she believes she has blinked the first LED the correct number of times, she presses the built switch to advance to the next level (the next LED). The red LED lights once to indicate that it has moved to the next LED. If the player blinks an LED too few times before advancing, or too many, the red LED lights continuously to indicate that the player has lost. The game then goes back into a ready state until the user presses the built switch to start the game again.
If the player succesfully makes it to the end of the game, all the LEDs blink up and down like a ripple and the game goes back into a ready state to wait until the player hits the built switch to start again.

You can see that I used the same soldered-resistor-and-LED combination I did during Lab 1. This made it easier to fit all the LEDs next to each other, and removed some of the logistical headaches in making everything fit well.
The coding of the PIC was by far the hardest thing. My code is pretty clean, but not nearly as efficient as I'd like. I had trouble getting for loops and dynamic variables working well, so I created separate loops for a lot of functions that probably could have been handled using a single function (for instance, I created a separate loop for each "level" rather than one loop that just iterated for each new level). Also, by far the biggest problem with the game is the random number generation. Every time the power is turned off and on, the game starts up with the same "random" configuration (hint: all but the last two lights blink 3 times). I never figured out a good way to be more random.
Code
The code.Posted September 13, 2005 03:21 PM. Categories: Labs , Week 2 | Permalink