- Home /
Long delay sending data to Arduino via Serial Communication
I am trying to power a Servo motor by clicking on buttons in Unity. It works but it takes almost a second for the data to reach Arduino...
Arduino:
#include <Servo.h>
String received;
Servo myservo;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
myservo.attach(9);
}
void loop() {
received = Serial.readString();
Serial.println(received);
if (received == "1")
{
myservo.write(180);
}
if (received == "2")
{
myservo.write(0);
}
}
Unity:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO.Ports;
public class Communication : MonoBehaviour {
SerialPort sp = new SerialPort("COM6",9600);
public void Send1 () {
sp.Open ();
sp.Write ("1");
sp.Close ();
}
public void Send2() {
sp.Open ();
sp.Write ("2");
sp.Close ();
}
}
The public functions are called by two buttons.
Is there a way to reduce the delay? Is my code wrong or something?
Edit: I noticed that the serial LED on arduino lights up almost instantaneously, and the servo works only after a while.... why?
no idea, but I think that means this is not the right forum for this. If you observed the LEDs actually reacting to your call, it must be on the Arduino side.
Your answer
![](https://koobas.hobune.stream/wayback/20220612165953im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Arduino Button Input not working 0 Answers
very long LAG in my unity game using arduino 2 Answers
Unity Arduino Communication Problem 0 Answers
ReadLine() Function Crash 0 Answers
High drawcall make delay when read variable from serial port 1 Answer