// 2018/6/25 // Faya-Nugget Sample Code (BluetoothRemote_1C.ino) // 單元: 創意組合 - 藍芽遙控車 (使用藍芽手機控制) // 網址: https://fayalab.blogspot.com/2018/06/BluetoothBotAndroid.html // 目標: (1) Android藍芽介面控制藍芽遙控車 // (2) App控制介面 [Ghost Remote BT] // Wire : Arduino ==> faya Module // D11 ==> SIG (減速馬達) // D12 ==> DIR (減速馬達) // D6 ==> SIG (減速馬達) // D7 ==> DIR (減速馬達) // D3 ==> TxD (藍芽模組) // D2 ==> RxD (藍芽模組) #include // 引入軟體模擬串列埠的函式庫,用來連接藍芽模組 #define delayTime 250 // define delay time // define module pins connect to Arduino #define Wheel_R_DIR 12 // 右輪方向腳位 #define Wheel_R_SIG 11 // 右輪速度腳位 #define Wheel_L_DIR 7 // 左輪方向腳位 #define Wheel_L_SIG 6 // 左輪速度腳位 const int TX = 2; // 藍芽模組 RxD 埠連接到Arduino第2腳 const int RX = 3; // 藍芽模組 TxD 埠連接到Arduino第3腳 // 命名並且設定 SoftwareSerial 參數 SoftwareSerial fayaBT(RX, TX); String command; // 儲存藍芽控制字串 void setup() { Serial.begin(9600); // 串列埠通訊 baudrate (電腦 <=> Arduino) fayaBT.begin(9600); // 軟體串列埠通訊 baudrate (Arduino <=> 藍芽模組) pinMode(Wheel_R_DIR,OUTPUT); //右輪方向腳位 pinMode(Wheel_R_SIG,OUTPUT); //右輪速度腳位 pinMode(Wheel_L_DIR, OUTPUT); //左輪方向腳位 pinMode(Wheel_L_SIG, OUTPUT); //右輪速度腳位 StopMove(); //停止前進 } void loop() { if(fayaBT.available()>0) // 有資料時 { char command = fayaBT.read(); // 讀取指令 Serial.println(command); // 列印指令 if(command == 'S') // 指令 'S' = StopMove (停止前進) { StopMove(); delay(delayTime); } else if(command == 'F') // 指令 'F' = Move Foraward (向前進) { Forward(); delay(delayTime); } else if(command == 'B') // 指令 'B' = Move Backward (向後退) { Backward(); delay(delayTime); } else if(command == 'R') // 指令 'R' = Turn Right (右轉) { turnRight(); delay(delayTime); } else if(command =='L') // 指令 'L' = Turn Left (左轉) { turnLeft(); delay(delayTime); } } } void Forward() //向前進副程式 { digitalWrite(Wheel_R_DIR,LOW); analogWrite(Wheel_R_SIG,150); // 修改數值讓前進速度一致 digitalWrite(Wheel_L_DIR,HIGH); analogWrite(Wheel_L_SIG,150); // 修改數值讓前進速度一致 Serial.println("Forward"); } void Backward() //向後退副程式 { digitalWrite(Wheel_R_DIR,HIGH); analogWrite(Wheel_R_SIG,150); // 修改數值讓後退速度一致 digitalWrite(Wheel_L_DIR,LOW); analogWrite(Wheel_L_SIG,150); // 修改數值讓後退速度一致 Serial.println("Back"); } void turnRight() //向右轉副程式 { digitalWrite(Wheel_R_DIR,HIGH); analogWrite(Wheel_R_SIG,120); digitalWrite(Wheel_L_DIR,HIGH); analogWrite(Wheel_L_SIG,120); Serial.println("Turnright"); } void turnLeft() //向左轉副程式 { digitalWrite(Wheel_R_DIR,LOW); analogWrite(Wheel_R_SIG,120); digitalWrite(Wheel_L_DIR,LOW); analogWrite(Wheel_L_SIG,120); Serial.println("Turnleft"); } void StopMove() //停止副程式 { analogWrite(Wheel_R_SIG,0); analogWrite(Wheel_L_SIG,0); Serial.println("Stopmove"); }