Hi All,
I posted here previously regarding the problem I'm having with my project. I'm using a Arduino MEGA 2560 and a OPB704 photoelectric sensor to detect RPM of a fan blade. I have attached my code below, sometime it picks up some speed, but its random figures, after a while it then detects a different speeds, I'm wondering if my coding is correct? or could it be a faulty sensor as most the time it stays on 0.
I have attached a 220Ohm resistor and also a 100k resistor as per the guidance of the specifications.
Your help would be appreciated.
Thanks
volatile float time = 0;
volatile float time_last = 0;
void setup ()
{
Serial.begin (19200);
attachInterrupt(0, rpm_interrupt, RISING); //Pin 3 Set As An Interrupt ( if signal increase )
}
//Loop To Calculate RPM and Update The Serial Monitor Display
void loop () {
int rpm = 0;
while(1){
delay(250); //Slow Down Display Updates
Serial.println (" ");
Serial.println (" ");
Serial.print ("rpm:");
Serial.println (rpm);
if(time > 0) {
rpm = 60*(10000060/(time));
}
}
}
//Capture The OPB704 Beam =>Interrupt
void rpm_interrupt() {
time = (micros() - time_last);
time_last = micros();
}