People counter on ultrasonic sensor

There is a task: physical count of people entering through the door.

Using Arduino UNO + HC-SR04 + Lcd keypad shield from DFrobot

sketch:

#include "DHT.h"
#include <LiquidCrystal.h>
#define echoPin 2 // Echo Pin
#define trigPin 3 // Trigger Pin
#define LEDPin 13 // Onboard LED
int maximumRange = 500; // Maximum range needed
int minimumRange = 50; // Minimum range needed
long duration, distance; // Duration used to calculate distance
int incrementState = 0; //variable that will read the increment button (either HIGH or LOW)
int decrementState = 0; //variable that will read the decrement button (either HIGH or LOW)
int counter = 0; //variable that will store the count
int lastIncrementState = 0;
int lastDecrementState = 0;
int currentState = 0;
int previousState = 0;
 
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
 
 
void setup() {
	Serial.begin(9600);
	lcd.begin(5, 1);
        pinMode(trigPin, OUTPUT);
        pinMode(echoPin, INPUT);
        pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
}
void loop() {
	/* The following trigPin/echoPin cycle is used to determine the
 distance of the nearest object by bouncing soundwaves off of it. */ 
 lcd.print(counter); //print it on serial monitor
 
 digitalWrite(trigPin, LOW); 
 delayMicroseconds(2); 
 
 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10); 
 
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 
 //Calculate the distance (in cm) based on the speed of sound.
 distance = duration/58.2;
 
 if (distance <= minimumRange){
 /* Send a negative number to computer and Turn LED ON 
 to indicate "out of range" */
    lcd.setCursor(0,0);
     Serial.println("-1");
 digitalWrite(LEDPin, HIGH); 
 currentState = 1;
 }
 else {
 /* Send the distance to the computer using Serial protocol, and
 turn LED OFF to indicate successful reading. */
 Serial.println(distance);
 digitalWrite(LEDPin, LOW);
   currentState = 0;
 }
 if(currentState != previousState){
if(currentState == 1){
counter = counter + 1;
Serial.println(counter);
}
}
previousState = currentState;
 //Delay 50ms before next reading.
 delay(500);
}

during operation, the following occurs:

on the screen duplicated numbers to the end. what am I doing wrong?

Error for ultrasonic + lcd people counter.zip (1.26 KB)

cooperkz: lcd.print(counter); //print it on serial monitor

i think that line isn't printing to the serial monitor. it is printing to your lcd.

if you want to print to the serial monitor, use:

  Serial.println(counter); //will print "counter" into new lines on the serial monitor

if you do want to print to the lcd at that place in your code, than you could set the cursor before doing it (so it always prints on the same place, and not values one after the other)

  lcd.setCursor(0,0);   // or whatever other place on the lcd that you want it to print
  lcd.print(counter);

good luck! ;)

Walk through your code on paper noting the value of counter each time through the loop() function. What will happen if you print it each time and don't tell the lcd which position to print it at ?

You should at least position the lcd cursor before printing counter and preferably only print it if it has changed to avoid the lcd flickering. Who knows, you may even get extra marks for that.

boguz:
good luck! :wink:

Thank you for your help. Was like to work with. But I will test :slight_smile:

#include "DHT.h"
#include <LiquidCrystal.h>
#define echoPin 2 // Echo Pin
#define trigPin 3 // Trigger Pin
#define LEDPin 13 // Onboard LED
int maximumRange = 1000; // Maximum range needed
int minimumRange = 100; // Minimum range needed
long duration, distance; // Duration used to calculate distance
int incrementState = 0; //variable that will read the increment button (either HIGH or LOW)
int decrementState = 0; //variable that will read the decrement button (either HIGH or LOW)
int counter = 0; //variable that will store the count
int lastIncrementState = 0;
int lastDecrementState = 0;
int currentState = 0;
int previousState = 0;
 
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
 
 
void setup() {
	Serial.begin(9600);
	lcd.begin(5, 1);
        pinMode(trigPin, OUTPUT);
        pinMode(echoPin, INPUT);
        pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
}
void loop() {
	/* The following trigPin/echoPin cycle is used to determine the
 distance of the nearest object by bouncing soundwaves off of it. */ 
 lcd.setCursor(0,0);
 lcd.print(counter  ); //print it on serial monitor
 
 digitalWrite(trigPin, LOW); 
 delayMicroseconds(50); 
 
 digitalWrite(trigPin, HIGH);
 delayMicroseconds(100); 
 
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 
 //Calculate the distance (in cm) based on the speed of sound.
 distance = duration/58.2;
 
 if (distance >= maximumRange || distance <= minimumRange){
 /* Send a negative number to computer and Turn LED ON 
 to indicate "out of range" */
    lcd.setCursor(0,0);
     Serial.println("-1");
 digitalWrite(LEDPin, HIGH); 
 currentState = 1;
 }
 else {
 /* Send the distance to the computer using Serial protocol, and
 turn LED OFF to indicate successful reading. */
 Serial.println(distance);
 digitalWrite(LEDPin, LOW);
   currentState = 0;
 }
 if(currentState != previousState){
if(currentState == 1){
counter = counter + 1;
lcd.setCursor(0,0);
lcd.println(counter  );
}
}
previousState = currentState;
 //Delay 50ms before next reading.
 delay(200);
}
 //Delay 50ms before next reading.
 delay(200);

One of those lines is wrong. I will leave it to you to decide which one.

Can you post the schematic you used with this code? I'm trying to connect the same components together for other experiments. Thanks.

Please share the circuit diagram.