Counting the number of distances measured in one minute

Hi Everyone , I'm new to Arduino. I'm trying to use HC-SR04 ultrasonic sensor to measure distances. I also want to count the number of distances measured in one minute. So far I have only being able to write the code to measure the distances . But as to how to count the number of changes in distances... I'm having a problem.
Any help would be gladly appreciated.

#include <LiquidCrystal.h>

#define trigger 18
#define echo 19

LiquidCrystal lcd(2,3,4,5,6,7);

float time=0,distance=0;

void setup()
{
lcd.begin(16,2);
pinMode(trigger,OUTPUT);
pinMode(echo,INPUT);
lcd.print(" Ultra sonic");
lcd.setCursor(0,1);
lcd.print("Distance Meter");
delay(2000);
lcd.clear();
lcd.print(" Project");
delay(2000);
}

void loop()
{
lcd.clear();
digitalWrite(trigger,LOW);
delayMicroseconds(2);
digitalWrite(trigger,HIGH);
delayMicroseconds(10);
digitalWrite(trigger,LOW);
delayMicroseconds(2);
time=pulseIn(echo,HIGH);
distance=time*340/20000;
lcd.clear();
lcd.print("Distance:");
lcd.print(distance);
lcd.print("cm");
lcd.setCursor(0,1);
lcd.print("Distance:");
lcd.print(distance/100);
lcd.print("m");
delay(1000);
}

Filter the ranges into a number of bins, say 10-19cm into one bin, 20-29cm into another and so on.

Please remember to use code tags when posting code

Please follow the advice given in the link below when posting code . Use code tags when posting code here to make it easier to read and copy for examination

and this

are not the same thing.

Which one is it that you want?

And please edit your post. Use code tags.

It would be great to have a button that just posts your message over and over and over and over.

It's what HID Arduinos were built for...

#include <LiquidCrystal.h>

#define trigger 18
#define echo 19

int count ;
LiquidCrystal lcd(2,3,4,5,6,7);

float time=0,distance=0;

unsigned long startMillis; unsigned long currentMillis;
const unsigned long oneMinute = 3000; //one minute

void setup()
{
lcd.begin(16,2);
pinMode(trigger,OUTPUT);
pinMode(echo,INPUT);
lcd.print(" Ultra sonic");
lcd.setCursor(0,1);
lcd.print("Distance Meter");
delay(2000);
lcd.clear();
lcd.print(" Project");
delay(2000);

startMillis = millis();

}

void loop()
{
lcd.clear();
digitalWrite(trigger,LOW);
delayMicroseconds(2);
digitalWrite(trigger,HIGH);
delayMicroseconds(10);
digitalWrite(trigger,LOW);
delayMicroseconds(2);
time=pulseIn(echo,HIGH);
distance=time*340/20000;
lcd.clear();
lcd.print("Distance:");
lcd.print(distance);
lcd.print("cm");
lcd.setCursor(0,1);
lcd.print("Changes in distances per minute:");

currentMillis = millis();
if(currentMillis - startMillis >= oneMinute){
if(currentMillis - startMillis >= oneMinute){
count++;}

}

  startMillis = currentMillis;

delay(1000);
}

@dieterzwarterz did you deliberately ignore the request to use code tags when posting code tags here ?

i honestly do not know how to do that.

Then you did not read the advice in the link I posted in reply #3

Please use code tags.

Enter the words 'code tags' in the search to find out about the code tag thingies or read post #3.

[quote="dieterzwarterz, post:7, topic:894941, full:true"]
#include <LiquidCrystal.h>

#define trigger 18
#define echo 19

int count ;
LiquidCrystal lcd(2,3,4,5,6,7);

float time=0,distance=0;

unsigned long startMillis; unsigned long currentMillis;
const unsigned long oneMinute = 3000; //one minute

void setup()
{
lcd.begin(16,2);
pinMode(trigger,OUTPUT);
pinMode(echo,INPUT);
lcd.print(" Ultra sonic");
lcd.setCursor(0,1);
lcd.print("Distance Meter");
delay(2000);
lcd.clear();
lcd.print(" Project");
delay(2000);

startMillis = millis();

}

void loop()
{
lcd.clear();
digitalWrite(trigger,LOW);
delayMicroseconds(2);
digitalWrite(trigger,HIGH);
delayMicroseconds(10);
digitalWrite(trigger,LOW);
delayMicroseconds(2);
time=pulseIn(echo,HIGH);
distance=time*340/20000;
lcd.clear();
lcd.print("Distance:");
lcd.print(distance);
lcd.print("cm");
lcd.setCursor(0,1);
lcd.print("Changes in distances per minute:");

currentMillis = millis();
if(currentMillis - startMillis >= oneMinute){
if(currentMillis - startMillis >= oneMinute){
count++;}

}

 startMillis = currentMillis;

delay(1000);
}

Can you be more specific?

You can get readings about every 30 milliseconds or about 2000 samples per minute. Even if nothing moves you are likely to get differences between readings. You will have to experiment to see how much change is enough to count.

Whew that only took 13 posts. Imagine how many postings it will take to resolve
the problem at this rate.

I suggest the OP start reading about using millis() for timing, you may need it.

1 Like

i want to be able to count the number of times the distance changed in one minute

Store a distance in a variable.
Have another variable as a counter.

if( OldDistance != NewDistance) {counter++;}
If (oneMinteHasPassed) {counter=0;}

How much will it need to change for it to be seen as significant ?

between 1 to 20

What i am actually trying to accomplish by this is to be able to measure the breathing rate of a human being using an ultrasonic sensor pointed at the chest region.

since when breathing the chest moves (forward and backwards) , i want to be able to measure the number of times there would be a change in distance in one minute when an ultrasonic sensor is placed in front of the chest.

hope this makes sense

I wouldn't rely on a human body being a very good reflector of ultrasound.