Arduino to Unity 3D Communication

Thread Starter

Michael Knight

Joined Nov 5, 2015
90
I'm working on some interaction between Unity 3D and Arduino. I'm wondering what might be the best way to tackle something like this:
Lets say I want to have various sensors like an LDR send data into Unity 3D and have that data trigger something in Unity 3D when it reaches a certain value but not have it "Buggy" for lack of a better word. Like activate all the time for crazy reasons.
Basically I would like to trigger some random voice response clips if say the ambient lighting fell below a certain threshold.
I would also like to input data from a temperature sensor (Not sure what type would be best) but then read that data in Unity 3D and display it as U.I. text on a graphical GUI (NOT the old Unity On GUI) but the new UI system.

Has anyone done this who has some detailed information or perhaps even a tutorial on how to do this?
 

shteii01

Joined Feb 19, 2010
4,644
So...
What hardware does Unity 3D has to receive data?

I have done a little bit of work with Arduino. In my opinion, Arduino has poor description of the hardware. On the other hand, Arduino has a tone of examples which I can use to learn or copy code/concept from, and there is always the ultimate fallback, the ATmel datasheet.

At your first link, I see lots of buzz words, videos, "software platform" crap. Where is the hardware?
 

Thread Starter

Michael Knight

Joined Nov 5, 2015
90
So...
What hardware does Unity 3D has to receive data?

I have done a little bit of work with Arduino. In my opinion, Arduino has poor description of the hardware. On the other hand, Arduino has a tone of examples which I can use to learn or copy code/concept from, and there is always the ultimate fallback, the ATmel datasheet.

At your first link, I see lots of buzz words, videos, "software platform" crap. Where is the hardware?
Not really sure I follow you, all I am using is an Arduino Uno to communicate with unity 3D? I'm trying to use a light sensor (LDR) to input data into Unity 3D so I can have it trigger an audio clip in Unity 3D.

Maybe take a look at my Arduino code, maybe that might help?
Code:
int lightPin = 0; //Define Pin For Photoresistor
int lightInt = 0;

const byte rLed = 12; //Sets Pin number LED is conneted too
const byte yLed = 11;
const byte gLed = 10;
const byte bLed = 9;
const byte bLed2 = 8;
char myChar;         //changed the name of this variable because they are not all colurs now
const byte pulsePins[] = {13, 7};  //pins for a pulse output
char pulseTriggers[] = {'p', 'q'};
const int NUMBER_OF_PULSE_PINS = sizeof(pulsePins);
unsigned long pulseStarts[NUMBER_OF_PULSE_PINS];
unsigned long pulseLength = 1000;

void setup()
{
  //Serial.begin (9600);
  Serial.begin (115200);
  pinMode(rLed, OUTPUT);
  pinMode(yLed, OUTPUT);
  pinMode(gLed, OUTPUT);
  pinMode(bLed, OUTPUT);
  pinMode(bLed2, OUTPUT);
  digitalWrite(rLed, LOW);
  digitalWrite(yLed, LOW);
  digitalWrite(gLed, LOW);
  digitalWrite(bLed, LOW);
  digitalWrite(bLed2, LOW);
  for (int p = 0; p < NUMBER_OF_PULSE_PINS; p++)
  {
    pinMode(pulsePins[p], OUTPUT);
    digitalWrite(pulsePins[p], LOW);
  }
}

void loop()
{
  if (Serial.available())              //if serial data is available
  {
    //Light Sensor
    if((lightInt - analogRead(lightPin)) > 10 || (lightInt - analogRead(lightPin)) < -10){
      lightInt = analogRead(lightPin);
      Serial.println(lightInt);
    }
    int lf = 10;

    myChar = Serial.read();             //read one character from serial
    if (myChar == 'r')                  //if it is an r
    {
      digitalWrite(rLed, !digitalRead(rLed));  //change the state of the r LED
    }
    if (myChar == 'b')
    {
      digitalWrite(bLed, !digitalRead(bLed));
    }

    if (myChar == 'y')
    {
      digitalWrite(yLed, !digitalRead(yLed));
    }

    if (myChar == 'g')
    {
      digitalWrite(gLed, !digitalRead(gLed));
    }

    if (myChar == '1')
    {
      digitalWrite(bLed2, !digitalRead(bLed2));
    }

    for (int p = 0; p < NUMBER_OF_PULSE_PINS; p++)
    {
      if (myChar == pulseTriggers[p])
      {
        pulseStarts[p] = millis();  //save the time of receipt
        digitalWrite(pulsePins[p], HIGH);
      }
    }
  }

  //the following code runs each time through loop()
  for (int p = 0; p < NUMBER_OF_PULSE_PINS; p++)
  {
    if (millis() - pulseStarts[p] >= pulseLength)  //has the pin been HIGH long enough ?
    {
      digitalWrite(pulsePins[p], LOW);   //take the pulse pin LOW
    }
  }
}
 

