Это код платы на C++ (Примечание: я использовал библиотеку, которая позволяет мне отображать входные данные моего потенциометра в число с плавающей запятой):
Код: Выделить всё
#include ;
const int potPin = 5;
const int ledPin = 2;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(potPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
float readValue = mapFloat(analogRead(potPin), 0, 4095, -8, 8);
Serial.println(readValue);
if (Serial.available()) {
char cmd = Serial.read();
if(cmd == '1') {
digitalWrite(ledPin, 1);
}
if(cmd == '0') {
digitalWrite(ledPin, 0);
}
}
}
Код: Выделить всё
using UnityEngine;
using System.IO.Ports;
public class arduinoConnector : MonoBehaviour
{
// Start is called once before the first execution of Update after the MonoBehaviour is created
SerialPort serial = new SerialPort("COM7", 9600);
void Start()
{
serial.Open();
serial.ReadTimeout = 100;
}
// Update is called once per frame
void Update()
{
string data = serial.ReadLine();
Debug.Log(data);
float value = float.Parse(data);
transform.position = new Vector3(value, -4, 0);
}
private void OnApplicationQuit()
{
serial.Close();
}
public void turnOn()
{
serial.Write("1");
}
public void turnOff()
{
serial.Write("0");
}
}
Что я могу сделать, чтобы сделать последовательное соединение с Arduino и потенциометром более плавным?>