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 () {
    	} 
}

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.

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.

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

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
        }*/
    }
}

connect arduino to unity on android with this plugin: