- Home /
How To Make Arduino Read More Than One Thing From Unity,how to send multiple values to Arduino Servo Motor
Hello I Am Having Trouble Sending multiple things from unity to arduino. I have two servo motors on the
arduino side and i am trying to send different inputs to both of them at the same time(if that makes sense)
i asked a question on arduino forums. they got me half way but couldn't help me anymore.
here is my code:
Arduino Code
// Example 4 - Receive a number as text and convert it to an int
const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data
#include <Servo.h>;
Servo servo1;
Servo servo2;
boolean newData = false;
int dataNumber = 0; // new for this version
int dataNumber2 = 0;
void setup() {
Serial.begin(9600);
servo1.attach(8);
servo2.attach(7);
}
void loop() {
recvWithEndMarker();
showNewNumber();
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
if (Serial.available() > 0) {
rc = Serial.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void showNewNumber() {
if (newData == true) {
dataNumber = 0;
dataNumber2 = 0;
dataNumber = atoi(receivedChars);
dataNumber2 = atoi(receivedChars); // new for this version
Serial.println(dataNumber); // new for this version
servo1.write(dataNumber);
servo2.write(dataNumber2);
newData = false;
}
}
Unity Code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO.Ports;
public class Head : MonoBehaviour {
SerialPort sp = new SerialPort("COM2",9600);
[Range(-180, 180)] public float leftAndRight;
[Range(-180, 180)] public float upAndDown;
public Text text;
int rotations1;
string x;
string y;
// 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(upAndDown.ToString());
// sp.WriteLine(leftAndRight.ToString());
sp.WriteLine(Mathf.Clamp(this.transform.localEulerAngles.y,0,180).ToString());
// sp.WriteLine((Mathf.Clamp(this.transform.localEulerAngles.x, 0, 180).ToString()));
text.text = "Cords: " + (sp.ReadLine().ToString());
print (sp.ReadLine());
}
private void OnApplicationQuit()
{
sp.WriteLine(0.ToString());
sp.Close();
}
}
,Hello So I Have an arduino and i am trying to set up multiple servo motors pleese help
here is my code
arduino [` // Example 4 - Receive a number as text and convert it to an int
const byte numChars = 32; char receivedChars[numChars]; // an array to store the received data
include ;
ervo servo1; Servo servo2;
boolean newData = false;
int dataNumber = 0; // new for this version int dataNumber2 = 0;
void setup() { Serial.begin(9600); servo1.attach(8); servo2.attach(7); }
void loop() { recvWithEndMarker(); showNewNumber(); }
void recvWithEndMarker() { static byte ndx = 0; char endMarker = '\n'; char rc;
if (Serial.available() > 0) {
rc = Serial.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void showNewNumber() { if (newData == true) {
dataNumber = 0;
dataNumber2 = 0;
dataNumber = atoi(receivedChars);
dataNumber2 = atoi(receivedChars); // new for this version
Serial.println(dataNumber); // new for this version
servo1.write(dataNumber);
servo2.write(dataNumber2);
newData = false;
}
}`]
unity using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System.IO.Ports;
public class Head : MonoBehaviour {
SerialPort sp = new SerialPort("COM2",9600);
[Range(-180, 180)] public float leftAndRight;
[Range(-180, 180)] public float upAndDown;
public Text text;
int rotations1;
string x;
string y;
// 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(upAndDown.ToString());
// sp.WriteLine(leftAndRight.ToString());
sp.WriteLine(Mathf.Clamp(this.transform.localEulerAngles.y,0,180).ToString());
// sp.WriteLine((Mathf.Clamp(this.transform.localEulerAngles.x, 0, 180).ToString()));
text.text = "Cords: " + (sp.ReadLine().ToString());
print (sp.ReadLine());
}
private void OnApplicationQuit()
{
sp.WriteLine(0.ToString());
sp.Close();
}
}