#include <Wire.h>
#include <LiquidCrystal_I2C.h>
int ledRed = 10;
int ledGreen = 11;
int signal = 2;
int pirState = LOW;
int value = 0;
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
// put your setup code here, to run once:
pinMode(ledRed, OUTPUT);
pinMode(ledGreen, OUTPUT);
pinMode(signal, INPUT);
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Ready");
delay(700);
lcd.clear();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Motion detected:");
lcd.setCursor(3,1);
lcd.print("FALSE");
}
void loop() {
// put your main code here, to run repeatedly:
value = digitalRead(signal);
if (value == HIGH) {
digitalWrite(ledRed, HIGH);
digitalWrite(ledGreen, LOW);
if (pirState == LOW) {
Serial.println("Motion detected!");
lcd.clear();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Motion detected:");
lcd.setCursor(3,1);
lcd.print("TRUE");
pirState = HIGH;
}
}else {
digitalWrite(ledRed, LOW);
digitalWrite(ledGreen, HIGH);
if (pirState == HIGH) {
Serial.println("Motion ended");
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Motion detected:");
lcd.setCursor(3,1);
lcd.print("FALSE");
pirState = LOW;
}
}
}