2016年3月21日 星期一

C# 控制 Arduino 2個LED

C#
加入
using System.IO.Ports;
程式碼
        private void Form1_Load(object sender, EventArgs e)
        {
            serialPort1.PortName = "COM10";
            serialPort1.BaudRate = 9600;
            serialPort1.Parity = Parity.None;
            serialPort1.DataBits = 8;
            serialPort1.StopBits = StopBits.One;
            button1.Enabled = true;
            button2.Enabled = false;
            button3.Enabled = false;
            button4.Enabled = false;
            button5.Enabled = false;
            button6.Enabled = false;
           
            label1.Text="斷線中";
        }
        private void button1_Click(object sender, EventArgs e)
        {
            serialPort1.Open();
            button1.Enabled = false;
            button2.Enabled = true;
            button3.Enabled = true;
            button4.Enabled = false;
            button5.Enabled = true;
            button6.Enabled = false;
            label1.Text = "連線中";
        }
        private void button2_Click(object sender, EventArgs e)
        {
            serialPort1.Write("b");
            serialPort1.Write("d");
            serialPort1.Close();
            button1.Enabled = true;
            button2.Enabled = false;
            button3.Enabled = false;
            button4.Enabled = false;
            button5.Enabled = false;
            button6.Enabled = false;
            label1.Text = "斷線中";
        }
        private void button3_Click(object sender, EventArgs e)
        {
            serialPort1.Write("a");
            button1.Enabled = false;
            button2.Enabled = true;
            button3.Enabled = false;
            button4.Enabled = true;
            label1.Text = "LED1 ON";
        }
        private void button4_Click(object sender, EventArgs e)
        {
            serialPort1.Write("b");
            button1.Enabled = false;
            button2.Enabled = true;
            button3.Enabled = true;
            button4.Enabled = false;
            label1.Text = "LED1 OFF";
        }
        private void button5_Click(object sender, EventArgs e)
        {
            serialPort1.Write("c");
            button1.Enabled = false;
            button2.Enabled = true;
            button5.Enabled = false;
            button6.Enabled = true;
            label1.Text = "LED2 ON";
        }
        private void button6_Click(object sender, EventArgs e)
        {
            serialPort1.Write("d");
            button1.Enabled = false;
            button2.Enabled = true;
            button5.Enabled = true;
            button6.Enabled = false;
            label1.Text = "LED2 OFF";
        }


Arduino程式
void setup() {
    Serial.begin(9600);     // 開啟 Serial port, 通訊速率為 9600 bps
    // 初始化 LED 接腳
   pinMode(12, OUTPUT);
   pinMode(11, OUTPUT);
 }       
 void loop() {
    // 檢查是否有資料可供讀取
   if (Serial.available() > 0) {
      // 讀取進來的 byte
      int inByte = Serial.read();
      // 根據收到的字元決定要打開或關掉 LED
      switch (inByte) {
      case 'b':    
        digitalWrite(12, LOW);
        break;
      case 'a': 
        digitalWrite(12, HIGH);
        break;
      case 'd':    
        digitalWrite(11, LOW);
        break;
      case 'c': 
        digitalWrite(11, HIGH);
        break; 
      default:
        // 關掉所有的 LED
        digitalWrite(12, LOW);
      }
    } 
 }