#include <Wire.h> // ไลบรารีสำหรับ LCD I2C
#include <LiquidCrystal_I2C.h>
#define PIR_PIN 15 // ขาเชื่อมต่อกับ PIR motion sensor
LiquidCrystal_I2C lcd(0x27, 16, 2); // 0x27 คือ address ของ LCD 1602 I2C, 16 และ 2 คือขนาดของ LCD (16x2)
int motionCount = 0; // ตัวแปรเก็บจำนวนการตรวจจับการเคลื่อนไหว
int ledPin= 2; //on board led
int inputPin= 15; //from PIR
void setup(){
Serial.begin(115200);
lcd.init();
lcd.begin(16, 2);
lcd.backlight();
lcd.setCursor(0, 1);
lcd.print("Count No. = 0");
pinMode(inputPin, INPUT);
pinMode(ledPin, OUTPUT);
delay(4000);
}
void loop(){
int value= digitalRead(inputPin);
if (value == HIGH)
{
delay(2000);
motionCount++;
//if (motionCount == 11) {
// motionCount = 1;
//}
updateLCD();
digitalWrite(ledPin, HIGH);
Serial.println("Motion Detect !");
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Motion Detect !");
delay(2000) ;
}
else
{
digitalWrite(ledPin, LOW);
Serial.println("NO motion detect");
lcd.setBacklight(LOW);
lcd.clear();
}
delay(1000);
}
void updateLCD() {
lcd.setCursor(0, 1);
lcd.print(" "); // ล้างเลขเก่า
lcd.setCursor(0, 1);
lcd.print("Count No. = ");
lcd.print(motionCount); // แสดงจำนวนการตรวจจับการเคลื่อนไหวใหม่
}