Arduino Nano 藍牙小夜燈 ( Control a Lamp from Android Phone )

若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。

感謝 NFU Dream Maker 所提供的免費課程,讓筆者完成DIY的藍牙小夜燈,底下是筆者重新設計Arduino程式與Android APP兩部分的分享。

硬體材料:
1. Arduino Nano 板子 x 1
2. NFU Dream Maker 所設計的 PCB x 1
3. RGB LED x 3
4. 電阻 x 3
5. HC-05 藍牙模組 x 1

硬體電路:


紅色燈接 --> D9
綠色燈接 --> D10
藍色燈接 --> D11
上圖省略藍牙的VCC、GND接線

完成圖:

Arduino程式:
https://goo.gl/hIcj8x
int redColor = 0, greenColor = 0, blueColor = 0; // 三色LED燈顏色數值
int rLedPin = 9, gLedPin = 10, bLedPin = 11; // 三色燈在Nano上的數位腳位
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(rLedPin, OUTPUT);
pinMode(gLedPin, OUTPUT);
pinMode(bLedPin, OUTPUT);
// 此例所用的三色LED為共陽極
analogWrite(rLedPin, 255-redColor);
analogWrite(gLedPin, 255-greenColor);
analogWrite(bLedPin, 255-blueColor);
}
void loop() {
// 當資料超過 3 Bytes 時,將前 3 Bytes分別當成 Red, Green, Blue 顏色的數值(0~255)
if (Serial.available() >= 3) {
redColor = Serial.read();
greenColor = Serial.read();
blueColor = Serial.read();
analogWrite(rLedPin, 255-redColor);
analogWrite(gLedPin, 255-greenColor);
analogWrite(bLedPin, 255-blueColor);
}
}
view raw BtRGBLED.ino hosted with ❤ by GitHub

MIT App Inventor程式:
https://goo.gl/CKoxH0

Android APK 下載處:
https://goo.gl/Vdrsjl


示範影片:

沒有留言: