I want to add 1 to n if the distance is less than 20 for 5 seconds or more.
any help will help. ![]()
if (elapsedTime > 5000 && distance < 20)
{
 n++;
}
DMAG:
Its not working
. . is probably the single least-liked phrase on this forum.
That, and posted screen-shots.
When you've fixed that, there'll be the problem that "n" isn't declared either.
thanks I finally did it after your help.
#include <LiquidCrystal.h> // includes the LiquidCrystal Library
LiquidCrystal lcd(1, 2, 4, 5, 6, 7); // Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7)
const int trigPin = 12;
const int echoPin = 13;
long duration;
int distance;
int count;
#include <elapsedMillis.h>
elapsedMillis timeElapsed;
void setup() {
lcd.begin(16,2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance= duration*0.034/2;
if (distance > 20)
{ timeElapsed=0;}
if (timeElapsed > 5000 && distance < 20)
{
count++;
timeElapsed=0;
}
lcd.setCursor(0,0); // Sets the location at which subsequent text written to the LCD will be displayed
lcd.print("sjdah: "); // Prints string "Distance" on the LCD
lcd.print(count); // Prints the distance value from the sensor
delay(10);
}
I want the program to count once up if the distance is less that 20, and stop counting until the distance gets more than 20 and when it comes back again it only count up once until the distance gets more than 20.
/*
- Ultrasonic Sensor HC-SR04 and Arduino Tutorial
- Crated by Dejan Nedelkovski,
- www.HowToMechatronics.com
*/
#include <LiquidCrystal.h> // includes the LiquidCrystal Library
LiquidCrystal lcd(1, 2, 4, 5, 6, 7); // Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7)
const int trigPin = 12;
const int echoPin = 13;
long duration;
int distance;
int count;
#include <elapsedMillis.h>
elapsedMillis timeElapsed;
void setup() {
lcd.begin(16,2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance= duration*0.034/2;
if (distance > 20)
{ timeElapsed=0;}
if (timeElapsed > 5000 && distance < 20)
{
count++;
do{
timeElapsed=0;
}while(distance<20);
}
lcd.setCursor(0,0);
lcd.print("count: ");
lcd.print(count);
delay(10);
}
Hi,
Welcome to the forum.
Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.
What is your code not doing, or doing wrong?
Thanks.. Tom.. ![]()
When first in your do_loop, you never get out!
hello
I am trying to solve this problem i have. I want an ultra sonic counter to count when the distance is less than 20 and not count until the distance is more than 20 and when it comes back to less than 20 it counts again. I i am wring the code for 8 days now and i still didn't figure it out. thanks for any help.
/*
* Ultrasonic Sensor HC-SR04 and Arduino Tutorial
*
* Crated by Dejan Nedelkovski,
* www.HowToMechatronics.com
*
*/
#include <LiquidCrystal.h> // includes the LiquidCrystal Library
LiquidCrystal lcd(1, 2, 4, 5, 6, 7); // Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7)
const int trigPin = 12;
const int echoPin = 13;
long duration;
int distance;
int count;
#include <elapsedMillis.h>
elapsedMillis timeElapsed;
//////////////////////////////////////////
void setup() {
lcd.begin(16,2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
/////////////////////////////////////////
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance= duration*0.034/2;
////////////////////////////////////////////
if (distance > 20)
{ timeElapsed=0;}
if (timeElapsed > 5000 && distance < 20)
{
 count++;
while(distance<20){
 timeElapsed=0;
}
 }
 Â
//////////////////////////////////////////
lcd.setCursor(0,0);
lcd.print("count: ");
lcd.print(count);
delay(10);
///////////////////////////////////////////
}
DMAG:
while(distance<20){
timeElapsed=0;
}
The value of distance is never changed in that while loop and so the Arduino will be stuck in it forever. You need to change your program to a non-blocking system so that the ping code can continue to run constantly.
Please use Tools > Auto Format before posting code to the forum. This will make it easier for us to read and you will find the automatic indentation is helpful for spotting bugs in code.
I am just a beginner at programming and I am not aware how to make it non blocking.
Maybe something like:
static bool close = false;
if(distance < 20 && close == false)
{
 counter++;
 close = true;
}
if(distance > 20)
 close = false;