Thread Starter

Michael Knight

Joined Nov 5, 2015
90
Exactly what it means. How does Unity 3D communicate to the Arduino. What hardware is involved?
OK this can explain it Way better than I can with my limited knowledge because all I know is that unity CAN communicate with Arduino, how it does it internally I have no clue, but watch this video and perhaps it might help.
 

shteii01

Joined Feb 19, 2010
4,644
OK this can explain it Way better than I can with my limited knowledge because all I know is that unity CAN communicate with Arduino, how it does it internally I have no clue, but watch this video and perhaps it might help.
Ok. Got it. Unity is software that runs on pc. Since it is software, it has no hardware.

Your next task is to go and learn how Arduino talks to PC. The key is the Windows OS (I assume you use Win7). It will be Windows OS that will receive data/signals from Arduino board. Then Unity will collect that data/signals from Windows. How that is going to work I have no effing clue, my education is electrical engineering, this is software engineering.

Bottom line. This is purely software issue. It has nothing to do with hardware. Go talk to people at Unity forums, at Arduino forums and even Windows software forums.
 

ISB123

Joined May 21, 2014
1,236
You are going to have to program Unity to read new device as a joystick or similar device,but before that you will need to write drivers for Windows to recognizance the device as HID since unity takes all the info through the OS.
 

Thread Starter

Michael Knight

Joined Nov 5, 2015
90
You are going to have to program Unity to read new device as a joystick or similar device,but before that you will need to write drivers for Windows to recognizance the device as HID since unity takes all the info through the OS.

Well getting Unity and Arduino to communicate is not so much the difficult part..... well maybe for me a little because I'm not really a code guy, graphics is more my field of play ;)
But I manage to get some pretty cool stuff done.
I recently picked up the DS18B20 Digital temperature sensor module for the Arduino. All I need to do now is just get it to display it's temperature read out information on a UI text in Unity 3D. The LDR you can see in my video I have hooked up to a Slider that reads the light value, I then have that trigger audio clips when the light falls between certain values, these audio clips I have set in a random array so I can add as many as I want that way K.I.T.T. can give a wide range of audio responses based on certain real world even triggers like light values.... Heck I even hear Arduino has a sound detector so I'll bet I could have a little fun with that too given enough figuring out time ;)

But yeah right now on my Christmas Wish list of stuff to get done is get the temperature data in and read out on the UI text in Unity...... Maybe have that trigger some audio event too ;)
 

spinnaker

Joined Oct 29, 2009
7,830
Talking to the DS18B20 is not a trivial task. The one wire protocol is very complex. I have some code written for the Pic, you are welcome to it if you are interested in converting it but you might be better off with one of the analog sensors. Hopefully someone has a library for you chip for the DS18B20.
 

shteii01

Joined Feb 19, 2010
4,644
From Arduino Playground:
http://playground.arduino.cc/Learning/OneWire
Addressing a OneWire device
Each 1-Wire device contains a unique 64-bit 'ROM' address, consisting of an 8-bit family code, a 48-bit serial number, and an 8-bit CRC. The CRC is used to verify the integrity of the data. For example, the sample code, below, checks if the device being addressed is a DS18S20 temperature sensor by checking for its family code, 0x10. To use the sample code with the newer DS18B20 sensor, you'd check for a family code of 0x28, instead, and for the DS1822 you'd check for 0x22.

Single-device commands
Before sending a command to a single slave device, the master must first select that device using its unique ROM. Subsequent commands will be responded to by the selected device, if found.

