Mesuring rps between pulses, bounce

I have a very easy problem, but because I'm a beginner I can't solve it.Of course I tried to find it using the search tool but I'm not able to adapt the code.
Well, the question is that I need to send to serial,not only the pulse counting ,but alsol the rps ( n of pulses/second).
The frequency of the measured pulses go between 3-10 pulses/second. I really only need to send a rps value every 10 pulses.(more or less or alternatively every 2 seconds)
Could me anyone help to adapt the following code ?
The code iI'm using is a bounce code or state change detection with a noise filter.
Thank you in advance for your help!

/*  pulse counter*/

const int  buttonPin = 2;   
const int ledPin = 13;       

int buttonPushCounter = 0;  
int buttonState = 0;         
int lastButtonState = 0; 

long lastEvent = 0;  
long interval = 20;    

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);

void loop() {
  buttonState = digitalRead(buttonPin);

 if (buttonState != lastButtonState)
 if (buttonState == HIGH && millis() - lastEvent > interval) 

 lastEvent = millis(); 
      // Serial.print("no pulsos:  ");
    else {
  lastButtonState = buttonState;
  if (buttonPushCounter % 4 == 0) {
    digitalWrite(ledPin, HIGH);
  } else {
   digitalWrite(ledPin, LOW);

Properly indent your code first, CTRL-T in the IDE.

What is pushing the buttons?

Where are you measuring the time between pulses?
Where are you sending anything?
Where are you computing the values you want to send?

Hi Paul,
thanks for reply.
By the way,this is your code you wrote to me 1 year ago.
Answering your questions:
-the button is a inductive sensor with a magnet.

  • The time I'm not measuring it directly but I thought that "lastinterval" would be the time between pulses
    I'm right?
  • I'm sending the pulses counter via serial-usb and reading it in excel using netcomm.


  • The time I'm not measuring it directly but I thought that "lastinterval" would be the time between pulses
    I'm right?

lastEvent is the last time the event (the magnet going by the sensor) occurred. You can copy that value, and when another event occurs, you can determine the time between events, which corresponds to events per unit time (rotations per second, for instance).

You could also forget about lastEvent, and simply execute a while loop, waiting for some period of time to go by. If you wait for 100 milliseconds, and 10 events occur, then 100 events would occur (100 revolutions) per 1000 milliseconds (1 second). The problem with this approach is that the pulses are fairly far apart, and pausing long enough to get an accurate count might be a problem. Of course, it doesn't appear that your sketch is doing all that much, so it might not be.

Could you help me adding the code to the given sketch?
Thanks a lot!