#include <Adafruit_NeoPixel.h>
//#define LED_PIN 2
#define BUTTON_PIN 16
#define PIR_PIN 12
#define LED_PIN 5 // WS2812 LED가 연결된 핀
#define NUM_LEDS 1 // WS2812 LED의 개수
// WS2812 LED 객체 생성
Adafruit_NeoPixel strip(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);
// 버튼 상태를 추적하는 변수
volatile bool buttonState = false;
void buttonInterrupt();
int lastButtonState = LOW;
int ledState = LOW;
int pir_val = 0;
int btn_val = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
//pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT); // Set LED pin as output
pinMode(BUTTON_PIN, INPUT); // Set pushbutton pin as input
pinMode(PIR_PIN, INPUT);
// WS2812 LED 초기화
strip.begin();
strip.show(); // 모든 LED를 초기화하여 끄기
// 버튼 핀에 인터럽트를 연결하여 눌려지는 것을 감지
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), buttonInterrupt, CHANGE);
}
void loop() {
pir_val = digitalRead(PIR_PIN);
btn_val = digitalRead(BUTTON_PIN);
if (pir_val == HIGH or btn_val) { // check if the sensor is HIGH
digitalWrite(LED_PIN, HIGH); // turn LED ON
delay(100); // delay 100 milliseconds
if (ledState == LOW) {
Serial.println("Motion detected!");
ledState = HIGH; // update variable state to HIGH
}
} else {
digitalWrite(LED_PIN, LOW); // turn LED OFF
delay(200); // delay 200 milliseconds
if (ledState == HIGH){
Serial.println("Motion stopped!");
ledState = LOW; // update variable state to LOW
}
}
if (buttonState) {
// 버튼이 눌린 경우
// 랜덤한 색을 생성하여 WS2812 LED에 표시
int red = random(256);
int green = random(256);
int blue = random(256);
strip.setPixelColor(0, strip.Color(red, green, blue)); // 첫 번째 LED에 색 설정
strip.show(); // 변경된 색상을 표시
delay(100); // 잠시 대기
buttonState = false; // 버튼 상태를 초기화
}
}