#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
int ledPin = 3; // choose the pin for the LED
int inputPin = 2; // choose the input pin (for PIR sensor)
int buzzerPin = 5;
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(buzzerPin, OUTPUT); // declare Buzzer as output
pinMode(inputPin, INPUT); // declare sensor as input
lcd.begin(16, 2);
}
void loop() {
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
lcd.setCursor(0,0);
lcd.println("Motion detected!");
digitalWrite(buzzerPin, HIGH); // Buzzer berbunyi
tone(buzzerPin, 1050);
delay(100);
digitalWrite(buzzerPin, LOW); // Buzzer mati
tone(buzzerPin, 1000);
digitalWrite(ledPin, HIGH); // turn LED ON
delay(100);
digitalWrite(ledPin, LOW); // LED mati
delay(100);
if (pirState == LOW) {
// we have just turned on
lcd.setCursor(0,1);
lcd.println("Motion detected!");
// We only want to print on the output change, not state
pirState = HIGH;
}
} else {
tone(buzzerPin, LOW);
lcd.setCursor(0,0);
lcd.println("No Motion ");
digitalWrite(ledPin, LOW); // turn LED OFF
delay(1000);
digitalWrite(ledPin, HIGH); // LED mati
delay(1000);
if (pirState == HIGH) {
// we have just turned off
lcd.setCursor(0,1);
lcd.println("Motion ended! ");
// We only want to print on the output change, not state
pirState = LOW;
}
}
delay(10); // Menunggu 150 ms sebelum pengecekan jarak berikutnya
}