Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 joan · Apr 26, 2014 at 05:19 PM · arduino

Arduino to unity

I have got a script to send data from arduino to unity but it doesn´t work. When i open the serial monitor of arduino, all data are ok, but in unity it doesnt work... When i click on play in unity the Rx led of arduino blink, but that´s it, no data from arduino. I need some help please!!!

My arduino code:

 const int buttonPin01 = 6;
 const int buttonPin02 = 7;
 
 void setup()
 {
   Serial.begin(9600);
   
   pinMode(buttonPin01, INPUT);
   pinMode(buttonPin02, INPUT);
   digitalWrite(buttonPin01, HIGH);
   digitalWrite(buttonPin02, HIGH);
 }
 
 void loop()
 {
   if(digitalRead(buttonPin01) == LOW)
   {
   Serial.print(1);
   Serial.flush();
   delay(20);
 }
 
 if(digitalRead(buttonPin02) == LOW)
   {
   Serial.print(2);
   Serial.flush();
   delay(20);
 }
 }

And my unity code:

 using UnityEngine;
 using System.Collections;
 using System.IO.Ports;
 
 
 public class TutScript : MonoBehaviour {
     
     public float speed;
     private float amountToMove;
     
     private SerialPort sp = new SerialPort("COM3",9600);
      
     // Use this for initialization
     void Start () {
         sp.Open();
         sp.ReadTimeout = 0;
     
     }
     
     // Update is called once per frame
     void Update () {
     
         amountToMove = speed * Time.deltaTime;
         
         if(sp.IsOpen)
         {
             try
             {
                 MoveObject(sp.ReadByte());
                 print(sp.ReadByte());
             }
             catch(System.Exception)
             {
             }
         }
     }
 
     
     void MoveObject(int Direction) 
 {
     if (Direction == 1)
     {
         transform.Translate(Vector3.left * amountToMove, Space.World);
     }
     if (Direction == 2)
     {
         transform.Translate(Vector3.right * amountToMove, Space.World);
     }
 }
     }
     



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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by MarkD · May 09, 2015 at 08:21 PM

Hi, I had the same problem this sp.Open(); sp.ReadTimeout = 0;

should never be 0, you have to timeout for at least 1 milisecond. If you wan't it to continiously read, just put // infront of that code to disable it or remove the line.

Comment
Add comment · Share
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
0

Answer by cgressel · Oct 11, 2018 at 04:01 PM

hey guys hope you can help me! i try to use the serial numbers from my arduino board to transform the object in unity. on arduino site my code is really simple and looks like this:

   void setup() {
   // put your setup code here, to run once:
   Serial.begin(9600);
 }
 
 void loop() {
   // put your main code here, to run repeatedly:
   int a = analogRead(A0);
   //Sleep for 50ms, which provides the recommended sample rate (20Hz)
   delay(500);
   Serial.println(a);
 }

in the serial monitor from arduino i get these values (picture)alt text

now i want to use these values to constantly transform my geometry (a cube) in unity. for example if the value from arduino is 50 so the cube should be scaled by 50

do you know how this can work???

thanks in advance


unbenannt.jpg (27.9 kB)
Comment
Add comment · Share
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
0

Answer by unity_XY9GfkpHMyaikw · Apr 16, 2019 at 09:47 AM

What namespaces are you using? using System.IO is obsolete right now and changing my API compatibility to .NET 2.0 doesn't change anything.

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

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

23 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

Related Questions

Cannot get HID gamepad to work in Android 0 Answers

How do I to send a varible via bluetooth android 0 Answers

I'm trying to change the colour of an RGB LED using an object in Unity! 0 Answers

Is it possible to make unity take control of my mouse and control it via arduino joystick? 0 Answers

delay sending to serial port 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