simple couting reset

Hi

I need a pulse counter and I tried this one .It works perfectly , but I want to modify the code ,

I need to reset the numbers of the pulses in specific period of time . Is that possible ?

/* Simple Pulse Counter

Counts digital pulses fed into pin 12. For reliability, the minimum pulse width is 1 millisecond.
If the signal is held high, the arduino will count it as one pulse.

*/

const int input = 12; // This is where the input is fed.
int pulse = 0; // Variable for saving pulses count.
int var = 0;

void setup(){
pinMode(input, INPUT);

Serial.begin(9600);
Serial.println(F("No pulse yet...")); // Message to send initially (no pulses detected yet).
}

void loop(){

if(digitalRead(input) > var)
{
var = 1;
pulse++;

Serial.print(pulse);
Serial.print(F(" pulse"));

// Put an "s" if the amount of pulses is greater than 1.
if(pulse > 1) {Serial.print(F("s"));}

Serial.println(F(" detected."));
}

if(digitalRead(input) == 0) {var = 0;}

delay(3); // Delay for stability.
}

thank you

It is not clear to me what you want to do but it is probably easier to try than to take the time to ask here.

Someone with multiple posts should know to use code tags and to read the locked posts at the top of this forum.

If you want code written for you, head on over to "Gigs and Collaborations" and bring your wallet.

If you want to write the code yourself, show us what you tried or at least provide a better description.

I didnt ask to write the code , asked if it is possible .

“ I need to reset the numbers of the pulses in specific period of time . Is that possible ?”

What does that actually mean?

Even “reset the numbers” is confusing.

If you want something like the counter goes back to 0 every 2 minutes then yes it’s possible.

reset the numbers of the pulses in specific period of time .

Yes.
At the required time, simply call a function, or cause -
pulse = 0;

that should do it.
Using a timer, RTC or millis() logic should be enough to get you done.

It starts with

"no pulses yet"

and after

counting

1 pulse detected
2 pulses detected
and so on

after 10sec

it resets to

"no pulses detected yet"

and if there are pulses
1 pulse detected
2 pulses detected

after 10 secs it resets again

that what I am looking for

So, setup a millis() timer, then each time it reaches 100000 mSec, set pulse to zero, and restart the timing.

lastchancename has enough zeroes for one hundred seconds. 10000 milliseconds is ten seconds.

What do you want to use to measure time and/or how accurately do you need to measure ten seconds?

By the way, the original code does not properly enforce the stated minimum pulse width.

millis() hits or skips 10000 only once in a large number of days because it keeps counting up and it cannot be reset to zero or to any other value. There is an excellent tutorial that explains how to easily deal with this.

oops, we’ll it was 3 am...!
or was that 33333 am?

Thank you guys I got it ,

but what I dont understand ,is some people comments ,
yes the coding isnt my field , and that is why I post my help . If you dont like how I put the code or the description of the problem , then just ignore it , no lectures.

Your comment is acknowledged, but for people to help, they need the best information possible, or many won’t even try because they feel you aren’t trying to help yourself...

It’s not a personal criticism unless the poster simply ignores the help being offered to address their question.

Using tags is literally as easy as pressing a button on the web forum, or typing a [word] in your text editor...

Unformatted code text can carry unexpected error.. Easy as that.

Since you are just looking for a LOW to HIGH transition this is the way I usually code it which is more readable.

/* Simple Pulse Counter

  Counts digital pulses fed into pin 12. For reliability, the minimum pulse width is 1 millisecond.
  If the signal is held high, the arduino will count it as one pulse.

*/

const int input = 12; // This is where the input is fed.
int pulse = 0; // Variable for saving pulses count.
byte lastState = LOW;

void setup() 
{
  pinMode(input, INPUT);

  Serial.begin(9600);
  Serial.println(F("No pulse yet...")); // Message to send initially (no pulses detected yet).
}

void loop() 
{
  byte currentState; 
  
  currentState = digitalRead(input);
  
  if (currentState == HIGH && lastState == LOW)
  {
    pulse++;

    Serial.print(pulse);
    Serial.print(F(" pulse"));

    // Put an "s" if the amount of pulses is greater than 1.
    if (pulse > 1) 
    {
      Serial.print(F("s"));
    }
    Serial.println(F(" detected."));
  }
  lastState = currentState;
}

thank you for your help , but I have it done , with help, of course

@December28

Don't sweat the lectures, its the internet. A lot of the long time posters see the same thing every day and they start going a bit bonkers because it feels like the same person over and over not learning anything at all. In reality its thousands of people just starting out and they are all completely lost.

The pattern I've noticed is some newbie comes on with the standard : "I'm a newbie :slight_smile: and I don't know anything. (Like they are proud of it) I found some codes on the internet and they don't work. I tried yelling at my Arduino, but that didn't work either.." And it just spirals downhill form there.

At this point, a longtime poster, who really needs a break from these forums, might go berserk and hammer the person about how they should have posted. Or that they are not here to write code for you etc etc. Then all the sheep posters will get on the band wagon and hammer everyone..

And it goes on and on.

You ran across one the favorite minefields of "Not posting in code tags". Its a knee jerk reaction and they can't help themselves. Some even keep their replies in scripts so they can bash you even faster.

Its kinda' sad, but that's they way it goes. I'm sure I've lost it and bashed a few out here myself. I've trained myself to take long breaks when I start wanting to bash the newbies. I've also noticed by breaks are getting longer and longer and the time between seems to be getting shorter and shorter.

And don't feel bad, I've never read the pinned stuff at the top of the pages either. Nobody does.

-jim lee

I've read the pinned stuff at the top of the pages but even my friends know me to be A.R.

December28:
thank you for your help , but I have it done , with help, of course

I just figured it would be a great way to learn a more readable, logical way to implement.