Falling block matching game

Download source

I’ve always considered embedded programming as something that only people who practice black magic can do, with the aid of sacrificing several small animals. I’ve come to terms with the fact that writing application code allows you to manipulate data and do interesting things, like mine databases or create games, but I just assumed that getting your software to interact with something physical, something that I can pick up and spin in my fingers was something that would be well beyond the realms of my intellect. Luckily I like a challenge, and given the opportunity I figured I’d give it a go.

It sort of came about by accident really, one day I was browsing reddit when I saw an ad spruiking NerdKits at the top of a page. I checked it out, and it turns out it’s a bunch of guys in America who decided to make a beginners pack for embedded programming. I, along with a bunch of other redditors (Note to self, when I have something to sell, advertise on reddit) handed over my cash money and received the kit. I went through the tutorials, then started modifying them to try and learn some more things. Not long after that I realised that if I was going to actually learn anything I was going to need a project, so I though what a better idea then writing the ‘Hello World’ of the game world: Tetris Falling block matching game. [1]

So off I went, and here are my results…

Finished breadboard Finished breadboard

There she is. Not the most beautiful piece of electronics you’ll ever see, but it works. Here’s a rundown of the parts…

  1. Push Button – Move piece left
  2. Push Button – Move piece right
  3. Push Button – Rotate piece
  4. Atmega168 Microcontroller (MCU) : Stores the program, and allows you to tie together the pieces.
  5. ADC (Analog to Digital Converter)– Random number generator
  6. Crystal Oscillator – The clock for the circuit
  7. SPDT (Single pull double throw) switch. Used to set the board to either run normally, or put it in programming mode to update the MCU.
  8. Resistor (1K Ohm) – used to set the contrast on the LCD
  9. Voltage regulator

That’s it. The only components I had to get separate from the original Nerdkit were the push buttons, although they were bigger than I had anticipated and things got crowded on the breadboard pretty quickly. Next time I’ll be sure to check the dimensions of what I’m buying, but I think it would also be a good idea to get a bigger breadboard anyway. I ended up using the ADC to generate my random numbers, which is used to determine the next piece. I got the idea from the NerdKit forums, where basically the last significant bit is so volatile due to temperature shifts and noise that every time you poll it you should get something different.

Breadboard and LCD Breadboard and LCD

It took a while to become familiar with how to interperate the values sent to the MCU and how to handle interrupts, but like most things once you get your head around it it’s not that bad. What completely threw me off was a whole range of new things that can go wrong. From my non-electronics background, I ‘knew’ that a switch has two states, on and off, but what I learned was that when you press a switch the state actually flickers between on and off for a few milliseconds until the switch comes to rest. This wreaks havoc when you are reacting to whenever a switches state changes. I had pieces flying all over until I read up on debouncing , which is basically just not taking a switches value when it’s changed straight away, but giving it time to come to rest first. My approach was that if the button has been in the same state for the last 10 checks, then it is safe to consider it changed, and then use it’s value.

Game in motion Game in motion

For the future, I am thinking about adding sound and some other features to my game, although I also would like to start playing around with motors and sensors…

Game over Game over

[1] Originally I was going to call this simple game Tetris, although I went for an interview at EA, and was warned that there was another game with the same name already. That’s one of the things I like most about 'Falling block matching game', how it is in no way like any other game created.

Chris Dempsey