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.