People counter keeps counting

Hi,
I made a visitor counter and it keeps counting constantly. How can I fix this?

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);
    
  }
 
}

The easier you make it to read and copy the code the more likely it is that you will get help

Please follow the advice given in the link below when posting code

Have to tried printing distance before you test it ?
Is it what you expect ?
Does it change when you move in front of the sensor ?

Let me get ahead and not cross, it doesn’t matter it counts on its own

looks like it increments the counted whenever is measures a distance > 60. so it continually increments whenever that condition is true.

shouldn’t it only increment on the first occurrence of something < 40 and then reset when the distance is > 60?

Hi,
You need to detect when your distance changes either increase or decrease by a fixed amount.

Tom… :grinning: :+1: :coffee: :australia:

print the value of a just after you pulseIn to determine if this function returns what you expect.

Print the value of distance just after your multiplication to learn that your condition is effectively always true.

Continuous decreasing … -1 ,-2 ,-3

That would seem to indicate that distance is always greater than 60. I don’t suppose by any chance that you have tried printing distance before testing its value as previously suggested

Please edit your code in the original post and add code tags as suggested

I did but still same

Did what, same what ?

If you have changed your code then please post the new version and explain what is wrong with it. If you added Serial.print()s as suggested then what do they show ?

Hi,
Can suggest you use the NewPing library example and make sure your Ultrasonic is working and communicating.
Nothing else in the code just the example,

// ---------------------------------------------------------------------------
// Example NewPing library sketch that does a ping about 20 times per second.
// ---------------------------------------------------------------------------

#include <NewPing.h>

#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}

void loop() {
  delay(50);                     // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  Serial.print("Ping: ");
  Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)
  Serial.println("cm");
}

You will need to change the echo and trigger pin allocations.
Also make sure you have the NewPing library loaded, if not google it.

Tom… :grinning: :+1: :coffee: :australia:

1 Like

When I remove my out of the visitor code its counting normal / true. Its not counting itselves`

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);

}`

Sorry, but I have no idea what you are trying to tell us.

Please post your complete sketch, using code tags when you do, and explain your current problem

 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);
    
  }[quote="UKHeliBob, post:15, topic:857452, full:true"]
Sorry, but I have no idea what you are trying to tell us.

Please post your complete sketch, using code tags when you do, and explain your current problem
[/quote]

When I removed the exit code, the number of people entering normally works. So it does not count by itself or decrease by itself. It works normally when it passes in front of you.

I will try again

Please post your complete sketch

I solved the problem. I changed the dictance delay.