Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I send numbers and variables to my arduino? #50

Closed
timeylies opened this issue May 26, 2021 · 1 comment
Closed

How can I send numbers and variables to my arduino? #50

timeylies opened this issue May 26, 2021 · 1 comment

Comments

@timeylies
Copy link

Here is the script:
/**

using UnityEngine;
using System.Collections;
using UnityEditor;

/**

  • Sample for reading using polling by yourself, and writing too.
    */
    public class SampleUserPolling_ReadWrite : MonoBehaviour
    {
    public SerialController serialController;
    public string speed;

    // Initialization
    void Start()
    {
    serialController = GameObject.Find("SerialController").GetComponent();

    }

    // Executed each frame
    void Update()
    {

     //---------------------------------------------------------------------
     // Send data
     //---------------------------------------------------------------------
    
    
     speed = Car_Controller.speed1.ToString("D5");
     serialController.SendSerialMessage(speed);
     
    
    
    
     //---------------------------------------------------------------------
     // Receive data
     //---------------------------------------------------------------------
    
     string message = serialController.ReadSerialMessage();
    
     if(message == "P")
     {
         EditorApplication.isPlaying = false;
     }
    
     if (message == null)
         return;
    
     // Check if the message is plain data or a connect/disconnect event.
     if (ReferenceEquals(message, SerialController.SERIAL_DEVICE_CONNECTED))
         Debug.Log("Connection established");
     else if (ReferenceEquals(message, SerialController.SERIAL_DEVICE_DISCONNECTED))
         Debug.Log("Connection attempt failed or disconnection detected");
     else
         Debug.Log("Message arrived: " + message);
    

    }
    }

Im trying to send my car speed to my arduino so it can display it to a lcd

@dwilches
Copy link
Owner

dwilches commented May 29, 2021

Hello,

On the receiving end, your numbers will be strings, so you need to find out how to convert a string to a number in C#. That's not specific to Ardity, but C#, look for a method that mentions "parsing" a string as a number. StackOverflow is a good place to find an answer to this.

Regards.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants