1

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

0

1 Answer 1

0

My best guess would be that the sp.Open() fails to open the COM port and throws an exception on sp.ReadLine();

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.