RX 3 블루투스
TX 2 블루투스
#include <SoftwareSerial.h>
SoftwareSerial bluetooth(2, 3); // TX, RX
#define PIR_SENSOR 7
int pre_val = LOW; // previous value
int cur_val = LOW;
bool bReady = false;
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
pinMode(PIR_SENSOR, INPUT);
bluetooth.begin(9600);
Serial.begin(19200);
Serial.println("ready!");
}
void loop() {
delay(1000);
if(bluetooth.available()) {
char cmd = bluetooth.read();
Serial.println("bluetoot !");
switch(cmd) {
case '1':
Serial.println("case 1 !");
bReady = true;
break;
case '2':
bReady = false;
break;
}
}
if(bReady) {
cur_val = digitalRead(PIR_SENSOR);
digitalWrite(LED_BUILTIN, cur_val);
if(pre_val != cur_val) {
if(cur_val == HIGH) {
Serial.println("Motion Detected");
bluetooth.write('a'); // Motion Detected
}
else {
Serial.println("Motion Ended");
bluetooth.write('b'); // Motion Ended
pre_val = cur_val;
}
}
}
}
Device > HC-06 연결
다음엔 앱 인벤터로 스마트폰 앱을 만들어 LED 를 ON/OFF 해보자.
https://brunch.co.kr/@topasvga/2022