Sending X,Y Coordinates from Unity3d to Arduino

Hi , i am trying to figure out how to get X,Y coordinates to the arduino through Serial usb . As we know we cant use Serial Monitor while we are sending data through Serial usb . I am sending via Unity3d X,Y coordinates endlessly(type: String) . X,Y coordinates can also have negative values. Any ideas for Arduino code ? Here is the Unity3d code :

using UnityEngine;
using System.Collections;
using System.IO;
using System.IO.Ports;

public class Avatar_position : Monobehaviour {
         private SerialPort sp;
         public GameObject avatar_center;
         public GameObject robot_center;
         public Vector3 avatar_Pos;
         public Vector3 robot_Pos;
         public Vector3 relative_Pos;
                   void Start() {
                          sp = new SerialPort("COM3",9600);
                          sp.ReadTimeout = 50;
                          sp.Open();
                   }
                   void Update() {
                          avatar_Pos = avatar_center.transform.position;
                          robot_Pos = robot_center.transform.position;
                          relative_Pos = -1000.0f*(avatar_Pos - robot_Pos);
                          string _x = relative_Pos.x.ToString();
                          string _y = relative_Pos.y.ToString();
                          char[] s = new char[1];
                          s[0] = 'x';
                          sp.Write(s,0,1);
                          sp.Write(_x);
                          s[0] = 'y';
                          sp.Write(s,0,1);
                          sp.Write(_y);
                          s[0] = 'e'       // to understand full package of X,Y is sent
                          sp.Write(s,0,1);
                    }
}

As we know we cant use Serial Monitor while we are sending data through Serial usb .

That has nothing to do with what the code on the Arduino looks like.

Any ideas for Arduino code ?

http://forum.arduino.cc/index.php?topic=396450.0

xristos_mar:
Hi , i am trying to figure out how to get X,Y coordinates to the arduino through Serial usb .
As we know we cant use Serial Monitor while we are sending data through Serial usb .

What Arduino board are you using?

A Mega has 3 spare hardware serial ports and a Leonardo or Micro has 1. If you are using an Uno you can create an extra serial port with SoftwareSerial

…R

hi robin , the link Paul sent is very helpful , where u explain many things about it .I am using a ArduinoMega . What do you mean by SoftwareSerial ? can i see what i am sending to arduino mega through the Serial monitor ? Thanks for the replies.

With a Mega there is no need to use SoftwareSerial. Just connect your Unity3D device's Tx and Rx pins to the Serial1 Rx and Tx pins, and also connect GND to GND.

...R

unity3d's device is PC , I dont think thats possible thats why i am using the usb cable for communication . or is it ?

or is it ?

Yes, that's why we told you.

You need a USB to serial TTL lead and connect the TTL up to a spair serial port on the mega. Then you can pick either port for your unity3D software and the other one for the serial monitor.

Grumpy_Mike: Yes, that's why we told you.

You need a USB to serial TTL lead and connect the TTL up to a spair serial port on the mega. Then you can pick either port for your unity3D software and the other one for the serial monitor.

Thanks mate . Something like this ? https://grobotronics.com/usb-to-ttl-serial-cable-debug-console-cable-for-raspberry-pi.html

Quote from your link

This cable is not good for Arduino re-programming

I suggest you get a device that can also be used for Arduino programming. It’s unlikely to be any more expensive.

…R

xristos_mar:
Something like this ?
USB to TTL Serial Cable - Debug / Console Cable for Raspberry Pi

Yes that will do the job of giving you a second serial port.

Thank you guys . its working . You helped me a lot .