Space: The Game

December 18, 2017

I wanted to build a desktop arcade cabinet that will use an Arduino Leonardo to control an iPad game.  The Arduino will connect to the iPad via Bluetooth and control the game using simulated keyboard inputs.  The enclosure will be laser cut out of MDF and resemble a classic arcade cabinet with a joystick and 2 large concave buttons.

When trying to figure out how I would get an Arduino to control an iOS app, I had trouble finding any sort of resources or documentation on how this is done.  I then came across the idea of programming the game to respond to keyboard inputs because iPads can connect to bluetooth keyboards.  From there, I could get the Arduino to simulate a bluetooth keyboard with each of the buttons and joystick sending a specific ASCII key.  I chose the Arduino Leonardo because it could simulate a keyboard.

Arduino as a Keyboard

The first thing I did was try to hook up the Arduino and BLE Shield.  This would be the main part of the project because if it didn’t work, my idea wouldn’t pan out like I intended.  When I first plugged in my Arduino, I had a lot of  trouble uploading code.  Even just the bare minimum amount of code wouldn’t upload.  The IDE couldn’t find the port.  After several attempts and a lot of research, I decided to try a different micro usb cable.  This thankfully solved the problem and I threw the other one across my room.

https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537219792125-LT2J7G44PIJPX60KT25C/image-asset.jpeg?format=1000w
https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537219800923-JRSSZ45NBF74QBKYQ2KI/2.jpg?format=1000w

To get the Arduino sending keyboard inputs, I included the “Keyboard” library.  Keyboard.begin() initializes it similarly to Serial.begin() in the setup function.  Sending text is as simple as Keyboard.print(“Hello”).

Note to self: Don’t ever, and I mean EVER just test the keyboard functionality using Keyboard.print(“Hello”) in the loop function because it will be nearly impossible to do anything.  It will just constantly type hello wherever you are on your computer.  I tried to delete that code and reupload it so it would stop but it would keep typing as it was compiling and uploading and mess it up.  Literally chaos.

https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537219832879-41DR6WDWDQ0Q2ZNSQBJ4/3.jpg?format=1000w
https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537219840845-TL4PA0WSR6W2QP89X536/4.jpg?format=1000w

The second thing I did was try to connect the Arduino to my phone via Bluetooth.  I bought the 1Sheeld+ because I heard it was a good, versatile BLE shield.  After playing around with it and trying out some sample code, I got it to connect to my phone and control some LEDs with a slider in the 1Sheeld app.  This was great and all but I couldn’t find any information on sending data from the Arduino to the phone.  There are thousands of websites, videos, and tutorials on doing it the other way but thats not what I needed.  After a lot of research, I decided the 1Sheeld+ wasn’t the part that I needed so I returned it and ordered a Bluefruit LE Shield by Adafruit.

https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537219896068-V099RXK8MY1IEYJKVAYG/9.jpg?format=300w
https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537219896063-H6CGCX3X01FFHM6LGE3Y/8.jpg?format=300w
https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537219896724-26L2D4RBGZ86Y8G4QN8C/7.jpg?format=300w
https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537219896771-81MMW6ILU0YUSFSTS3EW/6.jpg?format=300w
https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537219897448-FY83N314SY00UJVL47IJ/5.jpg?format=300w

The Bluefruit turned out to be pretty much perfect for this project.  I soldered in the header pins so it could sit on top of my Arduino and loaded the sample HID Keyboard sketch.  Right away, it worked really well with my iPad.  It connected via bluetooth like a normal bluetooth device rather than through an app like the 1Sheeld+.  The example sketch took in user inputs in the serial monitor, and sends them to the iPad over bluetooth.  Below is a quick gif of it in action.

https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537219944597-ZJF7CL4O3TKRSX2J1NYD/image-asset.gif?format=500w

