- 使用藍芽裝置控制Relay來開啟關閉12v的風扇。
元件︰
- BT Module x 1
- 1KΩ電阻 x 1
- 2N2222電晶體 x 1
- 1N4007 二極體 x 1
- 繼電器 DC-5v x 1
- DC 12v 風扇 x 1
接線圖︰
程式碼︰
#include <SoftwareSerial.h> #define RxD 2 #define TxD 3 SoftwareSerial blueToothSerial(RxD,TxD); const int RELAY_ON = 0x1; const int RELAY_OFF = 0x2; const int relayPin = 12; // 繼電器(Relay) const int DELAY = 300; unsigned char temp_val; unsigned char Receive_val; unsigned char len; unsigned char Buffer_temp[4]; unsigned char i; char inputBuffer[10]; void setup() { pinMode(relayPin, OUTPUT); // 把 relayPin 設置成 OUTPUT Serial.begin(9600); blueToothSerial.begin(19200); temp_val=0; len=0; } void loop() { UART_FUN(); if(temp_val==RELAY_ON) { digitalWrite(relayPin, HIGH); } else if(temp_val==RELAY_OFF) { digitalWrite(relayPin, LOW); } } void UART_FUN() { for(i=0;i<5;i++) Buffer_temp[i]=0x0; delay(100); while(blueToothSerial.available()) { Receive_val=blueToothSerial.read(); if(Receive_val==0xAA) { len=0; Buffer_temp[len++]=Receive_val; } else if(len < 5) { Buffer_temp[len++]=Receive_val; } } if(Buffer_temp[0]==0xAA && Buffer_temp[1]==0xBB && Buffer_temp[3]==0xF1) { temp_val=RELAY_ON; } else if(Buffer_temp[0]==0xAA && Buffer_temp[1]==0xBB && Buffer_temp[3]==0xF2) { temp_val=RELAY_OFF ; } }
沒有留言:
張貼留言