Sending Values From Arduino Uno to Unity 3D

Hello all, Im sure what Im trying to do is simple and I have watched multiple tutorials on this already but Im still not finding a working solution.

Im using an Arduino Uno board with a Compass/Accelerometer (LSM303DLHC)

The compass connects to the Arduino Uno with the SCL and SDA connections.(Power and GND also)

The readings im getting from the compass I have stored as a Float variable in Arduino and I want those to be sent and picked up in Unity where I can display them.

I feel my biggest road block is the explanations only talk about the Digital/ Analog pins and maybe Im not typing something in right for the SCL/SDA pins.

What are the I1 and I2 pins for on the compass board?

Thank you for reading.

What is a unity 3D? Can you provide a link.
Also post the code you are trying to use.

The game engine is Unity 3D

float heading = (atan2(event.magnetic.y,event.magnetic.x) * 180) / PI;

This line is converting my output of the compass in radians to degrees.

I want to sent the "heading" variable over to Unity and use it as a variable thats updating based on the compass on my Arduino Uno.

Any thoughts?

Thanks

Ok thanks. Would you not be better off asking on the Unity forum?

Basically what the Arduino can do is to look like a serial port so if Unity can accept input from a com port any Arduino can do that.

Otherwise the Leonard and the Micro can be made to look like a keybord or a mouse. So you can send commands as if you had typed them on a keyboard, or you can move the mouse pointer.

Are any of those acceptable to Unity?

It looks like a nice system thanks for bringing it to my attention.

I feel my biggest road block is the explanations only talk about the Digital/ Analog pins and maybe Im not typing something in right for the SCL/SDA pins.

Since you have not posted your Arduino code, we can't help.

Since you haven't posted a link to "the explanations", we can't help with your understanding of them.

What are the I1 and I2 pins for on the compass board?

No idea what you are talking about. Are you asking what they are for? Where they connect to the Arduino?

If using with an Arduino, its extra-easy to get started as we already wrote a nice little Arduino library to get you started.

Perhaps you need to read this...

PaulS: Your asking for my code…
float heading = (atan2(event.magnetic.y,event.magnetic.x) * 180) / PI;

That is the variable i want to send to Unity. The variable is called heading if you understand.

You asked what im looking for in the two pins on the compass board, yes I want to know what they are for if anyone knows. I provided a link to the board if your curious.

Here is my latest code, im just trying to print the updated values from the compass in the console log in Unity by sending them through the serial port.

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_LSM303_U.h>
#include <math.h>

Adafruit_LSM303_Mag_Unified mag = Adafruit_LSM303_Mag_Unified(12345);

const int scl = A5;
const int sda = A4;

void setup() {

Serial.begin(9600);

pinMode(SCL, INPUT);
pinMode(SDA, INPUT);
}
void loop() {

sensors_event_t event;
mag.getEvent(&event);

float heading = ((atan2(event.magnetic.y,event.magnetic.x) * 180) / PI);

Serial.write(heading);
Serial.flush();
delay(20);
}

AND HERE IS MY MASSIVE ERROR MESSAGE

Arduino: 1.6.4 (Windows 7), Board: “Arduino Uno”

sketch_aug11a_HelloUnity.ino: In function ‘void loop()’:
sketch_aug11a_HelloUnity:27: error: call of overloaded ‘write(float&)’ is ambiguous
sketch_aug11a_HelloUnity.ino:27:21: note: candidates are:
In file included from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:223:0,
from C:\Users\Brad\Documents\Arduino\libraries\Adafruit_Sensor_master/Adafruit_Sensor.h:24,
from sketch_aug11a_HelloUnity.ino:2:
C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/HardwareSerial.h:119:20: note: virtual size_t HardwareSerial::write(uint8_t)
virtual size_t write(uint8_t);
^
C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/HardwareSerial.h:120:19: note: size_t HardwareSerial::write(long unsigned int)
inline size_t write(unsigned long n) { return write((uint8_t)n); }
^
C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/HardwareSerial.h:121:19: note: size_t HardwareSerial::write(long int)
inline size_t write(long n) { return write((uint8_t)n); }
^
C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/HardwareSerial.h:122:19: note: size_t HardwareSerial::write(unsigned int)
inline size_t write(unsigned int n) { return write((uint8_t)n); }
^
C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/HardwareSerial.h:123:19: note: size_t HardwareSerial::write(int)
inline size_t write(int n) { return write((uint8_t)n); }
^
call of overloaded ‘write(float&)’ is ambiguous

This report would have more information with
“Show verbose output during compilation”
enabled in File > Preferences.

Please read this:-
How to use this forum
Because your post is breaking the rules about posting code.

You can not do this:-

float heading = ((atan2(event.magnetic.y,event.magnetic.x) * 180) / PI);

Serial.write(heading);

The write function only accepts single bytes.

What sort of data is the receiving program expecting?

pinMode(SCL, INPUT);
pinMode(SDA, INPUT);

SCL and SDA are analog pins. Fucking with the pin mode for them is pointless.

sketch_aug11a_HelloUnity:27: error: call of overloaded 'write(float&)' is ambiguous

So, why are you NOT using Serial.print()?

Serial.flush();
delay(20);

Block until all pending output has been sent, and then sit around with your thumb up your ass for a while longer? Why?