Now that I knew this worked, all I had to do was modify the sketch so that I could send specific characters with the press of a button.  This turned out to be both easy and difficult at the same time.  The solution to the problem is pretty simple.  Setup your buttons like normal with input pins and get the button state with digitalRead.  An if statement will make sure that you only send data to the iPad when the button is pressed.  Seems simple right?  Well it took me a long time to figure out.  Maybe its cause I haven’t slept in like 40 hours or something but it just wasn’t working and I couldn’t figure out why.

After trying a lot of different variations of the code and wiring, I added in an if statement that was in the example code and it worked.  This if statement didn’t really do anything other than print to the console but it made the button work as intended.

https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537220022618-QOVYLFDGYX2JC1963493/11.png?format=500w

Here is a quick video showing the button typing the letter ‘A’ on my iPad.

Next on the list was to try it with all of the buttons that I will be using and make sure it all works as it should.  I added in some more buttonState integers and if statements that send different characters.  I wired up the arcade buttons and it all worked perfectly.

Enclosure

I want this project to resemble a classic arcade cabinet so my enclosure had to be a mini version of one.  I started out by sketching what it would look like and getting all of the dimensions that I needed.  From there, I built my model in Fusion 360.

https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537220099990-LS7CD4E09UPNLN38JB9H/image-asset.jpeg?format=1000w

My first sketch was the front face.  Here, I decided on the dimensions of the box itself so it would be proportioned decently while still having a good place to put the iPad.

https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537220130895-FB5QWBMKLSNE2AMDZFZS/image-asset.png?format=1000w

The second sketch was from the side where I created the back part of the box.

https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537220145204-B46LX0HEF6TR4RAVGH2C/image-asset.png?format=1000w

Once each piece was extruded to the desired dimension, it took on a form that I was pretty happy with.

https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537220165192-OTOVZNSBI7D6YY2GOCYI/image-asset.png?format=1000w

After creating this model, I got a few tips from Toby on how to turn it into a 2D sketch for laser cutting.  He said that he usually extrudes each face separately and moves them together into the model.  It took a lot longer but will be easier to laser cut.  I was also able to add in some tabs that will fit together when I assemble it.

https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537220194128-XFIOUVMOZ3IL38FZ3QCV/image-asset.png?format=1000w

Once I finished the model using this new method, I played around with animations in Fusion and created this little explosion gif.

https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537220218161-UJDIJ52IUXVOPMYC3B6C/image-asset.gif?format=500w

After this, I went back in and created a bunch of new sketches to add interlocking tabs so it would be more secure and easier to glue together once it was all laser cut.

https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537220259461-KWC0C1BXQBKNIF2HL9HO/21.png?format=300w
https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537220259343-20YUO5BPJ21NWO9T9ZLX/20.png?format=300w
https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537220259815-CX2E5SA66XQAJ6563SY4/19.png?format=300w
https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537220259912-ECLYGPL3HHQHHI7U35Y7/18.png?format=300w

Like any classic arcade cabinet, it had to have a cool design.  I chose to make a space game because I could find resources online for it pretty easily.  My sister came up with the name “Space: The Game” so that’s what its called.  I designed a simple vector rocket to be used on the side of the cabinet as well as in the game.

https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537220294573-ES9HQBTNQT8U84P91VJK/22.png?format=300w

The sides of the cabinet were the most fun part of the enclosure.  I got to mess around in Illustrator and come up with some cool designs.  These are the designs that I ended up using in the format that I sent to the laser cutter.

https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537220320201-ZZNGYFAZT6J1PGWS9FW1/image-asset.png?format=1000w

Getting materials and laser cutting was not as smooth as I had planned.  I had built my entire enclosure in Fusion with the intent of having 3mm thick MDF.  I went to Home Depot to buy my materials only to find they didn’t have anything in metric, nor anything close to 3mm.  I ended up getting 1/4″ MDF and had it cut down to two 32″x18″ pieces to fit in the laser cutter.  When I got home, I had to go back through my model and change all of the thicknesses to 6.35mm (1/4″) and make sure all of my joints were adjusted accordingly.  It was kind of a hassle but necessary to have a good looking final product.

