- Home /
Sending Data From Unity To Arduino
Hi! I am currently working on a project for a vr glove that is controlled with an arduino that communicates with Unity. I currently have a system set up that tests when the finger in unity collides with an object and sends the fingers angle to the arduino so that it can set the lock position (The angle that a servo limits your finger to so that you cannot bend past that angle.) to that angle. I have unity send the data into the serial monitor and a "C" in front of it in order for the arduino to distinguish it from other data. Here is my code for unity:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class arduinoCollision : MonoBehaviour
{
public arduinoMovement arduino;
public float lockPos;
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Finger")
{
arduino.sp.WriteLine("C" + arduino.pos);
Debug.Log("C" + arduino.pos);
}
}
private void OnTriggerExit(Collider other)
{
if (other.tag == "Finger")
{
arduino.sp.WriteLine("C180");
}
}
}
(I have the arduino and Unity link in script "arduinoMovement")
Arduino code:
#include <Servo.h>
int analogPin = 3;
int lockPos = 90;
Servo servo1;
void setup()
{
Serial.begin(2000000);
}
void loop()
{
int val = analogRead(analogPin);
int position = map(val, 105, 466, 0, 180);
Serial.println(position);
if(Serial.available() > 0)
{
String dataString = Serial.readString();
if(dataString.startsWith("C"))
{
dataString.replace('C', '0');
delay(30);
lockPos = dataString.toInt();
}
}
if(position > lockPos)
{
servo1.attach(9);
servo1.write(lockPos);
delay(30);
servo1.detach();
}
}
Currently the data does not transfer quick enough and seems to be very finicky with setting the lock Position. If you know how to fix this I would really appreciate some help. Thanks!
Uhm You have two potential 30 ms delays in your arduino code. 30ms would limit you to about 33 fps not considering the actual latency of the interface you're using. How is it actually connected? Serial port? Baud rate? Reading from a serial port is done character by character. "readString" will read all data currently available. You don't seem to care about any sort of transfer protocol. You use WriteLine on the sending side which should actually add a linefeed character at the end. For time critical applications it might be better to don't use text based data encoding as text generally requires much more characters to transfer the same information. If you just want to transmit an angle it might be enough to encode it as a single byte (256 distinct directions).
Sorry this is my first project involving serial data and do not have much experience with it. I need to transfer data between the Arduino and Unity while being able to decifer and sort the data. What do you think is the most efficient way of doing this?
Curious, this isn't by chance a project for Deco3850?
Your answer
Follow this Question
Related Questions
Unity crashes when connecting to a serial port 0 Answers
Transform Unity Object with Arduino 2 Answers
The Semaphore Timeout Period Has Expired 0 Answers
Using Arduino as controller in Unity is giving me bad framerate 1 Answer
How do I use servo motor as output in arduino uno from touch controllers in unity3D? 1 Answer