PWM/ RPM feedback loop

You need to explain more clearly what sort of light sensor you are using and how its output relates to the speed of your motor.

I use a QRE1113 reflective optical sensor that produces one pulse for each revolution of a small DC motor. The code I use to measure the speed is similar to this - it's not complete but should give the idea

volatile unsigned long isrMicros;
unsigned long latestIsrMicros;
unsigned long previousIsrMicros;
volatile boolean newISR = false;

void loop() {
   if (newISR == true) {
     previousIsrMicros = latestIsrMicros; // save the old value
     noInterrupts(); // pause interrupts while we get the new value
        latestIsrMicros = isrMicros;
        newISR = false;
     interrupts();
     microsThisRev = latestIsrMicros - previousIsrMicos;
}

void myISR() {
   isrMicros = micros();
   newISR = true;
}

I then use PID (actually just PI) to keep the speed at a chosen rate.

...R