In order to take that 3D model and translate it to 2D for laser cutting, I had to take multiple steps that I kind of made up along the way.  First, I copied every body, or piece, in Fusion to a new component.  From there,  I made components out of each body so I could easily move them around.  I rotated them so they were all flat on one plane and laid them out so they wouldn’t overlap.  I took all of these components and made a drawing of it in 1:1 scale and exported as PDF.

https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537220347748-FDM7B5TSLC00Z7Y75DY6/24.png?format=1000w
https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537220354339-5GTGJEU39E3E9GZAQ22G/image-asset.png?format=1000w

In Illustrator, the first time I created files to laser cut, I knew I needed to make each line thin and red so it would be cut.  I couldn’t easily modify the pdf so I just used the pen tool and the rectangle tool to trace each shape.  The second time around, when I had my adjusted thicknesses, I was able to isolate each shape by clicking on them a bunch of times and copy them to another file where I could move them around.  To make this enclosure, I needed pretty much all of 2 32″x18″ sheets of MDF.  As you can see in the pictures below, there was not a lot of space around each piece.

https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537220403374-LVYRMBISNUDYKE55KXS4/image-asset.png?format=1000w
https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537220410455-C2ZQ5WMGVG1TBOA6EJZB/image-asset.png?format=1000w

Laser cutting was kind of a pain.  Like the last time I had to get something laser cut, my files weren’t formatted correctly.  The lines weren’t the right red, even though I specified 255,0,0 and the line widths were wrong.  On the file with the designs, each design showed up as an outline to be cut instead of etching.  Huge thank you to Toby for helping me out and getting the files fixed in Rhino to be laser cut.  The file with the designs took over an hour to finish, mostly because of the etching.  It came out perfect though.  Each piece fit together exactly how I wanted and it looked really cool.  Here it is mocked up and standing without any glue yet.

https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537220439676-PGRO6TCBNA1LDIQIVBDI/image-asset.jpeg?format=750w

As well as it fit together, it still needed glue to be sturdy and less fragile.  I used liquid nails to keep everything together and had it in some clamps for a long time.

https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537220493919-UQR3MLRC8HSP1K1WRVMY/33.jpg?format=500w
https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537220493967-BCERX5CXTU9G71WFRFG7/32.jpg?format=500w
https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537220494738-7UCVJY2CK66B4KUKZ83A/31.jpg?format=300w
https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537220494640-N7M6XK96INOJWKANTVRG/30.jpg?format=300w
https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537220495184-SWC8W9JGT3XKWTG3O23Y/29.jpg?format=500w

Adding the joystick and buttons was really easy.  The buttons have a nut on the bottom that screws off so you can slide the button into the hole.  The nut screws back on to basically sandwich the board.  The joystick handle screws off but to attach the joystick without having visible screws, I cut another, smaller piece with the same hole cut out to sit right below the top surface of the arcade cabinet.  I could then screw into this instead and attach that to the top with liquid nails.  The final product turned out great and reminds me a lot of actual arcade games.

https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537220552039-X7JOMAX63D7P2580KR4F/36.jpg?format=500w
https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537220552177-QWY31QFWQAF3G6HISKLW/35.jpg?format=300w
https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537220553029-68YHP884A6BQR2X5IK0T/34.jpg?format=500w

The details on the sides are lighter than I expected but came out awesome.  The depth from laser etching really adds to the feel of the enclosure as a whole.

https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537220597986-RYFMUZU3SQYGUPCDBCTQ/39.jpg?format=500w
https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537220598165-59L88MMG8D4U4J17FYBW/38.jpg?format=300w
https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537220599779-SH3ZZNGMISMOD53FVJLF/37.jpg?format=300w

Wiring

The wiring was pretty straight forward considering I only had 2 buttons and a joystick.  I couldn’t find anything helpful about the buttons online so I just messed around with wiring them up until it worked.  I figured out this was the way it should be wired up, with ground going to both the input pin as well as ground with a pull down resistor.

