control rpm of a motor

so here's a simple sketch that counts a photointerrupt and Serial.prints the result

/*  Photo inturrupt with phototransister RPT-38PB3F
 taken from Fan Timer circuit http://www.neufeld.newton.ks.us/electronics/?p=244
 
metasphere72_091413
 
 */

volatile static long counter = 0;

void setup() {
  Serial.begin(9600);
  attachInterrupt (0, count, CHANGE);
}

void loop(){
  delay(500);
  Serial.println(counter);
  counter = 0;
}
void count() {
  ++ counter;
}

it's really simple but I was also advised that when using a counter I needed to do the Counting in a SAFE way.
like this sketch that shows how to create a safe count variable to count the pulses without any interruptions.

/*  Photo inturrupt with phototransister RPT-38PB3F
 taken from Fan Timer circuit http://www.neufeld.newton.ks.us/electronics/?p=244
 altered to work Four LED's when count gets higher.
 Also this sketch incorporates a servo that moves an analog meter needle up and down in proportion to
 interrupt count.
 
 RPT-38PB3F phototransister Pins:
 Blk wire on Emmitter - TO Arduino GROUND PIN with 100k R in series.
 Red wire on Collector - TO Arduino 5v PIN
 Yellow signal Wire - connect to (Blk)ground before the 100k R TO DIGITAL PIN 2
 LED units:
 Powering the LED's are TIP-120 darlingtons running 12VDC. The Signal output comes from Arduino Digital Out Pins.
 PINS 6, 7, 8, 9 and 10.
 
 Sketch hacked and written by: metasphere72_11/13/13
 
 */
#include <Servo.h>  // include servo library
#define ServoMIN 29  // Don't go to end of servo travel which is 0
#define ServoMAX 145  // which may be 0 to 180.

Servo myservo;   // servo object to control a servo 
int val; // variable to store the servo position

unsigned long counter = 0;
unsigned long countVal = 0;
int ledPin01 = 6;
int ledPin02 = 7;
int ledPin03 = 8;
int ledPin04 = 9;
int ledPin05 = 10;
boolean cheatState = 0;
long previousMillis = 0;
long interval = 6000;


void setup() {
  Serial.begin(9600);
  myservo.attach(5);
  myservo.write(30);
  attachInterrupt (0, count, CHANGE);
  attachInterrupt (1, count, CHANGE);

  pinMode(ledPin01, OUTPUT);
  pinMode(ledPin02, OUTPUT);
  pinMode(ledPin03, OUTPUT);
  pinMode(ledPin04, OUTPUT);
  pinMode(ledPin05, OUTPUT);
}

void loop(){
counter = 0;
  noInterrupts();// This creates a safe count variable
  countVal = counter;
  interrupts();
  delay(50);
  countVal = (counter - countVal);// calculates the number of interrupts since last poll
  // Serial.println(counter);
 // Serial.println(countVal);
  
  // Normal Settings
if (cheatState = 0){
  
  if(countVal >=5  && countVal <=9)  { 
    digitalWrite(ledPin01, HIGH);
  }
  else{
    digitalWrite(ledPin01, LOW);

  }
  if(countVal >=10 && countVal <=18)  {  
    digitalWrite(ledPin02, HIGH);

  }
  else{
    digitalWrite(ledPin02, LOW);
  }
  if(countVal >=19 && countVal <=29)
  {
    digitalWrite(ledPin03, HIGH);
  }
  else
  {
    digitalWrite(ledPin03, LOW);
  }
  if(countVal >=30 && countVal <=40)
  {
    digitalWrite(ledPin04, HIGH);
  }
  else
  {
    digitalWrite(ledPin04, LOW);
  }
  if(countVal >=41 && countVal <=100)
  {
    digitalWrite(ledPin05, HIGH);
  }
  else
  {
    digitalWrite(ledPin05, LOW);
  }
 val = (countVal);
 val = map(val, 0, 45, 30, 140);
    myservo.write(val);
 }
}
void count() {
  ++ counter;
}
 unsigned long currentMillis = millis();

hope this helps.