Thursday, 8 May 2014

HammerPong #2: Reading Player Inputs

The next step in the build of my Hammer Pong game was to read the user input - when they whack something with a big ol' hammer.. I wanted to give the game some kind of “velocity sensitivity” so that different levels of force would trigger different behaviours in the game (but maybe a very hard hit would not always be to player's advantage - perhaps if the opponent manages to return a fast shot it comes back at ridiculous speed:)

Since I can see this part of the build taking a lot of punishment, I wanted to avoid anything with moving parts that were likely to break with prolonged use. My idea was to use a piezo disk as a contact pickup, and design in some way to detect the force of a hit (tell the difference between different volume levels). For robustness and safety reasons I thought using a foam hammer with a foam block as the target. Burying the piezo disk deep in the block should protect it.

I'd seen the EVA foam blocks that are sold by gym shops as exercise aids and thought they would be good to try, being pre-shaped and finished, tough and quite dense with a small amount of “give” to them.

I had originally anticipated getting some kind of off-the-shelf toy hammers, made out of foam or rubber, but I could not find anything that looks very suitable. Most of them were very soft light foam, too small, or inflatable, so I decided to try and make my own using EVA foam blocks. For the hammer handle I bought some fibreglass broom handles (thinking they would be lighter and safer than wood, but still hold up to some abuse).

I still need to give the construction of the hammers some thought, but for my tests I used an EVA foam “yoga block” as the head and a length of broom handle. I used a standard holesaw, intended for timber, to bore a hole into the side of the block (I fully expected it to rip the foam to pieces, but it actually cut the cylindrical hole perfectly!). Again still experimenting I used generous amounts of cyanoacrylate superglue (set off with activator spray) to join the handle to the head. Superglue is brittle, so I thought it might crack with use, but I tried very hard to break it by hammering things or shear it by twisting the handle... suprisingly the glue join held up to all the abuse I gave it so there is definite potential.
Hammer Prototype

I wrapped the piezo in duct tape and buried it in the middle of an EVA foam cylinder. The only downside is that the “slap” of the two foam surfaces hitting each other is very loud.. maybe too loud. I might experiment with covering the target with fabric later on..
Foam Cylinder With Piezo

So on to the electronics... I know from experience that a piezo disk can throw out a pretty high voltage spike if you hit it hard. Since I wanted to be able to get a measure of how hard the target was hit, I had to use an analog measurement of the piezo output voltage. But, now the microcontroller I am using runs at 5V power, any input voltage higher than 5V would be clipped at 5V on my analog input (I could use a higher analog reference voltage, but lets keep it simple) reducing the range of input levels I can tell apart so middling hits and hard hits would be indistinguishable.

So, I need some kind of attenuation (scaling down) of the input voltage level, for which I used a simple 10k trim pot. I also thought some low-pass filtering would help separate the main impact impulse from other noise. Something else I noticed with an oscilloscope was that the negative voltage peak coming from the struck piezo was substantially higher than the preceding positive peak (not sure why that should be..). 

Anyway, all of the factors led me to using an op-amp to make an active lowpass filter (which also inverts and buffers the input). The trimpot gives a variable attenuation of the raw piezo output to bring it within the range of the opamp input. The opamp output can then be fed directly to an analog input pin on the microcontroller where it can be measured.



While I am planning to use an ATMega328 for the main processor on this project, I decided to offload the input monitoring to a second MCU, I think this is particularly important given the main processor could be blocked for a short time updating the LED strips and might possibly miss input pulses. I used a PIC16F1825 for my little helper as I am familiar with this chip and have a ton of them.

Circuit On Stripboard
My PIC code monitors two analog inputs (one for each hammer target) in parallel. When an input reading exceeds a trigger threshold, the PIC samples the input over a short period, storing the maximum value (the idea being to get the peak value of the trigger pulse). This information is then transmitted as a serial message and the PIC waits holds off for period of time to allow the sound to fade before going back to listening. See the code here https://github.com/hotchk155/HammerPong/blob/master/InputListener/InputListener.c

I used MIDI for the serial protocol. Some people might say I use MIDI for everything, but I love it for a lot of sensible reasons :)

- It is very simple to implement, with most messages being only 3 bytes long
- It has a nice easy synchronisation scheme (bit 7 set means start of new message - simple as..) that stops your sender and receiver getting out of step
- It is perfect for this kind of thing (transmitting simple “note” events with a velocity) - it is pretty much what it is designed for.
- The best bit... if you have some MIDI gear and utilities then you already have all the test tools you need! I can test my input reader before I have the main game program written by simply playing notes through a synthesizer. When I test the game I can fake input using a MIDI keyboard. And I already have a bunch of MIDI cables I can used to connect things together.

Next to get some big flashing lights working!

No comments:

Post a Comment