Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by saml_baker · Apr 09, 2018 at 03:35 AM · vrarduinoserialportserial

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!

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Apr 09, 2018 at 05:09 AM 1
Share

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).

avatar image saml_baker Bunny83 · Apr 10, 2018 at 12:39 AM 0
Share

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?

avatar image Cornelis-de-Jager · Apr 09, 2018 at 05:51 AM 0
Share

Curious, this isn't by chance a project for Deco3850?

avatar image saml_baker Cornelis-de-Jager · Apr 10, 2018 at 12:32 AM 0
Share

No. It's a personal project.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

105 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges