程式
//利用 手機控制 8 LED 4種功能
#include <SoftwareSerial.h>
#include <Wire.h>
SoftwareSerial BT(11,10); //define serial BT RX=11, TX=10
//SoftwareSerial mySerial (rxPin, txPin);
const byte LEDPin[8] = {2,3,4,5,6,7,8,9};
int i=0 , j=0 ;
void setup()
{
for (j=0; j<8; j++)
pinMode(LEDPin[j],OUTPUT);
Serial.begin(9600);
BT.begin(9600);
}
//=============================
void loop()
{
if(BT.available()) //received BT data
{
int LED = BT.read();
Serial.print("Received BT cmd: ");
if (LED==0)
{
Serial.println(LED);
LED1_Function();
}
else if (LED==2)
{
Serial.println(LED);
LED2_Function();
}
else if (LED==4)
{
Serial.println(LED);
LED3_Function();
}
else if (LED==6)
{
Serial.println(LED);
LED4_Function();
}
} //if
} //loop
//=========================================
void LED1_Function() {
Serial.println("LED1");
for(j=0;j<=4;j++){
for (i=0; i<=8; i++)
{
digitalWrite(LEDPin[i], HIGH);
delay (50);
digitalWrite(LEDPin[i], LOW);
delay(50);
} // for i
Serial.println(j);
} //for j
BT.write(0x11);
}
//===============================
void LED2_Function() {
Serial.println("LED2");
for (j=0;j<=4;j++) {
for (i=7; i>=0 ; i--)
{
digitalWrite(LEDPin[i], HIGH);
delay (50);
digitalWrite(LEDPin[i], LOW);
delay(50);
} // for i
Serial.println(j);
} // for j
BT.write(0x21);
}
//===============================
void LED3_Function() {
for(j=0;j<=4;j++){
for(i = 0;i<=8;i++){
digitalWrite(LEDPin[i],HIGH);
delay(50);
digitalWrite(LEDPin[i],LOW);
}
for(i=8;i>=0;i--){
digitalWrite(LEDPin[i],HIGH);
delay(50);
digitalWrite(LEDPin[i],LOW) ;
}
Serial.println(j);
} //for j
BT.write(0x31);
Serial.println("LED3");
}
//===============================
void LED4_Function() {
for (j=0;j<=4;j++) {
for ( i=0; i<=8 ; i++) {
digitalWrite(LEDPin[i], HIGH);
}
delay (250);
for ( i=0; i<=8 ; i++) {
digitalWrite(LEDPin[i], LOW);
}
delay(250);
Serial.println(j);
} // for j
BT.write(0x41);
Serial.println("LED4");
}
//================================
沒有留言:
張貼留言