Unity Not Sending Right Strings To Arduino

Hey I am using a servo motor and connected it with unity, problem is

That The Numbers that are going through unity to arduino are not right on the arduino side of things

please help

please help

Based on what? Your completely unfounded claims?

POST YOUR CODE to back those claims up. Post your proof!

Unity

    SerialPort sp = new SerialPort("COM2",9600);

    [Range(0, 360)] public float servo;

    public Text text;

    int rotations1;

    // Use this for initialization
    void Start()
    {
        rotations1 = Mathf.RoundToInt(transform.localEulerAngles.y);
        if (!sp.IsOpen)
        {
            sp.Open();
            sp.ReadTimeout = 1;
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (!sp.IsOpen)
        {
            sp.Open();
            sp.ReadTimeout = 1;
        }

              sp.WriteLine(rotations1.ToString());
             text.text = "Cords: " + (this.transform.localEulerAngles.y.ToString());
        print (sp.ReadLine());
    }

    private void OnApplicationQuit()
    {
        sp.Write(0.ToString());
        sp.Close();
    }

Arduino

float RotationX;
float RotationY;

#include <Servo.h>
Servo servo1;

void setup() 
{
 servo1.attach(8);
 Serial.begin(9600);
}

void loop()
{

  RotationY = Serial.read();
  Serial.println(RotationY);

}

Serial.read() only read 1 byte at a time. And a float is more than 1 byte.

What you should do is: read the "whole" float as string then convert it to float (since you are sending it as string also).

The serial input basics tutorial might have helpful information.

Not to be rude but I’m a beginner could you explain a bit more? Thank you

Little_Tinkers:
Not to be rude but I’m a beginner could you explain a bit more? Thank you

If you tell us what part of Serial Input Basics you don't understand I will try to help.

...R

Robin2:
If you tell us what part of Serial Input Basics you don't understand I will try to help.

...R

Okay I Read That Whole Thing And I Think I Got The Arduino Side of Things Working But The Unity Side Isn't

Working?

I'm Sorry I am having problems

Post your new code

Arduino Code

#include <Servo.h>
Servo servo1;

// Example 5 - Receive with start- and end-markers combined with parsing

const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];        // temporary array for use when parsing

      // variables to hold the parsed data
float floatFromPC = 0.0;

boolean newData = false;

//============

void setup() {
    Serial.begin(9600);
  
servo1.attach(8);
}

//============

void loop() {
    recvWithStartEndMarkers();
    if (newData == true) {
        strcpy(tempChars, receivedChars);
            // this temporary copy is necessary to protect the original data
            //   because strtok() used in parseData() replaces the commas with \0
        parseData();
        showParsedData();
        newData = false;
           
    }
}

//============

void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;

    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }

        else if (rc == startMarker) {
            recvInProgress = true;
        }
    }
}

//============

void parseData() {      // split the data into its parts

    char * strtokIndx; // this is used by strtok() as an index
    
    floatFromPC = atof(strtokIndx); 
     Serial.println(floatFromPC);
     // convert this part to a float

}

//============

void showParsedData() {
    
    Serial.print("Float ");
    Serial.println(floatFromPC);
    servo1.write(floatFromPC);
    servo1.attach(8);
}

Little_Tinkers:
Okay I Read That Whole Thing And I Think I Got The Arduino Side of Things Working But The Unity Side Isn't
Working?

I know nothing about Unity.

What output are you getting from your Arduino program? Maybe try example 3 from my tutorial before trying to parse anything.

Post some examples of the messages that Unity is sending.

...R

This Is What Unity Is Sending To Arduino

    SerialPort sp = new SerialPort("COM2",9600);

    [Range(0, 360)] public float servo;

     sp.WriteLine(servo.ToString());
sp.WriteLine(servo.ToString());

Where are the start and end marker that the Arduino is expecting?

Okay Now It is Working But the servo motor is not working?

Little_Tinkers:
This Is What Unity Is Sending To Arduino

What you have posted is the code that sends the message but what I would like to see are some examples of the message - i.e. the stuff that reaches the Arduino.

That code snippet looks like it might open the Serial port every time it wants to send a message. That won't work with an Uno or Mega as opening the port causes them to reset.

...R

    servo1.write(floatFromPC);
    servo1.attach(8);

The write() method doesn't take a float. Why are you trying to move the servo before attaching it?

How IS the servo connected to the Arduino? What is powering the servo?

Robin2:
Uno or Mega as opening the port causes them to reset.

I am using The Sparkfun Redboard

PaulS:

    servo1.write(floatFromPC);

servo1.attach(8);



The write() method doesn't take a float. Why are you trying to move the servo before attaching it?

Yea I Have The Attach Method in Setup() To But The servo works once then after that it doesn't work so I

Thought that if it attached at the end of every loop and in setup then it would work, But I was wrong

PaulS:
How IS the servo connected to the Arduino? What is powering the servo?

the servo motor is connected to PWM 8 ,5 Volt, and Ground

Okay I don't Know What I Did But It Is Working Now. Thank You For All Your Help

the servo motor is connected to PWM 8 ,5 Volt, and Ground

You should NOT be having the Arduino power the servo.