im working with two mpu6050 sensors, 2 arduino boards (1 arduino Mega and 1 Arduino Uno) and unity 3d. Each board has one mpu6050 connected and it is supposed to send the information recieved to unity. Im having troubles getting the data from unity (I have no problems with the information viewing it from the Arduino's IDE) Im working with two differents COM, 1 per board.
With my current unity code im having problems since it crashes when i try to debug it and when it doesen't it sends an exception which says something like:
Ports.io access denied
I have the following arduino code (it is resumed to only the part that affects this question):
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(map((ypr[2] * 180/M_PI),-90,90,0,360));
delay(20);
}
Note: The MPU sends values from -90 to 90, so i use the map function to transform those values in a range from 0 to 360
Here is the unity code (i have two differents code, but the only difference is where it says "COM4" the other one says "COM3"):
using UnityEngine;
using System.Collections;
using System.IO.Ports;
public class Mover_Con_Arduino : MonoBehaviour
{
SerialPort sp = new SerialPort("COM4", 9600);
void Start()
{
}
void Update()
{
if (!sp.IsOpen)
{
sp.Open();
}
float rot = float.Parse (sp.ReadLine ());
transform.localEulerAngles = new Vector3 (rot, 0, 0);
print (rot);
}
}
What im doing is using the value sent from arduino to unity to rotate a gameobject