Friday, December 27, 2013

Speed Tester v2.0

Back in April I made an ATmega328P based speed and reaction time tester. Since then the game has been tested numerous times on our friends and relatives and it has also been a huge hit at a certain kindergarden! Last summer I was given orders to make a new device for a family of relatives. The game would be a christmas present for the whole family so utmost secrecy had to be maintained while building it. Also, the new device would have to be vandal proof because they have three kids :)

Instead of building an exact copy, which would have been boring, I decided to make a completely new revision. In this blog post I'll lay out the major changes and discuss some challenges I met.

But first a video!

From wall wart to battery powered

The old device was powered by a wall wart which was a little cumbersome to use and limited the usability somewhat. I decided early on that the new version would be battery powered. As this was my first real battery powered project, I had to do some research about battery efficiency. The old device uses a 7805 voltage regulator to convert a voltage between 7-15 volts down to 5 volts, a voltage more suitable for microcontrollers. The problem with 7805 is that it turns the extra voltage to heat and thus wastes a great amount of energy. Also the 7805 requires that the input voltage is about 2 volts higher than the output voltage. With batteries this would mean that once battery level would drop below the threshold voltage, the regulator would shut down, and thus even more battery capacity would be wasted.

I looked into another options like using low dropout regulators and even diodes to drop the voltage (the voltage drop of a typical diode is about 0.7 volts) but this was a dead end. Then I discovered the wonderful world of switching regulators. These tiny devices turn the voltage on and off very radiply and after suitable filtering this produces the desired output voltage. The amazing thing about switching regulators is that when the regulator switches off no current flows. This means that switching regulators are very efficient and only minimal amount of energy is lost. 

There are many kinds of switching regulators. Step Down voltage regulators work like the 7805, they take a higher voltage and drop it to desired output voltage. Step Up regulators work in the opposite direction. The issue with batteries is that battery voltage varies a lot during the lifetime of a battery as battery capacity is drained. Therefore a Step Down voltage regulator is not a good option for us; a Step Down regulator shuts down like the 7805 when input voltage drops below target voltage. Using a Step Up regulator to set up from 3 volts (two AA batteries) to 5 volts would probably be an acceptable solution, but I figured I would need the extra juice of 4x AA batteries.

Luckily I found a perfect match for my usecase: the Pololu S7V8F5. It is a combined Step Down/Step Up regulator. This little beast, about the size of a 7805, is capable of taking any voltage between 2.7 and 11.8 volts and turn it into steady 5 volts with typical efficiency over 90%. This is just what I needed to powered the device with four AA batteries.

Left: 7805 voltage regulator
Right: My new best friend, the S7V7F5 Step-up/step-down regulator

I also made some optimizations to reduce the power consumption of the hardware. First I lowered the MCU clock frequency from 16 Mhz to 2 Mhz by setting the 1/8 prescaler fuse bit. This reduced power consumption quite a bit (about 10mA less current drawn) and the clock rate is still fast enough to update the display hundreds of times per second and generate the necessary bleeps accurately in 300-600hz range. I also simplified the design by removing the 74HC595 shift register that was wired between the led display and the MCU. The MCU now drives the seven segment displays directly.

It is pretty easy to forget to turn off the game after playing, so I added an attract mode thingy which flashes the leds and makes some noise if the game sits too long in the main menu. As a last touch I added a reverse voltage protection circuit to protect the electonics in case the batteries are inserted the wrong way around. Typically reverse voltage protection is implemented using a diode in series with the battery, but diodes have a pretty big forward voltage drop, so this would have lowered battery efficiency. A better solution is to use a MOSFET or transistor. After some reach I decided to use a PNP transistor for this task. It was actually quite hard to find a suitable transistor: the required transistor needs to be PNP, have a high beta value, low saturation voltage and max collector emitter voltage of at least 6 volts. After a long search I found the ZXTP2012ASTZ which is just perfect for this task.

To protect the electronics I needed a sturdy enclosure. I have the privilege of knowing Antti Sirén, a master craftsman, who made the enclosure from wood and painted it bright christmasy red. Thanks Antti!

Yay! My first schematic done with Eagle

Inside the box are four AA batteries, main PCB and four arcade buttons with microswitches and LEDs. The 3x 7-segment display is on a separate PCB which is mounted on the lid.

New software revision

I also wanted to improve the software side to make the game more enjoyable. The old version is pretty harsh because the time window when the button needs to be pressed is very short. If you weren't fast enough to press the button before the next led lit up, it was game over. This resulted in unnecessary frustration and it was not sometimes clear what had happened. Sometimes the players felt it was the game's fault, not their own that the game had ended. To fix this, I changed the logic so that as long as you pressed the correct button in sequence, no matter when, it would be ok. So the player could be behind the led sequence as long as he kept pressing the buttons in the correct order. This improves the game a lot. Now the game can only end when the player presses the wrong button and there is no doubt whose fault it was.

Another aspect which has a big influence on gameplay is the speed up curve which makes the game gradually faster as you play. If this curve is wrong the game either speeds up too slow and feels boring, or too fast and it feels too difficult. Also abrupt changes to the speed are easily noticed and distrupt player's flow. Surprisingly a linear speed up curve does not feel natural at all. After many iterations I ended with a piecewise linear curve with 4 parts.

As a result of these changes the game is now a tad easier and a game last a little longer. The game should now be more rewarding to the player. As icing on the cake I added a totally new game mode, the memory game. Check it out in the video below!