Arduino - Google Speech API + Android APP + 伺服馬達(Servomotor) + FA-130馬達(motor) + 藍芽接收器( Bluetooth Receiver) + LED
目的:
透過Android APP + Google Speech API,用聲控模擬簡易的智慧家電。
APP介面:
成品:
實際影片:
Arduino Code:
#include < SoftwareSerial.h > #include < Wire.h > //引用二個函式庫SoftwareSerial及Wire SoftwareSerial #include < Servo.h > SoftwareSerial I2CBT(10, 11); //定義PIN10及PIN11分別為RX及TX腳位 Servo myservo; // 建立Servo物件,控制伺服馬達 int lightsize; //LED亮度 int insize; //接收的訊息長度 int openlight; int hasLast, temp; void setup() { Serial.begin(9600); I2CBT.begin(9600); //藍牙鮑率57600(注意!此鮑率每個藍牙晶片不一定相同,請先確認完再填寫進去) pinMode(6, OUTPUT); //設定 pin6 為輸出,LED就接在這 pinMode(5, OUTPUT); //馬達腳位5 openlight = 0; hasLast = 0; myservo.attach(9); myservo.write(20); // 一開始先置中0度 } void loop() { byte cmmd[20]; while (1) { if ((insize = (I2CBT.available())) > 0) //讀取藍芽訊息 { Serial.print("input size = "); //判斷有沒有訊息接收 Serial.println(insize); //顯示接收多少訊息 for (int i = 0; i < insize; i++) { Serial.print(cmmd[i] = char(I2CBT.read())); //將接收的訊息顯示出來 Serial.println(" "); } } if (insize == 1 && cmmd[0] >= 48 && cmmd[0] <= 57) { if (hasLast == 0) //判斷LED亮度大小的值 { hasLast = 1; temp = cmmd[0] - 48; continue; } else { hasLast = 0; lightsize = cmmd[0] - 48 + temp * 10; } } if (insize >= 2) { //判斷LED亮度大小的值 lightsize = (cmmd[0] - 48) * 10 + cmmd[1] - 48; } //analogWrite(13,map(lightsize,0,100,0,255)); //對應0~100 <-> 0~255 switch (cmmd[0]) { //讀取第一個字(如果讀到的指令是字母) case 97: //97為"a"的ASCII CODE 點亮LED Serial.println("Turn On LED"); openlight = 1; analogWrite(6, map(lightsize, 0, 100, 0, 255)); break; case 98: //98為"b"的ASCII CODE 熄滅LED Serial.println("Turn Off LED"); openlight = 0; analogWrite(6, 0); break; case 99: //99為"c"的ASCII CODE 開啟風扇 Serial.println("Turn On FAN"); analogWrite(5, 50); break; case 100: //100為"d"的ASCII CODE 關閉風扇 Serial.println("Turn Off FAN"); analogWrite(5, 0); break; case 101: //101為"e"的ASCII CODE 開啟窗簾 Serial.println("Turn On Curtain"); for (int i = 20; i <= 90; i += 1) { myservo.write(i); delay(20); } break; case 102: //102為"f"的ASCII CODE 關閉窗簾 Serial.println("Turn Off Curtain"); for (int i = 90; i >= 20; i -= 1) { myservo.write(i); // 直接以脈衝寬度控制 delay(20); } break; default: if (openlight == 1) analogWrite(6, map(lightsize, 0, 100, 0, 255)); break; } // Serial.println(lightsize); } }
留言
張貼留言