//Android手機APK 程式 下載點
http://www.mediafire.com/download/ttulhn593k23mno/BT_ControlArduino4LED.apk
#include <SoftwareSerial.h>
#include <Wire.h>
int LED1 = 6 ;
int LED2 = 7 ;
int LED3 = 8 ;
int LED4 = 9 ;
SoftwareSerial I2CBT(11,10); //RX=11 , TX=10
void setup() {
Serial.begin(9600);
I2CBT.begin(9600); //bluetooth baud rate
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
//set all LED OFF
digitalWrite(LED1,LOW);
digitalWrite(LED2,LOW);
digitalWrite(LED3,LOW);
digitalWrite(LED4,LOW);
}
///////////////////main///////////////////////////
void loop() {
byte cmmd[20];
int insize;
while(1){
/*
read message from bluetooth
*/
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.print("\n");
}
}
switch (cmmd[0]) {
//=====LED1=======
case 97: //"a"
Serial.println("a");
digitalWrite(LED1,HIGH);
cmmd[0]=31; //"1"
break;
case 98://"b"
Serial.println("b");
digitalWrite(LED1,LOW);
cmmd[0]=31;
break;
//=====LED2=======
case 99: //"c"
Serial.println("c");
digitalWrite(LED2,HIGH);
cmmd[0]=31;
break;
case 100://"d"
Serial.println("d");
digitalWrite(LED2,LOW);
cmmd[0]=31;
break;
//=====LED3=======
case 101: //"e"
Serial.println("e");
digitalWrite(LED3,HIGH);
cmmd[0]=31;
break;
case 102://"f"
Serial.println("f");
digitalWrite(LED3,LOW);
cmmd[0]=31;
break;
//=====LED4=======
case 103: //"g"
Serial.println("g");
digitalWrite(LED4,HIGH);
cmmd[0]=31;
break;
case 104://"h"
Serial.println("h");
digitalWrite(LED4,LOW);
cmmd[0]=31;
break;
default:
break;
} //Switch
} //while
}
沒有留言:
張貼留言