Reset count by using Ultrasonic sensor

Hi, i'm new for programming. Please help me guys. For my final project year i do something counting by using Ultrasonic sensor. I need to reset counting to back after count on ultrasonic stop , i suggest it will stop after ultrasonic detect 30 second don't have any object to count. Please help where should i modified and what is it the conclusion :((((

Here my coding

#include <LiquidCrystal_I2C.h> //lcd

#define trigPin 9
#define echoPin 10
LiquidCrystal_I2C lcd(39,16,2);
int counter = 0;
int currentState = 0;
int previousState = 0;
float calories;

void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
lcd.begin(16, 2);
lcd.init(); // LCD
lcd.backlight(); // Ative LCD

}

void loop() {
delay(500);

int duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;

if (distance <= 10){
currentState = 1;
}

else {
currentState = 0;

}

delay(100);
if(currentState != previousState){
if(currentState == 1){
counter = counter + 1;
Serial.println(counter);
lcd.setCursor(0,0);
lcd.print("counter : " );
lcd.print(counter);

calories = counter * 0.99;
lcd.setCursor(0,1);
lcd.println(" clr " );
lcd.println(calories);

}
}
previousState = currentState;
}

Try posting your code again using code tags as opposed to quotes (and no italics). If the code you've included doesn't work, help us by trying to explain what it is doing wrong or not doing that it should be doing. If there are compile errors, post those as well.

Sorry, i'm here for the first time. There is no problem for my code, i just curious where should i add line for my reset the count of ultrasonic after it stop for 30 second.

here my code

#include <LiquidCrystal_I2C.h> //lcd

#define trigPin 9
#define echoPin 10
LiquidCrystal_I2C lcd(39,16,2);
int counter = 0;
int currentState = 0;
int previousState = 0;

void setup() {
 Serial.begin (9600);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 lcd.begin(16, 2);
  lcd.init();           // LCD 
   lcd.backlight();      // Ative LCD
   
}

void loop() {
 delay(500);
 
 int duration, distance;
 digitalWrite(trigPin, LOW); 
 delayMicroseconds(2); 
 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10); 
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 distance = (duration/2) / 29.1;
 
 if (distance <= 10){
 currentState = 1;
 }
 
 else {
 currentState = 0;
 
 }
 
delay(100);
if(currentState != previousState){
  
if(currentState == 1){
counter = counter + 1;
Serial.println(counter);
lcd.setCursor(0,0);
lcd.print("counter : " );
lcd.print(counter);



}
}
previousState = currentState;
}

Okay, good start with the code tags. Now we just need a bit of work on formatting.

#include <LiquidCrystal_I2C.h> //lcd

#define trigPin 9
#define echoPin 10

LiquidCrystal_I2C lcd(39,16,2);
int counter = 0;
int currentState = 0;
int previousState = 0;

void setup() 
{
    Serial.begin (9600);
    pinMode(trigPin, OUTPUT);
    pinMode(echoPin, INPUT);
    lcd.begin(16, 2);
    lcd.init();           // LCD
    lcd.backlight();      // Ative LCD
}

void loop() 
{
    delay(500);

    int duration, distance;
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    duration = pulseIn(echoPin, HIGH);
    distance = (duration/2) / 29.1;

    if (distance <= 10)
    {
        currentState = 1;
    }
    else 
    {
        currentState = 0;
    }

    delay(100);
    if(currentState != previousState)
    {
        if(currentState == 1)
        {
            counter = counter + 1;
            Serial.println(counter);
            lcd.setCursor(0,0);
            lcd.print("counter : " );
            lcd.print(counter);
        }
    }
    previousState = currentState;
}

Let's work with this version.

Thankyou for the formating the coding:) but it still not reset count to 0 after 30 second no object count

The key words you used - after 30 seconds no count (or something like that). Each time you count (counter = counter + 1) you'll want to reset your timer. This is often done by setting an unsigned long variable, call it start_time (or some other catchy name) to the current value of millis(). Then, while currentState is equal to previousState, and millis() keeps on ticking, insert another test (doesn't really matter where) to see if the current value of millis() is 30 seconds bigger than the last value you saved as start_time. If so, do all your reset stuff.

Kids these days. No patience.

Thankyou for the reply :slight_smile:

Have a nice day

The basic purpose of this Forum is to help you understand how software affects the Arduino hardware. This is not a place to come to be taught how to program in C/C++. There is an assumed level of understanding upon entry, otherwise there are particularly good forums available to assist in that regard.
There are some, though not many, who enjoy writing code for others. Most however (myself included) prefer to offer direction. Hopefully, if you follow that direction, you will arrive with a fuller understanding of what your code is doing and why it now works where before it didn't.
This an English language forum and as much as we try to accommodate other languages, some attempts are better not made, Where there is misunderstanding, we try to work it out. That's general.

@Alis98, I tried in #5 to explain what you need to do. Maybe a bit more? Every time your sensor causes you to record an event you enter the if statement where currentState == 1. At the same time that you increment the counter, record the time: current_time = millis();. This value will only be updated when the counter gets incremented, so when the counter is idle, millis() will continue timing. So, put another if statement in to test (millis() - current_time) > 30000;, which subtracts the last updated time at which the count was incremented, from the 'now' value of millis() and if greater than 30000 (30 seconds), reset your counter to zero and do whatever else you need to do.

Okay , understand it, after read and noticed the pinned forum abt milis. Thankyou very much for your help and kind of people.