Help with my code automation

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

Using Arduino UNO + HC-SR04 + Lcd keypad shield

Is there any problem with my code?

And im newbie and I know very lil of C language

Please help with my code if I want to perform addition and subtraction when a guy enter and when a guy quit.

the code must be totally modified right?

Please assist

sketch_dec31a.ino (1.96 KB)

It compiles fine but if I were you I would make the code more readable.

Is there any problem with my code?

It is almost impossible to answer that question without actually testing the program.


Here is the code for people who don’t want to download it.

#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() {
	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); 

 digitalWrite(trigPin, HIGH);

 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" */
 digitalWrite(LEDPin, HIGH); 
 currentState = 1;
 else {
 /* Send the distance to the computer using Serial protocol, and
 turn LED OFF to indicate successful reading. */
 digitalWrite(LEDPin, LOW);
   currentState = 0;
 if(currentState != previousState){
if(currentState == 1){
counter = counter + 1;
lcd.println(counter  );
previousState = currentState;
 //Delay 50ms before next reading.