Sending Data from Arduino Uno to Unity
Hello guys, I have a question about one thing, sending a variable from my Arduino Uno to the Unity Game Engine.
As of now, I have been able to communicate the two devices successfully by sending byte info so a magnetic reed switch on my Arduino Uno to move my object in Unity. Now I am trying to send something more complex, a variable that might be positive or negative and include a decimal.
In Arduino, I am sending the data through the Serial Port and in unity i opened the Serial Port to receive it. In the Console Log in Unity, i am receiving data but the numbers are not matching up to the ones displayed in the Arduino Console Log.
It was suggested to me from the Arduino community that it must be something on the Unity end since they have signed off on everything Im doing from the Arduino. They suggested there might be some kind of conversion going on during the transfer through the SerialPort.
Hopefully I made myself clear in my explanation. Here is the Unity code:
using UnityEngine;
using System.Collections;
using System.IO.Ports;
using Uniduino;
public class Movement : MonoBehaviour {
public Arduino arduino;
private GameObject cube;
public float speed;
private float amountToMove;
private SerialPort sp = new SerialPort("COM9",57600);
void Start () {
sp.Open();
sp.ReadLine();
sp.ReadTimeout = 1;
}
void Update () {
}
}
Answer by Farneze · Sep 21, 2015 at 12:09 AM
Hello Surfninja,
I was doing some tests on my own and found this code working for me. On arduino side, I used this code:
int count = 1000;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
count = count +1;
if(count == 10000)
count = 0;
Serial.print("Teste: ");
Serial.println(count);
delay(1000);
}
And on Unity side, I used this code:
using UnityEngine;
using System.Collections;
using System.IO.Ports;
public class Move : MonoBehaviour {
SerialPort sp = new SerialPort("COM3", 9600);
void Start () {
sp.Open ();
sp.ReadTimeout = 1;
}
void Update ()
{
try{
print (sp.ReadLine());
}
catch(System.Exception){
}
}
}
So, I can confirm that arduino sends count from 1000 through serial port to Unity, and the unity shows on its console the output correctly.
Best regards,
Farneze: Thank you very much for that, I was able to get my variables sent over exactly as they appear in my Arduino Serial Log.
$$anonymous$$y next hurdle is now that I have the proper information strea$$anonymous$$g into Unity, is there a way save that into variables in Unity that can be utilized.
For Example, if I had Arduino send out: float UnitA float UnitB
Is there a way to receive those in Unity, save them as: public float UnitA public float UnitB
And then from there use those updated Units in Unity to perform a task? I'm sure this is possible, just outside my program$$anonymous$$g knowledge base. Thank you for your help.
Answer by Surfninja · Sep 21, 2015 at 09:43 AM
Farneze: Thank you very much for that, I was able to get my variables sent over exactly as they appear in my Arduino Serial Log.
My next hurdle is now that I have the proper information streaming into Unity, is there a way save that into variables in Unity that can be utilized.
For Example, if I had Arduino send out: float UnitA float UnitB
Is there a way to receive those in Unity, save them as: public float UnitA public float UnitB
And then from there use those updated Units in Unity to perform a task? I'm sure this is possible, just outside my programming knowledge base. Thank you for your help.
Answer by WilmUGameDev · Jan 16, 2016 at 12:21 AM
I can't get anything but hex numbers out of the Arduino...no strings, int, bool, nada just random numbers......what the heck am I missing
UPDATE: Apparently the code for the arduino I was using created HEX values, so when I was reading them into Unity I got the "random" numbers - I thought that it was the way I was parsing them into unity, but I used the code above and was able to recreate the same results......I'm reading IR codes from a remote control......at least now I can get my variables out...
Answer by maxparadox · Apr 22, 2016 at 05:36 PM
hope this helps i know its an old post ... but i thought its best to Share the answer .The code is working properly ..
using UnityEngine; using System.Collections; using System.IO.Ports;
public class RotScript : MonoBehaviour
{
private float calcInput = 0f;
private float epsilon = 0.01f;
private float factor = 2f;
private float currentInput = 0f;
/* private float timer = 0f;
private float timeToUpdate = 1f;
*/
SerialPort serial = new SerialPort("COM3", 9600);
void Start () {
}
void Update ()
{
if (!serial.IsOpen)
serial.Open ();
currentInput = int.Parse (serial.ReadLine ());
// use this block for linear increase or decrease
if (calcInput < currentInput - epsilon)
{
calcInput += Time.deltaTime * factor;
}
else if (calcInput > currentInput + epsilon)
{
calcInput -= Time.deltaTime * factor;
}
else // calcInput is really close to currentInput so we are ready to update currentInput
{
// get input from arduino
currentInput = float.Parse(serial.ReadLine ());
}
transform.localEulerAngles = new Vector3(0,calcInput,0);
/*timer += Time.deltaTime;
if (timer >= timeToUpdate)
{
timer = 0f; // reset timer
//get input from arduino
}*/
}
}
Your answer
Follow this Question
Related Questions
How can i open txt file outside the unity editor. When i have already finshed the game. 1 Answer
Is it possible to write to a port in C++ and read the data in Unity? And if so...how? 0 Answers
Read Arduino Stream with Js or C# 0 Answers
HELP: Arduino Genuino 101 to Unity3D. Serial Communication 0 Answers
Memory problem on android/ios 1GB ram with string[]? 0 Answers