dwilches/Ardity

Arduino Joystick Issue.

Closed this issue · 7 comments

Hi. I have a problem with adding the values of arduino analog joystick.
the buttons and leds are working fine, but need to add a joystick to rotate the camera.

the issue is in split. if you can, please tell me what is wrong. thanks
the DATAQ is used also for other inputs.

  string DATAQ = serialController.ReadSerialMessage();
        string[] vec3 = DATAQ.Split(',');
         if (vec3[0] != "" && vec3[1] != "" && vec3[2] != "")
        {
         transform.Rotate(
             float.Parse(vec3[0]) - lastRot[0],
             float.Parse(vec3[1]) - lastRot[1],
             float.Parse(vec3[2]) - lastRot[2]);

           lastRot[0] = float.Parse(vec3[0]);
        lastRot[1] = float.Parse(vec3[1]);
         lastRot[2] = float.Parse(vec3[2]);
        }

Hello,
Some questions before I'm able to help:

  • What value are you receiving in DATAQ?
  • Is it the same value that you are sending from Arduino or you are receiving an unexpected value?
  • Why is the code not working as expected? You mentioned there is an issue but what is the issue you are seeing?

Hello,
Some questions before I'm able to help:

  • What value are you receiving in DATAQ?
  • Is it the same value that you are sending from Arduino or you are receiving an unexpected value?
  • Why is the code not working as expected? You mentioned there is an issue but what is the issue you are seeing?

Hi. Thank you for your reply.

In DATAQ i'm receving the buttons states and want to receive the joystick pos.n .
in arduino i have this code>

{
  float axeX;
  float axeY;
  axeX = analogRead(joyPin1);
  delay(10);
  axeY = analogRead(joyPin2);
  axeX = map(axeX, 0, 1023, 0, 360) / 10;
  axeY = map(axeY, 0, 1023, 0, 360) / 10;

  Serial.print(axeX);
  Serial.print(",");
  Serial.print(axeY);
  Serial.print(",");
  Serial.println(255);
}
void LED1()
{
  switch (Serial.read())
  {
  case 'A':
    digitalWrite(led1, HIGH);
    Serial.println('9');
    break;
  case 'B':
    digitalWrite(led2, HIGH);
    Serial.println('6');
    break;
  case 'C':
    digitalWrite(led3, HIGH);
    Serial.println('6');
    break;
  case 'D':
    digitalWrite(led4, HIGH);
    Serial.println('6');
    break;
  case 'E':
    digitalWrite(led5, HIGH);
    Serial.println('6');
    break;
  }
}

As example i tried this code

using System.Collections;
using System.IO.Ports;

public class SerialRotation : MonoBehaviour
{
    private SerialController serialController;
    float[] lastRot = { 0, 0, 0 };
    void Start()
    {
        serialController = GameObject.Find("SerialController").GetComponent<SerialController>();
    }

    void Update()
    {
        string DATAQ = serialController.ReadSerialMessage();
        // string axesRED = serialController.ReadLine();
        string[] vec3 = DATAQ.Split(',');
        if (vec3[0] != "" && vec3[1] != "" && vec3[2] != "")
        {
            transform.Rotate(
                float.Parse(vec3[0]) - lastRot[0],
                float.Parse(vec3[1]) - lastRot[1],
                float.Parse(vec3[2]) - lastRot[2],
                Space.Self
            );

            lastRot[0] = float.Parse(vec3[0]);
            lastRot[1] = float.Parse(vec3[1]);
            lastRot[2] = float.Parse(vec3[2]);

        }
        Debug.Log(DATAQ);
    }

    void OnGUI()
    {
        string newString = "Connected: " + transform.rotation.x + ", " + transform.rotation.y + ", " + transform.rotation.z;
        GUI.Label(new Rect(10, 10, 300, 100), newString);

    }

}

and it's working perfectly. but i need to use ARDITY for threading because i have buttons,led and joystick.

in script ardity i haven't found how to do .ReadLine();.

