is there a way of counting pulses and producing an rpm reading?

I have a motor passing a bar through an IR slot sensor similar to this one.
every time the bar passes through, the LED on D13 comes on (or flashes as the bar passes through)
The sketch is here

/*
  monitor a motor passing a bar through an ir slot sensor and turning the led on D13
  on every pass.
 */
int buttonstate;
int sensorValue;
int outputValue;

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin 13 as an output.
  pinMode(4,INPUT_PULLUP);
  pinMode(5,OUTPUT);
  pinMode(13, OUTPUT);
  Serial.begin(9600);
}

// the loop function runs over and over again forever
void loop() {
    sensorValue = analogRead(A0);            
  // map it to the range of the analog out:
  outputValue = map(sensorValue, 0, 1023, 0, 255);       
  analogWrite(5,outputValue); //drive motor   
//  Serial.print(outputValue);
  buttonstate = digitalRead (4);
  if (buttonstate == LOW)
  {
  digitalWrite(13, LOW);   // turn the LED on (HIGH is the voltage level)
  delay(1);              // wait for a second
  }
  else
  {
  digitalWrite(13, HIGH);    // turn the LED off by making the voltage LOW
  delay(1);              // wait for a second
  }
//  Serial.println(buttonstate);
}

Is there any way of being able to turn the signal into an RPM of the motor that can be printed, say, on the monitor?
Even if the RPM is completely wrong and needs to be manipulated to make it similar to the actual RPM of the motor.
Thanks.

delay(1); // wait for a second
error in comment - it should be millisecond

for the RPM, you should use interrupts - see the example code of attachInterrupt how that is done.
in short, every falling pulse increments a counter.
every second you read the counter and convert it to RPM = count * 60;

or at every interrupt you log the time in micros();

RPM = 60000000.0 / (currentTime - previousTime);

tip: CTRL-T in the IDE auto-indents your code, making it even more readable

robtillaart:
delay(1); // wait for a second
error in comment - it should be millisecond

thanks for answering.

sorry I decreased the delay after being sure the sensor worked and didn't alter the comment.

I will take a look at the interupt examples.
I just wondered if there wasn't an instruction to do this as it seems something people would want to do?

I just wondered if there wasn't an instruction to do this as it seems something people would want to do?

No no such instruction exists afaik. This is because there are several parameters to consider, e.g. pulses per rotation, length of pulses, gearing to consider etc,
One can build the code that does exact your conversion, and that gives the freedom to convert pulses to RPM or to meters, kilometers, yards, miles etc.