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 02, 2018 at 03:12 AM · unity 5rotationarduinoserialportserial.io.port

Need Help Connecting Arduino to Unity

Hi. I am trying to make a glove with my arduino uno in which I am able to connect a potentiometer to each finger import the serial data into Unity to control a character. In my arduino sketch it is pretty simple. It converts the analog input of the pot, then converts it into degrees (0 - 179) where it is put into the serial monitor. In Unity I made a script that looks like this.

 using UnityEngine;
 using System.Collections;
 using System.IO.Ports;
 using System;
 using System.Linq;
 
 public class ArduinoConnector : MonoBehaviour
 {
     SerialPort sp;
     string[] stringDelimiters = new string[] { ":", "R", }; //Items we want to ignore in strings.
     public Transform target; //The item we want to affect with our accelerometer
 
     void Start()
     {
         sp = new SerialPort("COM5", 9600, Parity.None, 8, StopBits.One); //Replace "COM4" with whatever port your Arduino is on.
         sp.DtrEnable = false; //Prevent the Arduino from rebooting once we connect to it. 
                               //A 10 uF cap across RST and GND will prevent this. Remove cap when programming.
         sp.ReadTimeout = 1; //Shortest possible read time out.
         sp.WriteTimeout = 1; //Shortest possible write time out.
         sp.Open();
         if (sp.IsOpen)
         {
             sp.Write("Hello World");
             Debug.Log("Connection Successful!");
         }
         else
             Debug.LogError("Serial port: " + sp.PortName + " is unavailable");
     }
 
     void Update()
     {
         string cmd = CheckForRecievedData();
         
             float position = Single.Parse(cmd);
             target.transform.rotation = Quaternion.Slerp(target.transform.rotation, Quaternion.Euler(target.transform.rotation.x, target.transform.rotation.y, position + 90f), Time.deltaTime * 2f);        
 
             if (Input.GetKeyDown(KeyCode.Escape) && sp.IsOpen)
             sp.Close();
     }
 
     public string CheckForRecievedData()
     {
         try //Sometimes malformed serial commands come through. We can ignore these with a try/catch.
         {
             string inData = sp.ReadLine();
             int inSize = inData.Count();
             if (inSize > 0)
             {
                 Debug.Log("ARDUINO->|| " + inData + " ||MSG SIZE:" + inSize.ToString());
             }
             //Got the data. Flush the in-buffer to speed reads up.
             inSize = 0;
             sp.BaseStream.Flush();
             sp.DiscardInBuffer();
             return inData;
         }
         catch { return string.Empty; }
     }
 }

My problem is that when I run this it takes in the data in the beginning and rotates the object but then does not update for the rest of the time almost as if it is in the start function even though I put it in update. I do not know what the problem is but if anyone has any experience with this I would really appreciate some help. Thanks!

Comment
Add comment
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

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

177 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 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

Read in 2 Different Arduino Values 1 Answer

Serial Communication-Best Practise 1 Answer

Problems With Sending Data through Serial Port 1 Answer

Communication between Unity and Arduino Yun via WiFI 1 Answer

Cant drag player into Network transform child 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