#include <LiquidCrystal.h> // defining the library for LCD
#define IrSensor 10 //define the pin that the IR sensor is connected to
#define LED 9 //define the pin that the LED is connected to
LiquidCrystal lcd(0, 1, 2, 3, 4, 5); //assigning of Arduino to LCD
unsigned int count = 0; // variable to count the number of visitors
int IrStatus = 0; // variable to store the value of the sensor
void setup(){
pinMode(IrSensor, INPUT);// initalize IR sensor as input
pinMode(LED, OUTPUT);// initalize LED as output
lcd.begin(16, 2); // initializing the dimensions of the LCD
lcd.setCursor(0,0); // setting cursor position to index 0,0 - Top left of LCD
lcd.print("Welcome to"); //print a message on the LCD
lcd.setCursor(1,1); //setting cursor position to index 1,1
lcd.print("visitor counter"); //print a message on the LCD
delay(2000); //wait 2 sec
lcd.clear(); //clears the LCD screen
lcd.print("People in room:"); //print a message on the LCD
lcd.setCursor(0,1); //setting cursor position to index 0,1
}
void loop(){
IrStatus=digitalRead(IrSensor);// reading the value of the sensor
if(IrStatus==HIGH){// if the sensor detects the any reflected radiation
digitalWrite(LED,HIGH);// turn on the LED
lcd.clear();
count++;// increment in the number of people
lcd.print("People in room:");
lcd.setCursor(0,1); // setting the place for the data that is to be displayed
lcd.print(count);//display the number of people in the room
delay(1000); // wait 1 sec
}
if(count<=0){ //if nobody in the room
lcd.clear();
digitalWrite(LED,LOW); // turn off the LED
lcd.print("Nobody's here :( "); //print a message on the LCD
lcd.setCursor(0,1);
delay(200); //wait 2 sec
}
else { //all other cases
digitalWrite(LED,LOW);// otherwise keep the LED turned off
}
}