Grumpy Mike: I am sorry about the poor posting, it will not happen again. The tutorial about the compass gave me that line of code that is suppose to convert the readings from the compass from Radians to Degrees. I am trying to send that degrees read out to the receiving software(Unity 3D).

PaulS: I thought the SDA and SCL was where the radian reading were coming from the compass to my Uno, my mistake.

I didn’t have Serial.print because I was trying to send that message to Unity.

Again I’m sorry if this code is all wrong but I honestly read and watched over 10 tutorials trying to piece different things together to get what I’m trying to achieve.

Using Serial.print(heading); I am able to get a constant update from the compass in the Serial Monitor and I am trying to achieve the same thing in the Unity console.

Thank you for all the direction this far.

PaulS: I thought the SDA and SCL was where the radian reading were coming from the compass to my Uno, my mistake

They are but they come though the I2C bus which is attached to those pins. This is handled by the wire library you don’t have to do anything more.

Using Serial.print(heading); I am able to get a constant update from the compass in the Serial Monitor and I am trying to achieve the same thing in the Unity console.

That should be enough at the Arduino end.
You can not have the serial monitor and Unity look at the data at the same time, you must close one down. Maybe that is where you are going wrong. Can you post a link to what you are trying to follow at the Unity. End of things.

I've actually done this before... It's really not that hard.

Like others have said, just use serial output on Arduino (Serial.println(data)). In Unity you can code in C# so just create a class that processes the serial data.

Just use System.IO.Ports and open a Serial stream

i.e. SerialPort stream = new SerialPort("COM9", 9600);

Do stream.open() and stream.ReadLine () to get the data from Serial in Unity.

I would recommend threading the data, as I couldn't find a way to check if there was actually data available in the buffer, instead I just had to continuously read it. This can be expensive computationally if you have it running in the same thread as the rest of your Unity stuff.

I would recommend threading the data, as I couldn't find a way to check if there was actually data available in the buffer, instead I just had to continuously read it.

Multi-threading is the way to go, but not for that reason. The System.IO.Ports.SerialPort instance has a property, BytesToRead, that changes as data arrives.

Multi-threading is needed because the user interface itself runs in it's own thread, so showing incoming serial data in the user interface requires intra-thread communication. So, you might as well have serial data handling done in a separate thread, and have that thread communicate with the UI thread.

PaulS:
The System.IO.Ports.SerialPort instance has a property, BytesToRead, that changes as data arrives.

I remember finding that property, but never being able to implement it. I'm not sure why, but even when there was data I would get a null exception. I found this is true for other users using C# in Unity - since technically it's actually a certain version of Mono C#. Maybe I was just using it wrong though.

Other people have had the issue here

You can also use the event DataReceived, but I wasn't able to get it to properly bind to a function. It might be because I'm more used to C++ than C#, or maybe there are some limitations in the Unity version of C#.

With regards to threading, I didn't realize that. I normally just thread so that it doesn't cause huge hangups / delays in the main thread, and additionally if you have a time sensitive project for data you'll end up getting a mismatch between the actual state of the arduino and the serial data you've read. That's good to know though.

Thank you everyone for the helpful comments.

I took some time to read more and do more trial and error before posting any updates.

I am now seeing a steady stream of data coming to the Unity Game Engine by creating the SerialPortStream
and streamOpen and streamReadLine.

My problem is something I am not sure if it is a Arduino problem or a Unity problem. The numbers do not match up from the different Console Logs.

In the Arduino Serial Monitor, I am seeing my compass heading displayed in radians as I would like for the test.

EX:
Compass Heading: -1.12
Compass Heading: -1.12
Compass Heading: -1.13
Compass Heading: -1.11
Compass Heading: -1.12
Compass Heading: -1.12
Etc....

In the Unity Game Engine Console Log, I am seeing numbers that range from 10 - 115. Is there a significance of these numbers ranging between those two? And why am I not seeing my radian compass headings.

If you guys think this is something that needs to be on a Unity Game Engine forum, I apologize and will redirect.

Thank you

If what you are seeing on the serial monitor is what you expect to see then the problem can only be what Unity interpretes this to be.

You only need to fix the Arduino if you can't change what you are doing in Unity.

Understandable.

Grump_Mike: So going back to one of my first questions about sending the heading variable only through the Serial Port, is something like that possible? I know we established that using Serial.write(heading) wont work because it only accepts single bytes.

So what Im really trying to do is use Arduino Uno to do the equations that need to be worked out with the Compass/ Accelerometer Sensor’s output > saved into a variable ex:(heading) > then just send that variable through the SerialPort to Unity Game Engine.

What do you think?

I know we established that using Serial.write(heading) wont work because it only accepts single bytes.

If you need to send the whole message in terms of serial write then simply put it in a for loop that puts them out one byte at a time.

So what Im really trying to do .....

Yes I think we have got that. What we ( I ) don't know is what form of data Unity is expecting. If you know that then you can make the Arduino send that. If the Arduino is sending what you want them you have to change how Unity interpreters what it is receiving.

As you know I know nothing about Unity so you have to understand what it wants and tell me then I can tell you how the Arduino should send the data to match what Unity is expecting.

Grump_Mike: Thank you for that. I will do some digging on the Unity forums and get back to you. I really appreciate this help.