standard way to communicate with hardware (arduino)

Hi all,

Being able to easily read data from sensors or input from buttons/switches/sliders, together with having the ability to control hardware like motors/lights is in my opinion an important step for Unity. This makes it possible for Unity to be used to make for example interactive installations or your own custom input devices like joysticks. A nice example of an installation made together with Unity can be seen here:

http://forum.unity3d.com/viewtopic.php?t=28892&highlight=arduino

Probably the easiest, cheapest and most popular way right now to interface with hardware is the arduino platform ( www.arduino.cc ). There is already a possibility for Unity to talk to this platform by using myu ( http://forum.unity3d.com/viewtopic.php?t=19567&postdays=0&postorder=asc&start=0 ), but this requires Max/MSP, which costs around $699.00. For many artists/students this might be too expensive, and do you really need an external program to do this? So my question is, is there a easier/cheaper way to communicate with Arduino? I have the impression there are already quite some people/institutes who made there own connections, it would be great if they could share their code/setup, so we could make a “standard” way to connect to Arduino, and put it on the wiki.

Preferably it shouldn’t be limited to Unity3d Pro (again, to make it more accessible for artists/students), or there could be two versions. And it would be nice to have it working on both Windows and OSX.
I am not a great programmer myself, so I am afraid I can not really contribute to this project, besides trying to set it in motion… Hope you can help!

Wim

You are right that it would be nice to have a built in possibility to connect to Arduinos and such. But: We just did such a connection to an Arduino all by ourselves and it was not very hard to do. Though I have to admit that it is a custm plug-in and therefore needs Unity Pro to run. But again: If you have Unity Pro available and are able to throw in a few C# lines, everything will be running just fine.

Though I have to admit that Arduinos aren’t the best value for money. They’re incredibly slow. Ok, they give you the ability to program them by using a higher language. But you pay this with a considerable speed decrease compared with micro controllers programmed by using assembler. And yes, I know how hard assembler is, I hate it myself. But even the Arduino Mega is not able to handle software PWM for 120 LEDs plus some minimalistic serial communication.

Hi Whimsical,

Thanks for your reply. As with many things, it’s easy when you know how to do it… I started this thread to try to accomplish a ‘standard’ way to connect Unity with Arduino, so it would also be available for people who are not great at programming. Would you mind sharing you code here?

I know Arduino might not be the solution for every project, but in general I think it’s the easiest to use solution. And since it became so popular there is a huge community behind it, resulting in many forums/tutorials/books to help you using it.

Wim

I’m sure it’s slower, but like you I am no programmer but am able to get the Arduino up and running with little effort.

I recently did an installation using Unity on a MacMini, hooked to an Arduino and also using Twitter feeds to populate some textures. The Arduino used a PING Ultrasonic sensor to read proximity data and I used that to operate a ‘Player’ object in Unity. The player roughly mimicked the person standing in front of the projection and their movement would cause a collision with objects in the scene.

My approach was pretty low-tech. I tried the Max/MSP technique, but found it buggy (like Max!). So, I wrote a program in Processing to read in the sensor data and then write that to a text file. It’s very similar to the technique I read in this forum for writing Wii information to a text file. Bluetooth from the Wii I also found to be buggy.

This worked surprisingly well. My Unity game would run for 5 hours before crashing, and then I would have it automatically restart. Some kind of memory leak, probably related to the Twitter feed…

Anyway, here is javascript code I attached to the Player object in Unity:

import System; 
import System.IO; 

var minimumHeight : float;
var delaySeconds = 2.0;
var posvalues : Array; 

function Start (){
	
	InvokeRepeating("mover", .1, .5);
}

function mover () { 
	var poscoordinates = (Application.dataPath + "/positions.txt"); 
  //var poscoordinates = (Application.dataPath + "/Scripts/" + "positions.txt"); 
  //var poscoordinates = "/positions.txt";
    
 if (File.Exists(poscoordinates)){    
      var sr = new StreamReader(poscoordinates); 
      var txt = sr.ReadToEnd(); 
      posvalues = txt.ToString().Split("\n"[0]);
   } 
   
   zPosition = float.Parse(posvalues[0]);
   
   move(zPosition);
   
   
    //Debug.Log(values); 
}

 function move(zpositionTarget : float) 
{
   for (var n: int = 0 ; n < 1 ; n++) 
   { 
    
      // Lerp to the selected target position in 2 seconds 
      var fStartTime: float = Time.time; 
      var fCurrLerp: float = (Time.time - fStartTime) / delaySeconds;
      while (fCurrLerp <= 1.0) 
      { 
        fCurrLerp = (Time.time - fStartTime) / delaySeconds; 
   		transform.localPosition.z = Mathf.Lerp(transform.localPosition.z, zpositionTarget, fCurrLerp);   
    	yield; 
      } 
   } 
}

And here is the Processing code. Note that I am writing the text file into the Contents folder of the Unity application. Makes it convenient since I need both applications running at the same time.

BTW, I did not write most of the Processing code, that is coming from the Arduino.cc forums.

// import the serial libary for Processing
import processing.serial.*; 

// define a new port object
Serial port;


PrintWriter output;
PrintWriter outputPos;


void setup(){
  size(48,48);
  port = new Serial(this, Serial.list()[1], 9600);
}

void draw(){

    // delay 100 milliseconds - this must be the same delay used in the Arduino sketch.
  delay(100);
  getSerialData(); 

}

void getSerialData(){


  // create a string to store the value from reading the serial port
  String serialBuffer = port.readString();

  // check if the value is not null, that the value is greater that 2 and no more than 4 characters
  if (serialBuffer != null  serialBuffer.length() >= 2  serialBuffer.length() <= 4) {

    // check that the first character in the string is A
    // we add A to our output to cast the serial value to a string type.
    // Its also handy as the A is doubled up as a marker for a new value in the serial log
    if (serialBuffer.indexOf("A") == 0) {    
      // if this is true then remove A
      serialBuffer =  serialBuffer.substring(1,serialBuffer.length());
    }
    // double check to make sure there are no more A's in our string - otherwise we can't convert to a number
    if (serialBuffer.indexOf("A") == -1) {
      // convert the string to an integer
      int i = Integer.parseInt(serialBuffer);
      //map to our input range
      float m = map(i, 0, 150, 0, 40);
      // print our integer
      //println(m);
      //Code below writes incoming numbers to a file
      outputPos = createWriter(sketchPath("Uhypocenter.app/Contents/positions.txt"));
      //outputPos = createWriter(sketchPath("Assets/positions.txt"));
      //outputPos = createWriter("positions.txt");
      outputPos.println(m);
      outputPos.flush();
      outputPos.close(); // Finishes the file
    }
  }

}

I’m having trouble implementing your scripts above. They both are working fine separately but when ever I try and run processing and Unity at the same time processing gives me and error.
“Couldn’t create a writer for C:\blah blah blah” then in says “(The process cannot access the file because it is being used by another process)”

How do you allow both of these scripts to access the same text file at the same time without having a conflict?

Thanks your help is much appreciated.

It must be a mac thing, I did not run into any problems having it read/write at the same time. Make sure your path to the text file is correct, that’s the hardest thing to get right.

Hi,
It’s also possible to sent and receive between unity and arduino using a serial connection.
check out this open source project:

http://code.google.com/p/unity-arduino-serial-connection/

It allows windows PC’s to use a direct connection, and a mac implementation with serialproxy is on the way.
I also listed it here:
http://forum.unity3d.com/viewtopic.php?t=51433

I might be oversimplifying, but a good way of going about this would be to interface Arduino or any other microcontroller with Processing or OpenFrameworks, then sending the serial data to Unity via OSC. I’ve used this method before and it’s surprisingly responsive.