#include <LiquidCrystal_I2C.h>
int Pir_sensor_pin = 34; // Choose the input pin (for PIR sensor)
int ledPin = 25; // Choose the pin for the LED
int pirState = LOW; // We start, assuming no motion detected
int val = 0; // Variable for reading the pin status
int previousState = 0; // Define Previous state
int counter = 0; // Define Counter
#define I2C_ADDR 0x27 //LCD monitor I2C address (I2C configuration)
#define LCD_COLUMNS 16 //LCD monitor X axis columns = ?
#define LCD_LINES 2 //LCD monitor Y axis = ?
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES); //LCD part
void setup() {
Serial.begin(115200); //Initialize Serial port
lcd.begin(21, 22); //SDA = port D21 (data line) ,SCl = port D22 (clock time)
// you can now interact with the LCD, e.g.:
pinMode(Pir_sensor_pin, INPUT); // Declare sensor as input
lcd.init(); //Open LCD SET
lcd.backlight(); //Open LCD backlight
lcd.setBacklight(1);
delay(1000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Hello"); //Test LCD is work?
lcd.setCursor(0, 1);
lcd.print("Sam Sir!");
delay(2000);
lcd.clear(); //Clear LCD txt
lcd.setCursor(0, 0); //Set LCD txt location in line top
lcd.print("Group Project"); //LCD print txt
lcd.setCursor(0, 1); //Set LCD txt location in line down
lcd.print("People Counter");
delay(2000); //Delay 2sec (1000 = 1sec)
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Michael Angie");
lcd.setCursor(0, 1);
lcd.print("Henry Jacky");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Please press ");
lcd.setCursor(0, 1);
lcd.print("PIR motion sensor ");
delay(1500);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("To simulation ");
lcd.setCursor(0, 1);
lcd.print("People trigger ");
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(Pir_sensor_pin, INPUT); // declare sensor as input
}
void loop() {
val = digitalRead(Pir_sensor_pin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
if (pirState == LOW) {
// we have just use lcd output txt
lcd.setCursor(0, 0);
lcd.print("People Detected!");
delay(2000);
lcd.clear();
// We only want to print on the output change, not state
pirState = HIGH;
}
}
else {
digitalWrite(ledPin, LOW); // turn LED OFF
if (pirState == HIGH) {
// we have just turned of
lcd.setCursor(0, 0);
delay(2000);
// We only want to print on the output change, not state
pirState = LOW;
}
}
//detect people in +1
if(pirState != previousState){
if(pirState == 1){
counter = counter + 1;
lcd.setCursor(0,0);
lcd.print("People Count !");
lcd.setCursor(0,1);
lcd.print("= ");
lcd.print(counter);
delay(3000);
}
}
}