02雙A開關燈

App Inventor端的程式

Arduino 端的程式

參考http://swf.com.tw/?p=712

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

#include <SoftwareSerial.h> // 引用程式庫

// 定義連接藍牙模組的序列埠

SoftwareSerial BT(8, 9); // 接收腳(RX), 傳送腳(TX)

char val; // 儲存接收資料的變數

void setup() {

Serial.begin(9600); // 與電腦序列埠連線

Serial.println("BT is ready!");

// 設定藍牙模組的連線速率

// 如果是HC-05,請改成38400

BT.begin(9600);

}

void loop() {

// 若收到「序列埠監控視窗」的資料,則送到藍牙模組

if (Serial.available()) {

val = Serial.read();

BT.print(val);

}

// 若收到藍牙模組的資料,則送到「序列埠監控視窗」

if (BT.available()) {

val = BT.read();

Serial.print(val);

}

}

參考俊青寫法:

#include <SoftwareSerial.h>

SoftwareSerial BT(2,3); // 接收腳(RX), 傳送腳(TX);接HC-06之TXD、RXD;先不要用0,1,因為USB用

int bluepin = 13; //select the pin for the blue LED

char val;

void setup()

{

Serial.begin(9600); //for arduino serial port mointor

BT.begin(9600);

BT.print("BT is ready!"); //這條可有可無,不影響!上方是用Serial.println("BT is ready!");

pinMode(bluepin, OUTPUT);

}

void loop()

{

if(BT.available() ) {

val=BT.read();

Serial.println(val);

switch (val) {

case 'o' :

open();

break;

case 'c' :

close();

break;

}

}

}

void open(){

digitalWrite(13, HIGH);//亮燈

}

void close(){

digitalWrite(13, LOW);//關燈

} //blue LED