Hello
I am building a laser projector
and have a problem with the RPM measurement --
I use an interrupt , and the loop() only toggles the laser on and off with delay() functions.
so if I remove the delay() lines the RPM measurement is good , if delay() is used -- not in the interrupt itself but in loop() , the RPM goes crazy and deviates about 20% up/down.
checked for optical interference , none
checked for voltage interference between optical sensor and laser diode with oscilloscope , none
also disabled serial communication , no help
any ideas?
volatile byte rpmcount;
float rpm;
float interval;
unsigned long timeold;
volatile unsigned long sig;
long pausebegin;
long totalpause;
void rpm_fun()
{
rpmcount++;
}
void setup()
{
Serial.begin(19200);
attachInterrupt(0, rpm_fun, FALLING);
rpmcount = 0;
rpm = 0;
timeold = 0;
interval=46000;
totalpause=0;
}
void loop()
{
if (rpmcount >= 20) {
//Update RPM every 20 counts, increase this for better RPM resolution,
//decrease for faster update
rpm = 60000/(sig-timeold)rpmcount;
interval=1000(millis()-timeold)/rpmcount;
timeold = millis();
rpmcount = 0;
Serial.print("rpm=");
Serial.println(rpm,DEC);
Serial.print("interval=");
Serial.println(interval,DEC);
}
// digitalWrite(13, HIGH);
// delayMicroseconds(interval/4);
// digitalWrite(13, LOW);
// delayMicroseconds(interval/4);
// digitalWrite(13, LOW);
// delayMicroseconds(interval/4);
// digitalWrite(13, LOW);
// delayMicroseconds(interval/4);
}