Arduino程式碼如下
int val;
int encoder0PinA = 3;
int encoder0PinB = 4;
int encoder0Pos = 0;
int encoder0PinALast = LOW;
int n = LOW;
void setup() {
pinMode (encoder0PinA,INPUT);
pinMode (encoder0PinB,INPUT);
Serial.begin (9600);
}
void loop() {
n = digitalRead(encoder0PinA);
if ((encoder0PinALast == LOW) && (n == HIGH)) {//上升沿
if (digitalRead(encoder0PinB) == LOW) {
encoder0Pos--;
} else {
encoder0Pos++;
}
Serial.println (encoder0Pos);
}
encoder0PinALast = n;
}
而Unity的程式碼
using UnityEngine;
using System.IO.Ports;
public class Rotary : MonoBehaviour {
SerialPort serialPort = new SerialPort();
// Use this for initialization
void Start () {
serialPort.PortName = "COM3";
serialPort.BaudRate = 9600;
serialPort.Open();
}
}
而Unity執行結果如下
上網爬文了很多人的寫法,可是結果依然存取被拒
有高手可以幫幫忙嗎~~~~謝謝!!!!!
有兩個方向可以去檢查
你有開Serial Monitor嗎?
如果有開啟的話,Serial Port會被占用,
會導致存取被拒。
檢查裝置管理員的Arduino (COM port)是否選對