arduinoCtrl.cs
 1  // SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries
 2  //
 3  // SPDX-License-Identifier: MIT
 4  
 5  using System.Collections;
 6  using System.Collections.Generic;
 7  using System.IO.Ports;
 8  using UnityEngine;
 9  using UnityEngine.UI;
10  
11  public class arduinoCtrl : MonoBehaviour
12  {
13      // replace with your board's COM port
14      SerialPort stream = new SerialPort("COM52", 9600);
15  
16      public Transform t;
17  
18      void Start()
19      {
20          stream.Open();
21      }
22  
23      void Update()
24      {
25          Vector3 lastData = Vector3.zero;
26  
27          string UnSplitData = stream.ReadLine();
28          print(UnSplitData);
29          string[] SplitData = UnSplitData.Split('|');
30  
31          float AccX = float.Parse(SplitData[1]);
32          float AccY = float.Parse(SplitData[2]);
33          float AccZ = float.Parse(SplitData[3]);
34  
35          lastData = new Vector3(AccX, AccY, AccZ);
36  
37          t.transform.rotation = Quaternion.Slerp(t.transform.rotation, Quaternion.Euler(lastData), Time.deltaTime * 2f);
38  
39      }
40  }