Count & display result every minutes

Hi, since I am newbie in Arduino I need a guideline on how to make a counting process on Arduino that will display the counting result every minutes/any fixed time and keep looping for all the time.

The idea is I am using ping sensor as the counting element. It will count how many object that pass the sensor in more than 8cm in 1 minute period.. for object that pass the sensor less than 8cm it will not be count. It will be reset after 60 seconds and start a new count for new reading in next 1 minutes and keep looping the same process.

For now, I only manage to use the coding of example of counting the push button and I change it to the ping sensor as the input without counting in certain period of time. So, I need your help to guide me how to make a count by using the timer. :)

#define echoPin 7 // Echo Pin
#define trigPin 8 // Trigger Pin
#define LEDPin 13
int val = 0; 
int counter = 0;
int currentState = 0;
int previousState = 0;
long duration, distance;
void setup() {
pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 pinMode(LEDPin, OUTPUT);
Serial.begin(9600);
}

void loop(){

digitalWrite(trigPin, LOW); 
 delayMicroseconds(2); 

 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10); 

 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);

 //Calculate the distance (in cm) based on the speed of sound.
 distance = duration/58.2;

  
val = distance; 
if (val >8) { 
digitalWrite(LEDPin, HIGH); // turn LED on
currentState = 1;
}
else {
digitalWrite(LEDPin, LOW); // turn LED off
currentState = 0;
}
if(currentState != previousState){
if(currentState == 1){
counter = counter + 1;
Serial.println(counter);
}
}
previousState = currentState;
delay(250);
}

For starters, you probably want to check for a range, rather then only greater than. This is because it’s possible to get a reading that’s way off the scale (too far to measure), if nothing is in font of the sensor.

if ( ( val > 8 ) && (val < MAX_DISTANCE) {

Set MAX_DISTANCE to the farthest an object would be in order to be eligible to count.

Then you want to use millis() to count up to 1 minute. Have a look at the Example called Blink Without Delay, to see how to do this. Once the elapsed time reaches one minute, report your count over Serial, and rest count to zero.

I have review that example but I still could not understand the relationship between that example with my project.

I would suggest using a non-blocking architecture for your sketch. The blink without delay example sketch demonstrates this approach.

Have one piece of code which is called however often you like and returns a distance measurement. Use edge detection techniques to determine when the measurement changes suggest that something has arrived or left the sensing area. The State Change Detection example shows how to do that with a simple digital input, and the code to do that with a distance measurement would be very similar. Each time an object arrives increment a global counter.

Use code similar to blink without delay to output the counter value and reset it to zero at regular intervals.

mekduino:
I have review that example but I still could not understand the relationship between that example with my project.

Sorry, this %$%& editor munged my line turning two adjacent characters into a smilyface with sunglasses.

I was suggesting changing

      if (val >8) {

      // with

      if  ( ( val  > 8 )  &&  (val < MAX_DISTANCE) {

so that you don’t get a meaningless high value back.

mekduino:
I have review that example but I still could not understand the relationship between that example with my project.

Blinkwithoutdelay is a generic template for performing an action with a certain periodicity. In the example, the action is to blink an LED. In your case, it is to output the number of objects that passed the sensor and reset the count.

There are a few basic components of your algorithm:

  1. Detect that an object has passed in front of the sensor.
  2. Keep a running count of how many objects have passed in front of the sensor.
  3. Track elapsed time since last counter reset.
  4. When elapsed time has reached a certain value, output the value and reset the counter.

I would suggest first working on part (1), and once you have a fully working algorithm that detects objects in front of the sensor, move on from there.

lar3ry:

mekduino:
I have review that example but I still could not understand the relationship between that example with my project.

Sorry, this %$%& editor munged my line turning two adjacent characters into a smilyface with sunglasses.

I was suggesting changing

      if (val >8) {

// with

if  ( ( val  > 8 )  &&  (val < MAX_DISTANCE) {



so that you don't get a meaningless high value back.

yup, I get the idea… but for displaying the results every minutes I still could not understand the ways to make it count in interval time and then reset counting after that time

yup, I get the idea.. but for displaying the results every minutes I still could not understand the ways to make it count in interval time and then reset counting after that time

Forget about the Arduino for a minute. How would YOU count the number of items in some interval? First, you need a way to determine that something needs to be counted. You have that. Second, you need to know when to start counting and when to stop counting.

When to start counting is obvious. Start right away. When to stop is pretty obvious, too. After some defined interval has passed.

So, what happens when you start counting? You start at 0. Periodically, you see if it is time to stop counting. While watching the clock with one eye, you watch for objects with the other. Now, not really, since the Arduino can't do two things at once. But, it takes only a few nanoseconds to check the clock, so you're not likely to miss an object while checking the clock.

When it is time to stop counting, do something with the count, reset the start counting time, and reset the counter.

The millis() function is the clock. An unsigned long variable holds the start counting time. The rest is trivial, as illustrated in the blink without delay example.