DC motor speed control.

Hello,

I am a newbie and this is the first post here. I am trying to do a speed controller on a dc motor trough a photo interrupter(KTIR0611s). Something like what the guy in this video does:

but I want to calculate the number of revolution per second my motor does.
I control the PWM of the motor trough a potentiometer.

Here is my code:

int Val_PWM = 0; //PWM value (0-255)
int Val_Slider = 0; //Slider value
const int SLIDER = A5; //Slider 5 kohm
const int MOTOR = 10;
float speed;
unsigned long time;
unsigned long prev_time = 0;
unsigned long delta;
const int photo_interrupter = 2;

void setup(){

attachInterrupt(0, speed_control , FALLING);
Serial.begin(9600);
pinMode(photo_interrupter, INPUT);
pinMode(SLIDER, INPUT);
pinMode(MOTOR, OUTPUT);

}

void loop() {

Val_Slider = analogRead(SLIDER);
Val_PWM = Val_Slider;
analogWrite(MOTOR, Val_PWM/4);

}

void speed_control(){

time = millis();
delta = time - prev_time;
speed = 500.0/(float)delta; //the motor has two blades so it does half revolution per interrupt
Serial.print ("revolutions per second:");
Serial.println (speed);
prev_time = time;

}

The problem is that when the PWM is different from 0 it happens that many interrupts occur also when nothing pass trough the photo interrupter even if the wire on the digital pin 2 is detached, while when I don't supply the motor the photo interrupter works normally.

What the cause might be?

Sorry if my English is not correct.

Don't do serial I/O in an interrupt.
Please use code tags when posting code.

What the cause might be?

Noise? Do you have very long wires to the photo-interrupter?

Please post a schematic

Does it make any difference whether the motor is actually connected? I mean, if you connect it's power supply to the driver board/shield and switch it on in software, but don't connect the cables to the motor itself.

Noise? Do you have very long wires to the photo-interrupter?

I have really short wires (maximum 8 centimeters)

Please post a schematic

I attached a circuit I draw with Fritzing, I hope it is understandable (it is the first time using it)

Does it make any difference whether the motor is actually connected? I mean, if you connect it's power supply to the driver board/shield and switch it on in software, but don't connect the cables to the motor itself.

If I connect it's power supply and switch it on in software but i don't connect the cables to the motor the circuit works. I mean the interrupts occur only in the right moment printing on the serial acceptable results.

You should use a darington pair driver, it is more stable and will last longer. If you want to go in both directions, get an H-bridge.

http://www.google.com/imgres?start=207&um=1&hl=en&safe=off&tbo=d&biw=1440&bih=754&tbm=isch&tbnid=c6snM7yNRGutfM:&imgrefurl=http://www.electronics-tutorials.ws/io/io_7.html&docid=lX9Zbtbrh_KcCM&imgurl=http://www.electronics-tutorials.ws/io/io32.gif&w=295&h=270&ei=B13UUIGvB87V0gGl-4C4Cg&zoom=1&iact=hc&vpx=473&vpy=186&dur=2879&hovh=215&hovw=235&tx=108&ty=155&sig=106574055291386674452&page=7&tbnh=142&tbnw=155&ndsp=39&ved=1t:429,r:18,s:200,i:58