As a final project in a course in computer technology, we combined Unity with the Arduino platform (arduino.cc/) to control a classic game with a labyrinth and a ball using an accelerometer.
Based on the movement of our control, the labyrinth moves in the game. The goal is simply to get the ball to the end of the labyrinth by tilting the platform. If the ball falls into one of the holes – the game restarts.
Here is a video demonstration: http://vimeo.com/12288710
We got a lot of help from this forum (especially from here: http://forum.unity3d.com/viewtopic.php?p=294657#294657 ) so we wanted to give something back to you.
Credits: Andreas Huck, Jens Börjesson ( http://www.jensborjesson.se ) Alexander Gryth ( http://agryth.com/ )
Arduino code:
About: This code picks up the values the Arduino card gets from the accelerometer and transforms them into readable data. Gets the values from the x- and y-axis up to 90° and -90°. It is also able to read the z-axis to set jump to true or false.
/*************************************************************************
CREDITS:
Andreas Huck, Jens Börjesson Alexander Gryth
*************************************************************************/
/*
Hardware needed: Arduino card and an accelerometer.
This code picks up the values the Arduino card gets from the accelerometer and transforms them into readable data
*/
// variables to the analog ports that are used on the arduino card
int X = 0;
int Y = 1;
int Z = 2;
int calX = 18;
int calY = 14;
int zaxis; // variable that checks the Z-axis value. This is to determine if there is a jump or not
void setup()
{
Serial.begin(9600); // start serial communication
}
void loop(){
Serial.flush(); // Empty the memory each time in the loop
// The following adjusts the x- and y-values and prints them. These are selected by tweaking and comparing the values with the movement of the arduino card. You may need to change these to suit your accelerometer.
Serial.print(map(analogRead(X),278,417,-90,90)+calX);
Serial.print(",");
Serial.print(map(analogRead(Y),278,417,-90,90)+calY);
Serial.print(",");
zaxis = map(analogRead(Z),270,402,0,1000); // adjusts the z-value
// if the z-axis indicates a high value – there is a "jump". This results in a boolean (1 or 0)
if(zaxis < 800 || zaxis > 1200){Serial.print(1);}
else {Serial.print(0);}
Serial.println();
delay(10); // delay for each turn in the loop.
}
C# code:
About: C# code that connects Unity with the Arduino card and moves the in-game platform based on the movement of the card.
The Arduino card is also programmed to understand when the user jumps with the control. This is though not implemented fully in this version. Instead, the text “Jump” is written on the screen to demonstrate that it works.
/*************************************************************************
CREDITS:
Andreas Huck, Jens Börjesson Alexander Gryth
*************************************************************************/
/*
C# code that connects Unity with the Arduino card and moves the in-game platform based on the movement of the card.
The Arduino card is also programmed to understand when the user jumps with the control. This is though not implemented fully in this version. Instead, the text "Jump" is written on the screen to demonstrate that it works.
*/
using UnityEngine;
using System.Collections;
using System.IO.Ports;
public class styrScript : MonoBehaviour {
public static string jump; // String variable that displays on the screen
SerialPort stream = new SerialPort("COM4", 9600); // Defines the serial port and the speed
void Start () {
stream.Open(); // Opens the serial port
}
void Update () {
string value = stream.ReadLine(); // Reads the data from the arduino card
string[] vec3 = value.Split(','); // Splits the data from the arduino card so that we have values to work with
// Rotation. Makes the platform moves smoothly towards a target (the value from the arduino card)
target = Quaternion.Euler(float.Parse(vec3[1]),0,float.Parse(vec3[0]));
transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * 2.0f);
if(float.Parse(vec3[2]) == 1){// Checks if the z-value is 1 (true) and then writes "Jump" on the screen
print("Jump");
jump = "Jump";
}
else if(float.Parse(vec3[2]) == 0) { // If the z-value is 0 (false), nothing happens on the screen
jump = "";
}
}
void OnGUI()
{
GUI.Label(new Rect(170, 570, 60, 20), jump); // Creates the placeholder on the screen for the text when "jumping"
}
}