Introduction: Ultrasonic Joystick
Using an Arduino I wanted to make a construction to control the game Snake in an unusual way, using ultrasonic distance sensors. This is made for a school project called If This Than That at the University of the Arts Utrecht
After lots of experimentation, this is the end result.
There still are some bugs when controlling Snake.
(Dutch log can be found here)
- Arduino Uno (any Arduino would theoretically work)
- 2 ultrasonic distance sensors (HC-SR04)
- 8 cables to connect the distance sensors to the Arduino. Preferably male to female cables
- An enclosure at least 300mmx300mmx40mm. (width and height are easily adjustable. Depth is 40mm because you need room for the components inside)
To make this permanent, you need:
- Soldering iron
- (hot) glue gun (or mounts for the distance sensors)
- Male to male headers to plug into the Arduino
- 8 cables to solder the ultrasonic distance sensors to the male headers.
Step 1: Circuit
The circuit is really simple.
- You need to connect ground to the ground pins of the ultrasonic distance sensors.
- The 5V (I also use Vin, because I use USB power) goes on the Vcc pins on the Arduino.
- The Trig pins go onto D8 (Arduino Digital pin 8) for 1 sensor and D11 for the other sensor
- The Echo pins go onto D9 for 1 sensor and D12 for the other
For experimentation, it's easy to use male to female wires.
To make a permanent solution it's best to solder wires to the ultrasonic sensors and male to male headers. After this, you can put the male to male header in the Arduino to make it work.
Step 2: Arduino Code
There will be 2 parts of code for this project.
1. Getting the distance using the NewPing.h library and pushing it to the Serial.
2. Making sure the Serial is formatted in a way Unity can read it properly
You can see the code, with proper commenting here:
Step 3: Unity Code
I made snake in unity. There are some Open Source resources I used.
YouTube tutorials on how to make Snake in Unity by Code Monkey
WRMHL to handle the reading from Serial inside of unity.
With all the sources stated, the Unity project file can be downloaded here:
I make sprites out of pictures of the top of people and food. This is because I did not want to use the assets of other people.
Step 4: Making the Enclosure
This step is really up to you how you want to make it.
You can very easily laser-cut the box if you have the resources.
I just got a big piece of wood and saw it in the desired sizes.
- Underside: 450x450mm
- Upside: 450x450mm, with a cross cutout. This is centered, 60mm from the sides and has a width of 20mm
- Sides: 2x 450x50mm and 2x 420x50mm (This is because the wood would overlap otherwise)
It's smart to make a cutout in 1 side where you can pull through the Arduino USB port/cable for easy connectivity.
- Stick: 15mm diameter (make sure this is less than the width of the cross cutout
I simply put it together with nails. The top has been made removable by screwing in 90 degrees fittings on the bottom, so you can pull it up vertically, but you can't move it while playing.
I screwed in the Arduino so it won't move around.
The distance sensors are glued in with hot glue. The cables are also managed using hot glue.
I painted all plain sides white and painted 4 arrows on the top to make it more comfortable to use.