For my kids workshops i always try to come up with new Arduino games, and they really liked the recent Gyro game.  I decided to make a laser cut box for it, and add a second game, Pong for 2 players, with 2 potentiometers sticking out at the sides. It has been a very popular toy, with the entire family trying to break the Gyro record (catching as many squares as possible in 60 seconds by tilting the box to move the green ball, avoiding the borders). Below picture shows the end of the Pong game (score after 5 balls).

img_20161217_115320183

You can build this game with any Arduino; i include the code below. The red restart button pulls the Reset pin to GND so the MCU restarts for a new game. The switch has 3 positions: middle is power OFF; up/down chooses between Pong and Gyro via a digital pin with pinMode INPUT_PULLUP (2×3 switch, see below). The LED is not really necessary, it just lights up when you hit the ball in the Pong game and when you catch a target in the Gyro game. There is a buzzer inside the box. Below 2 pictures show the Gyro game: the top number is the score, the bottom number the remaining seconds. The start screen shows the high score (stored in EEPROM, so available after reset).

img_20161217_115058565 img_20161217_115132825

IMG_0325

For the first version i used the same 0.96” OLED that i always use, but clearly that is too small to enjoy the games. In the final version i replaced this with a 1.8” TFT colour LCD using a similar SPI connection plus one pin to power its LED backlight (5V via a 220Ω resistor).

img_20161129_124340976 img_20161129_124313879

I soldered an Atmega328 on a perf board with a 16MHz crystal so it can be programmed as a standard Arduino Duemilanove (see barebones post). You can use any Arduino board to build this.

img_20161217_100221173

For the final version i moved the buzzer to the perf board (as in above picture); the sound is still loud enough when the box is closed.

This simple schematics shows the Atmega328, with 16MHz crystal and 22pF capacitors, and the restart button on the RST pin (to GND). The screen is connected over SPI, and the MPU6050 (gyro/accelerometer) is connected over I2C on A4/A5 (on Arduino Uno/Duemilanove).

img_20161202_105727067

I wanted to power this box with 2x AAA batteries and the MPU6050 gyro/acc module GY-512 is said to need as little as 3V, but to get the most out of the batteries i added a USB booster that delivers a constant 5V even when the batteries go below 3V. The booster’s position is not correct in the schematics (see below): i put it after the switch so that it is only powered when the switch is ON. The minus of the battery goes straight to the booster, but the positive goes to the switch first (see above picture). And I forgot to show a connection between the GND on the upper side and the lower side in the schematics. The TFT LCD also needs a power line for its LED backlight, i put 5V on it with a 220Ω resistor.

IMG_2086

img_20161217_100252879

It’s a bit of trial and error to get the potentiometers connected the right way (swap 5V and GND), and after making sure the Pong paddles were in the middle, i glued a laser cut knob on them as below. The code only uses about 90 degrees of the range of the potentiometers to make it easier to move the paddles.

img_20161217_115355390

For the first version with the OLED i used the excellent U8G display library (see original Gyro game). This is very fast, but a bit complicated to code. Unfortunately it is not compatible with the TFT colour LCD, so i had to re-write the code and decided to use the standard Arduino TFT library that comes with the IDE. I noticed this is built on top of the Adafruit GFX library, so it’s probably faster to use that directly, but the speed i got with the TFT library was acceptable. The main difference with the U8G library is that U8G requires to redraw (in code) the entire screen in every loop, without causing a blinking effect, while the TFT library blinks too much if we erase the entire screen every loop. We have to erase the elements that change. In our case, for Pong this is the paddle and ball positions, and for Gyro the ball position. The TFT library speed seems acceptable as long as you don’t use the TFTscreen.stroke() function; for some reason that slows the display down significantly.

I’ve put the code on pastebin; suggestions for improvements certainly welcome. You will need to include 2 files in your sketch folder I2C and Kalman.h, they come with this MPU-6050 example.

I have created a PCB for this project as in below picture, which makes assembly a lot easier. The Atmega328 needs to be programmed externally.

IMG_0262

IMG_0312

IMG_0320

 

Leave a Reply

Your email address will not be published. Required fields are marked *