2016年4月15日 星期五

Android 藍牙控制 4個 LED 並回傳 I/O狀態 (EX3-3)

Android 藍牙控制 4個 LED 並回傳 I/O狀態
手機畫面 : 

 控制元件畫面 :    TimerInterval  100 

初始化 : 

藍芽連接 與 斷線 : 



Timer1 控制 : 



控制LED 按鈕 : 


 Build App (QR code For .apk)




// Arduino 程式

#include <SoftwareSerial.h>
#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;
  }
   
}


沒有留言:

張貼留言

2024_09 作業3 以Node-Red 為主

 2024_09 作業3  (以Node-Red 為主  Arduino 可能需要配合修改 ) Arduino 可能需要修改的部分 1)mqtt broker  2) 主題Topic (發行 接收) 3) WIFI ssid , password const char br...