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