---
# System prepended metadata

title: 'test: 藍芽控制'

---

<h1><i>test:</i> 藍芽控制</h1>

```arduino=
#include <SoftwareSerial.h>
int ENA = 9;
int ENB = 3; 
int IN1 = 7;
int IN2 = 6;
int IN3 = 5;
int IN4 = 4;

SoftwareSerial BT(2, 8); // 接收腳, 傳送腳
char val;  // 儲存接收資料的變數
 
void setup() {
   Serial.begin(9600);   // 與電腦序列埠連線
   Serial.println("BT is ready!");
   BT.begin(9600);

   pinMode(ENA, OUTPUT);
   pinMode(ENB, OUTPUT);
   pinMode(IN1, OUTPUT);
   pinMode(IN2, OUTPUT);
   pinMode(IN3, OUTPUT);
   pinMode(IN4, OUTPUT);
}
 
void loop() {
   analogWrite (ENA, 255);
   analogWrite (ENB, 255);
    
  // 若收到「序列埠監控視窗」的資料，則送到藍牙模組
   if (BT.available()) {
      val = BT.read();
      Serial.print(val);
   if(val == 'w'){
      delay(500);
      digitalWrite(IN1, LOW);
      digitalWrite(IN2, HIGH);
      digitalWrite(IN3, LOW);
      digitalWrite(IN4, HIGH);
     }

   else if(val == 's'){
      delay(500);
      digitalWrite(IN1, HIGH);
      digitalWrite(IN2, LOW);
      digitalWrite(IN3, HIGH); 
      digitalWrite(IN4, LOW);
     }

   else{
     delay(500);
     digitalWrite(IN1,  LOW);
     digitalWrite(IN2, LOW);
     digitalWrite(IN3, LOW); 
     digitalWrite(IN4, LOW);
     }
}
  
 
  // 若收到藍牙模組的資料，則送到「序列埠監控視窗」
 /* if (BT.available()) {
    val = BT.read();
    Serial.print(val);
  }*/
}
```