https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537221568806-TI82G47EF7615YJL2VV1/40.jpg?format=1000w

Through more experimenting, I found out that I could instead put ground on the top pin, 5V in the middle, and the wire to the input pin where the ground is in the image above.  This would help out a lot with soldering because I wouldn’t have to connect two wires to one pin.

https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537220661099-KZA8OLEECD2XZSGDQGAG/41.jpg?format=1000w

The joystick was pretty easy to figure out as well.  Each button only has two pins, one for power and the other for the input pin and ground.  I played around with it and printed data in the Serial console so I knew which button responded to which direction you move the joystick.  Here is the diagram for how each button is laid out.

https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537221596469-6MLAZMCAE1221VOL81BL/42.jpg?format=1000w

When I was first starting on the wiring,  I thought I’d need a separate pull down resistor and wire for ground for each button so I put them on a protoboard in this nice, organized fashion.

https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537220711842-C421GKZ76LKTBU01BKWE/43.jpg?format=1000w

This turned out to be kind of a waste of time because, during testing I realized that all of the ground wires and all of the 5V wires could be connected in series.  I still like how it looks though and I still used it.

To make the wiring a bit simpler when plugging into the Arduino, I soldered the ground and 5V wires of one button to the next until I got to the last one.  These wires would then connect to the Arduino.  For my game, I only wanted the user to be able to move sideways so I only soldered wires to 2 of the 4 buttons on the joystick.  This meant that I would have 4 total inputs.  I found some female header pins that I soldered to the BLE Shield so it would be easy to plug in and remove.

https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537220742377-TFF30AUAA1MI8V31PKGA/44.jpg?format=1000w

Making the Game

The game wasn’t the focus of this project.  I mainly chose this project because I wanted to make a really cool enclosure and learn to interface an Arduino with an iPad.  Because I am really good at procrastinating and got a late start to this project, I followed a tutorial by Hacking with Swift to create my space game.  I changed some things around once I was finished though.  I changed the orientation to work in portrait and changed some of the sprites with my own designs.  The main thing that I worked on was trying to get it to respond to keyboard inputs.  I connected my bluetooth keyboard to my iPad and was able to receive keyboard data.  However, this data and these functions only worked in the class GameViewController.swift but I needed them in GameScene.swift.  I tried a lot of different things but could never get it to work the way I wanted.

Code

All of the files for the game can be found at my Github repository here.

The Final Product

I was going to use the white seamless background but my enclosure was too big.

https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537220827899-96UWWRK9M9TQV5QSWD3H/45.jpg?format=1500w
https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537220824721-HSG5N4UWMSL19KZUZCDY/49.jpg?format=1500w
https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537220826403-V2DWS95SYG6S08ZF2NNP/46.jpg?format=1500w
https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537220823310-E1DGLC3P21WQ0QAJHRB1/51.jpg?format=1500w
https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537220823407-SNI3DMTTH8IMFKT8QU69/50.jpg?format=1500w
https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537220824989-WT91W4DZD86POTECIF63/48.jpg?format=1500w
https://images.squarespace-cdn.com/content/v1/5a61474ef09ca4cd8215fe42/1537220825604-5SWOZ5BPIL7DL2QBRRXR/47.jpg?format=1500w

Conclusion

I am happy with how this project turned out but like any project, it could be better.  Had I not procrastinated so hard I might have been able to get it working or make my game a little cooler.  I clearly didn’t learn my lesson from the first the first project because I was up fr 40+ hours working on it the day before it was due.  I love the way my enclosure looks.  To me that was the most important part because it wouldn’t feel like an arcade game if it didn’t look like one.  I think this project gave me a really good starting point for future projects.  I will still work on it occasionally to get it working as it was supposed to and from there, I can build upon the game.  I learned a lot doing this project and that feeling of closing 20 tabs once you’re finished never gets old.