手機畫面 :
初始化 :
藍芽連接 與 斷線 :
Timer1 控制 :
控制LED 按鈕 :
Build App (QR code For .apk)
// Arduino 程式
#include <Wire.h>//引用二個函式庫SoftwareSerial及Wire
SoftwareSerial BT(11,10); //define serial BT RX=10, TX=11
int time = millis(); //millisceond
void setup()
{
Serial.begin(9600);
BT.begin(9600);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
delay(20);
digitalWrite(7, LOW);
digitalWrite(6, LOW);
digitalWrite(5, LOW);
digitalWrite(4, LOW);
}
void loop()
{
if(BT.available()) //received BT data
{
int LED = BT.read();
Serial.print("Received BT cmd: ");
Serial.println(LED);
if(LED == 1) //turn on LED if BT cmd is 1
digitalWrite(7, HIGH);
else if(LED == 0) //turn off LED if BT cmd is 0
digitalWrite(7, LOW);
if(LED == 3) //turn on LED if BT cmd is 1
digitalWrite(6, HIGH);
else if(LED == 2) //turn off LED if BT cmd is 0
digitalWrite(6, LOW);
if(LED == 5) //turn on LED if BT cmd is 1
digitalWrite(5, HIGH);
else if(LED == 4) //turn off LED if BT cmd is 0
digitalWrite(5, LOW);
if(LED == 7) //turn on LED if BT cmd is 1
digitalWrite(4, HIGH);
else if(LED == 6) //turn off LED if BT cmd is 0
digitalWrite(4, LOW);
}
int now_time = millis();
if((now_time - time) >= 500) //actives every 100 milliseconds
{
for (int i=0 ;i<=3 ; i++) {
switch (i) {
case 0:
{ int LED1_status = digitalRead(7); //send the status of LED to smartphone over BT
BT.write(0x10+LED1_status);
break; }
case 1:
{ int LED2_status = digitalRead(6); //send the status of LED to smartphone over BT
BT.write(0x20+LED2_status);
break; }
case 2 :
{ int LED3_status = digitalRead(5); //send the status of LED to smartphone over BT
BT.write(0x30+LED3_status);
break; }
default:
{ int LED4_status = digitalRead(4); //send the status of LED to smartphone over BT
BT.write(0x40+LED4_status);
break;
}
} //switch
} //for
//Serial.print("Send LED_status: ");
//Serial.print(LED1_status);
//Serial.print(" ");
//Serial.print(LED2_status);
//Serial.print(" ");
//Serial.print(LED3_status);
//Serial.print(" ");
//Serial.println(LED4_status);
time = now_time;
}
}
沒有留言:
張貼留言