Arduino model: ESP32Wroom 32S
Unity version: 2021.3.10f1
Hi everybody, hope I am in the right place to post this.
I got the following problem as soon I implemented Arduino as input system for my game:
On the Profiler the frame rate is stable on 60fps but I get a spikes to 0 or close every 5 sec.
I share here the code I use to receive the inputs and a couple of screens of the Profiler window.
Despite the fact that the problem seem to have arised only when I introduced Arduino, disconnecting it physically or /commenting the whole code I implemented to receive it/ doesn’t prevent the frame rate issue.
Disabling all the game objects (and therefore their respective scripts?) doesn’t help eater.
Only when I open a new empty scene than I get rid of the problem, which at least proves that my unity didn’t got broken in some way.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO.Ports;
public class ArduinoReceiver : MonoBehaviour
{
SerialPort serialPort = new SerialPort("COM3", 115200);
public RT_Module RT_Module;
//private int parsedValue;
// Start is called before the first frame update
void Start()
{
if (!serialPort.IsOpen)
{
serialPort.Open();
serialPort.ReadTimeout = 50; // Timeout for reading data
}
RT_Module.GetComponent<RT_Module>();
}
// Update is called once per frame
void Update()
{
if (serialPort.IsOpen && serialPort.BytesToRead > 0)
{
try
{
string data = serialPort.ReadLine();
//Debug of the raw data received as string
//Debug.Log("Received: " + data);
//conversion of the data from arduino into string
if (int.TryParse(data, out int parsedValue))
{
//Debug.Log("Parsed Value: " + parsedValue);
}
//Sending to RT_Module script for rotation method
RT_Module.DialInteraction(parsedValue);
//Debug.Log("I just tried to ask to rotate to the RT");
}
catch (System.Exception ex)
{
//Debug.LogWarning("Error reading serial data: " + ex.Message);
}
}
}
void OnApplicationQuit()
{
if (serialPort.IsOpen)
{
serialPort.Close();
}
}
}
Please let me know if you would like to get more information. Thank you.
your very confuse Simone
Got help from Chat GPT for setting the code, but I’m proud to say that several years ago I did it by myself u.u