People counter keeps counting add LEDs

Hi,
I am working with a visitor counter and how can I add LEDs to my project? For example, I want a green LED to light up when you enter, how can I do it?

int trig = 10;
int fan =8;
int echo = 9;
int led = 7;
int buzzer= 6;
int i=0;
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() 
{
   lcd.begin(16, 2);
  Serial.begin(9600);
  pinMode(trig,OUTPUT);
  pinMode(echo,INPUT);
  pinMode(led,7);
  pinMode(fan,8);
  pinMode(buzzer,OUTPUT);
  lcd.setCursor(4,0);
  lcd.print("Welcome");
  delay(2000);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Please wait.");
  delay(200);
  lcd.setCursor(0,0);
  lcd.print("Please wait..");
  delay(200);
  lcd.setCursor(0,0);
  lcd.print("Please wait...");
  delay(200);
  lcd.setCursor(0,0);
  lcd.print("Please wait....");
  delay(200);
  lcd.setCursor(0,0);
  lcd.print("Please wait.....");
  delay(200);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Please wait.");
  delay(200);
  lcd.setCursor(0,0);
  lcd.print("Please wait..");
  delay(200);
  lcd.setCursor(0,0);
  lcd.print("Please wait...");
  delay(200);
  lcd.setCursor(0,0);
  lcd.print("Please wait....");
  delay(200);
  lcd.setCursor(0,0);
  lcd.print("Please wait.....");
  delay(200);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Please wait.");
  delay(200);
  lcd.setCursor(0,0);
  lcd.print("Please wait..");
  delay(200);
  lcd.setCursor(0,0);
  lcd.print("Please wait...");
  delay(200);
  lcd.setCursor(0,0);
  lcd.print("Please wait....");
  delay(200);
  lcd.setCursor(0,0);
  lcd.print("Please wait.....");
  delay(200);
  lcd.setCursor(0,0);
  lcd.print("Please wait.....");
  delay(200);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Visitor Counter");
    lcd.setCursor(1,1);
  lcd.print("By Thunder");
  delay(2500);
  lcd.clear();
}

void loop() {
  lcd.setCursor(0,0);
  lcd.print("People visited:"); 
  digitalWrite(trig,LOW);
  delayMicroseconds(5);
 
  digitalWrite(trig,HIGH);
  delayMicroseconds(10);
 
  
  digitalWrite(trig,LOW);
 
  
  int a = pulseIn(echo,HIGH);
 
  
 int distance = a*0.034/2;
  
  

  if ( distance<40) //in
  {
  i=i+1;
    delay(500);
    digitalWrite(buzzer,HIGH);
    delay(100);
    digitalWrite(buzzer,LOW);
    delay(100);
   
     lcd.clear();
    lcd.setCursor(0,0);
  lcd.print("People visited:"); 
    lcd.setCursor(0,1);
  lcd.print(i);
    delay(400);
  
  }
   if ( distance>60) //out
  {
  i=i-1;
    delay(500);
    digitalWrite(buzzer,HIGH);
    delay(100);
    digitalWrite(buzzer,LOW);
    delay(100);
     lcd.clear();
     lcd.setCursor(0,0);
  lcd.print("People visited:"); 
    lcd.setCursor(0,1);
  lcd.print(i); 
     delay(400);
    
  }
 
}

My project is there ;
image

the above will be true presumably as long as the person is in the room (within range), not just when entering the room.

keeping track of the previous distance would allow recognizing when there is a change in distance indicating a person entering the room, incrementing a count (i++) and turning on an LED.

similar logic can be added to track a person leaving

1 Like

Other post/duplicate DELETED
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.