Multiple-device commands
Alternatively, you can address a command to all slave devices by issuing a 'Skip ROM' command (0xCC), instead. It is important to consider the effects of issuing a command to multiple devices. Sometimes, this may be intended and beneficial. For example, issuing a Skip ROM followed by a convert T (0x44) would instruct all networked devices that have a Convert T command to perform a temperature conversion. This can be a time-saving and efficient way of performing the operations. On the other hand, issuing a Read Scratchpad (0xBE) command would cause all devices to report Scratchpad data simultaneously. Power consumption of all devices (for example, during a temperature conversion) is also important when using a Skip ROM command sequence.

Please see the DS18S20 or DS18B20 datasheets for more detailed information.
 

Thread Starter

Michael Knight

Joined Nov 5, 2015
90
Thanks guys, I have the DS18B20 connected and it the Arduino code that is sending the data it seems like the same, as near as I can tell I have my Arduino code wrong because it's not differentiating between the two.... or maybe it is it's kind of hard to tell not being very good at code.


This seems to be making the LDR sensor work but I think it's using the Temperature Sensor data to do it.... I dunno it daffy.

Code:
void loop()
{
  //Request temperature from DS18B20
  sensor_incar.requestTemperatures();
  //Get the temperature
  float incarTemperature = sensor_incar.getTempCByIndex(0);

  //Send it to Unity
  Serial.print("T");
  Serial.println(incarTemperature);
  Serial.println("T32.00:"); //Not sure how this Works as some sort of marker?
 
  // read the new light value (in range 0..1023):
     int sensorValue = analogRead(lightSensorPin);
     // if the value changed by 10
     if(lightSensorValue - sensorValue > 10 || sensorValue - lightSensorValue > 10){
         lightSensorValue = sensorValue; // save the new value
         float p = lightSensorValue * (100.0 / 1023.0);    // make the value to range 0..100
         //Serial.print("L");
         Serial.println(p); // send it to unity
        
     }
  delay(30);
I keep messing with it tying to figure it out.
 

shteii01

Joined Feb 19, 2010
4,644
Thanks guys, I have the DS18B20 connected and it the Arduino code that is sending the data it seems like the same, as near as I can tell I have my Arduino code wrong because it's not differentiating between the two.... or maybe it is it's kind of hard to tell not being very good at code.


This seems to be making the LDR sensor work but I think it's using the Temperature Sensor data to do it.... I dunno it daffy.

Code:
void loop()
{
  //Request temperature from DS18B20
  sensor_incar.requestTemperatures();
  //Get the temperature
  float incarTemperature = sensor_incar.getTempCByIndex(0);

  //Send it to Unity
  Serial.print("T");
  Serial.println(incarTemperature);
  Serial.println("T32.00:"); //Not sure how this Works as some sort of marker?

  // read the new light value (in range 0..1023):
     int sensorValue = analogRead(lightSensorPin);
     // if the value changed by 10
     if(lightSensorValue - sensorValue > 10 || sensorValue - lightSensorValue > 10){
         lightSensorValue = sensorValue; // save the new value
         float p = lightSensorValue * (100.0 / 1023.0);    // make the value to range 0..100
         //Serial.print("L");
         Serial.println(p); // send it to unity
       
     }
  delay(30);
I keep messing with it tying to figure it out.
Here is sample code for S20: http://www.pjrc.com/teensy/td_libs_OneWire.html
 

spinnaker

Joined Oct 29, 2009
7,830
Post your schematic for the DS18B20 portion.

Is sensor_incar.requestTemperatures(); your code or some library that you are certain that works?

Do you have a scope? Can you confirm seeing data on the data line?
 

shteii01

Joined Feb 19, 2010
4,644
Thanks Guys,
Finally got it working with some help from the Arduino Forums.

Link to Arduino Forum post where the solution was a found :) Just in case anyone else is doing something similar and having trouble sorting it out :D
http://forum.arduino.cc/index.php?topic=369185.45
Remember this:
Bottom line. This is purely software issue. It has nothing to do with hardware. Go talk to people at Unity forums, at Arduino forums and even Windows software forums.
from post #10?
 
Top