Arduino to Unity 3D Communication

Discussion in 'Embedded Systems and Microcontrollers' started by Michael Knight, Nov 20, 2015.

  1. Michael Knight

    Thread Starter Member

    Nov 5, 2015
    71
    3
    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?
     
  2. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,377
    494
    Never heard of Unity 3D.
    Got datasheet for it?
     
  3. Michael Knight

    Thread Starter Member

    Nov 5, 2015
    71
    3
    nope but I have a link ;)
    http://unity3d.com
     
  4. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,377
    494
    Link did not help.
     
  5. Michael Knight

    Thread Starter Member

    Nov 5, 2015
    71
    3
    Maybe this might help. I'm basically wanting to do this type of thing but instead of output from Unity 3D I would like to receive data into Unity 3D in the manor I mentioned at the beginning of this thread.
     
  6. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,377
    494
    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?
     
  7. Michael Knight

    Thread Starter Member

    Nov 5, 2015
    71
    3
    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 (Text):
    1. int lightPin = 0; //Define Pin For Photoresistor
    2. int lightInt = 0;
    3.  
    4. const byte rLed = 12; //Sets Pin number LED is conneted too
    5. const byte yLed = 11;
    6. const byte gLed = 10;
    7. const byte bLed = 9;
    8. const byte bLed2 = 8;
    9. char myChar;         //changed the name of this variable because they are not all colurs now
    10. const byte pulsePins[] = {13, 7};  //pins for a pulse output
    11. char pulseTriggers[] = {'p', 'q'};
    12. const int NUMBER_OF_PULSE_PINS = sizeof(pulsePins);
    13. unsigned long pulseStarts[NUMBER_OF_PULSE_PINS];
    14. unsigned long pulseLength = 1000;
    15.  
    16. void setup()
    17. {
    18.   //Serial.begin (9600);
    19.   Serial.begin (115200);
    20.   pinMode(rLed, OUTPUT);
    21.   pinMode(yLed, OUTPUT);
    22.   pinMode(gLed, OUTPUT);
    23.   pinMode(bLed, OUTPUT);
    24.   pinMode(bLed2, OUTPUT);
    25.   digitalWrite(rLed, LOW);
    26.   digitalWrite(yLed, LOW);
    27.   digitalWrite(gLed, LOW);
    28.   digitalWrite(bLed, LOW);
    29.   digitalWrite(bLed2, LOW);
    30.   for (int p = 0; p < NUMBER_OF_PULSE_PINS; p++)
    31.   {
    32.     pinMode(pulsePins[p], OUTPUT);
    33.     digitalWrite(pulsePins[p], LOW);
    34.   }
    35. }
    36.  
    37. void loop()
    38. {
    39.   if (Serial.available())              //if serial data is available
    40.   {
    41.     //Light Sensor
    42.     if((lightInt - analogRead(lightPin)) > 10 || (lightInt - analogRead(lightPin)) < -10){
    43.       lightInt = analogRead(lightPin);
    44.       Serial.println(lightInt);
    45.     }
    46.     int lf = 10;
    47.  
    48.     myChar = Serial.read();             //read one character from serial
    49.     if (myChar == 'r')                  //if it is an r
    50.     {
    51.       digitalWrite(rLed, !digitalRead(rLed));  //change the state of the r LED
    52.     }
    53.     if (myChar == 'b')
    54.     {
    55.       digitalWrite(bLed, !digitalRead(bLed));
    56.     }
    57.  
    58.     if (myChar == 'y')
    59.     {
    60.       digitalWrite(yLed, !digitalRead(yLed));
    61.     }
    62.  
    63.     if (myChar == 'g')
    64.     {
    65.       digitalWrite(gLed, !digitalRead(gLed));
    66.     }
    67.  
    68.     if (myChar == '1')
    69.     {
    70.       digitalWrite(bLed2, !digitalRead(bLed2));
    71.     }
    72.  
    73.     for (int p = 0; p < NUMBER_OF_PULSE_PINS; p++)
    74.     {
    75.       if (myChar == pulseTriggers[p])
    76.       {
    77.         pulseStarts[p] = millis();  //save the time of receipt
    78.         digitalWrite(pulsePins[p], HIGH);
    79.       }
    80.     }
    81.   }
    82.  
    83.   //the following code runs each time through loop()
    84.   for (int p = 0; p < NUMBER_OF_PULSE_PINS; p++)
    85.   {
    86.     if (millis() - pulseStarts[p] >= pulseLength)  //has the pin been HIGH long enough ?
    87.     {
    88.       digitalWrite(pulsePins[p], LOW);   //take the pulse pin LOW
    89.     }
    90.   }
    91. }
     
  8. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    Exactly what it means. How does Unity 3D communicate to the Arduino. What hardware is involved?
     
  9. Michael Knight

    Thread Starter Member

    Nov 5, 2015
    71
    3
    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.
     
  10. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,377
    494
    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.
     
  11. ISB123

    Well-Known Member

    May 21, 2014
    1,239
    527
    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.
     
  12. Michael Knight

    Thread Starter Member

    Nov 5, 2015
    71
    3


    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 ;)
     
  13. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    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.
     
  14. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,377
    494
    From Arduino Playground:
    http://playground.arduino.cc/Learning/OneWire
     
  15. Michael Knight

    Thread Starter Member

    Nov 5, 2015
    71
    3
    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 (Text):
    1. void loop()
    2. {
    3.   //Request temperature from DS18B20
    4.   sensor_incar.requestTemperatures();
    5.   //Get the temperature
    6.   float incarTemperature = sensor_incar.getTempCByIndex(0);
    7.  
    8.   //Send it to Unity
    9.   Serial.print("T");
    10.   Serial.println(incarTemperature);
    11.   Serial.println("T32.00:"); //Not sure how this Works as some sort of marker?
    12.  
    13.   // read the new light value (in range 0..1023):
    14.      int sensorValue = analogRead(lightSensorPin);
    15.      // if the value changed by 10
    16.      if(lightSensorValue - sensorValue > 10 || sensorValue - lightSensorValue > 10){
    17.          lightSensorValue = sensorValue; // save the new value
    18.          float p = lightSensorValue * (100.0 / 1023.0);    // make the value to range 0..100
    19.          //Serial.print("L");
    20.          Serial.println(p); // send it to unity
    21.        
    22.      }
    23.   delay(30);
    24.  
    I keep messing with it tying to figure it out.
     
  16. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,377
    494
    Here is sample code for S20: http://www.pjrc.com/teensy/td_libs_OneWire.html
     
  17. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    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?
     
  18. Michael Knight

    Thread Starter Member

    Nov 5, 2015
    71
    3
    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
     
  19. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,377
    494
    Remember this:
    from post #10?
     
Loading...