söndag 31 maj 2015

Arduino to Unity Project Part 2: Testing, testing



To start off my project I wanted to do a quick and dirty session with the goal to set up a connection between the Arduino IDE and Unity. For this session I had the following equipment:


Hardware:

1 An Arduino Uno microprocessor
2. A Breadboard and Cables
3. a 3-Pin Button
4. a LED-lamp

Software:
1. Arduino IDE
2. Unity 4


Arduino to Serial:

I started by setting up a simple Arduino sketch for using the 3-pin button as a sensor to turn the LED-lamp OFF/ON. I used an example sketch available in the Arduino IDE called Button. There is a tutorial for it here: http://www.arduino.cc/en/Tutorial/Button


 For the wiring a simply used the schematics provided from the retailer where I bought the button:



I also printed the "1" if the button was pressed and "0" if it was open.

This was the result:


Arduino to Unity:

For this part I followed this tutorial: https://www.youtube.com/watch?v=of_oLAvWfSI
I altered the code to write to the serial port. Same as before (1 for button pressed and 0 for button open). I then opened Unity and wrote a C#-script which 

1. Opens a connection to the indicated serialport and reads 1 byte from it.
2. If the byte is 1 the script calls a Translate function and tells it to move to the left. How much it moves depends on the time of the loop. (Everything in both Arduino and Unity-scripts works in a big loop which runs one time per frame).

And then comes the amazing part:
Once you have done the script you just drag it onto  the Unity-asset you want it to control! This might be old news for someone with some experience with Untiy. However, for me this was a very pleasent surprise.

Now I could do this:

Testing Unity Controls

Next I tried to alter the Unity script to do other thing than moving the cube:





Seems to work pretty good!
Now I need more buttons!
















Inga kommentarer:

Skicka en kommentar