I'm receiving now the message with pos.n like x,y,z.

Thank you.

By default, ReadSerialMessage reads one line from the data sent by the Arduino, so the ReadLine you are looking for is ReadSerialMessage.

In the code above I see you are already using ReadSerialMessage and you mention it is working perfectly, so it's still not clear to me what is the part that is not working well. For example: do you get a line that is chopped? do you get extra characters? are you missing a line that you sent from Arduino?

By default, ReadSerialMessage reads one line from the data sent by the Arduino, so the ReadLine you are looking for is ReadSerialMessage.

In the code above I see you are already using ReadSerialMessage and you mention it is working perfectly, so it's still not clear to me what is the part that is not working well. For example: do you get a line that is chopped? do you get extra characters? are you missing a line that you sent from Arduino?

Thank you for reply.
when i start the game the game is freezing and i receive only messages.
i think the problem is in Split. string[] vec3 = DATAQ.Split(',');

the code that work whell is that other(second code posted) with standart com port comunication(that is not good for me. i need to use ardity).

This one is the problem. sorry,i've not posted it before.

using System.Collections.Generic;
using UnityEngine;
using System.IO.Ports;
using UnityEngine.UI;
using System.IO;
public class LEDSONOFF : MonoBehaviour
{
    public Animator anim;
    private SerialController serialController;
    // GameObject QUESTIONS; 
    public GameObject QUESTIONS; // Assign in inspector
    float[] lastRot = { 0, 0, 0 };

    void Start()
    {
        serialController = GameObject.Find("SerialController").GetComponent<SerialController>();
        QUESTIONS.SetActive(false);
        anim.SetBool("butclose", false);
        anim.SetBool("but2close", false);
        anim.SetBool("but3close", false);
        anim.SetBool("but4close", false);
        anim.SetBool("but5close", false);
    }


    void OnGUI()
    {
        string newString = "Connected: " + transform.rotation.x + ", " + transform.rotation.y + ", " + transform.rotation.z;
        GUI.Label(new Rect(10, 10, 300, 100), newString);

    }
    void READWRITE()
    {
        string DATAQ = serialController.ReadSerialMessage();
        string[] vec3 = DATAQ.Split(',');
        if (vec3[0] != "" && vec3[1] != "" && vec3[2] != "")
        {
            transform.Rotate(
                float.Parse(vec3[0]) - lastRot[0],
                float.Parse(vec3[1]) - lastRot[1],
                float.Parse(vec3[2]) - lastRot[2]);

            lastRot[0] = float.Parse(vec3[0]);
            lastRot[1] = float.Parse(vec3[1]);
            lastRot[2] = float.Parse(vec3[2]);
        }
        print(DATAQ);

        if (DATAQ == "8")//read button 1 from arduino
        {
            startANIM();
            print("konpka1");
            QUESTIONS.SetActive(false);
        }

I suggest you troubleshoot the code by putting a Console.WriteLine like this:

string DATAQ = serialController.ReadSerialMessage();
Console.WriteLine("Message Arrived: " + DATAQ);

Then you'll see if Unity is not receiving the data from the Arduino (in which case we can take a look at why), or if the issue is somewhere in your code after the line is received (in which case asking in SO would be the way to go).

I suggest you troubleshoot the code by putting a Console.WriteLine like this:

string DATAQ = serialController.ReadSerialMessage();
Console.WriteLine("Message Arrived: " + DATAQ);

Then you'll see if Unity is not receiving the data from the Arduino (in which case we can take a look at why), or if the issue is somewhere in your code after the line is received (in which case asking in SO would be the way to go).

Hi. thank you for your reply.
i'm receiving and sending messages unity <-> arduino.
i think that the .Split(','); is not working with ardity. have ever tryed this function ?

String.Split is a method from C#. You can use it with any string, coming from Ardity or elsewhere.

So if the string arrives ok from Arduino -> Ardity -> your Unity script, and the issue is with Split, then we know the issue is not with Ardity. The best place to ask is StackOverflow.