// Unity 程式碼:
using UnityEngine;
using System.Collections;
using System.IO.Ports;
using System.Threading;
public class Test : MonoBehaviour {
void Start () {
new Thread (Open).Start();
}
SerialPort sp;
void Open(){
sp = new SerialPort("COM3", 9600);
sp.Open();
print ("Open");
}
void OnGUI(){
if(GUI.Button(new Rect(10, 10, 100, 50), "ON")){
sp.WriteLine("y");
}
if(GUI.Button(new Rect(10, 100, 100, 50), "OFF")){
sp.WriteLine("n");
}
}
void OnApplicationQuit(){
sp.Close();
print ("Close");
}
}
// Arduino 程式碼:
void setup(){
Serial.begin(9600);
pinMode(12, OUTPUT);
}
void loop(){
if (Serial.available() >0) {
char c = Serial.read();
delay(1000);
if(c == 'y'){
digitalWrite(12, HIGH);
}else if(c == 'n'){
digitalWrite(12, LOW);
}
}
}
留言列表