Not sure if this is the right section of the forum?
I am trying to monitor the RPM of a shaft on which I have a magnet and reed relay.
Here is the code
//-----------------------------------------------
volatile byte rpmcount;
unsigned int rpm;
unsigned long timeold;
unsigned long timeTemp;
void setup()
{
Serial.begin(9600);
attachInterrupt(0, rpm_fun, RISING);
rpmcount = 0;
rpm = 0;
timeold = 0;
timeTemp = 0;
}
void loop()
{
if (rpmcount >= 10) {
rpm = 30*1000/(millis() - timeold)*rpmcount;
timeTemp = millis();
Serial.print("millis ");
Serial.println(millis());
Serial.print("timeold ");
Serial.println(timeold);
Serial.print("rpmcount ");
Serial.println(rpmcount);
timeold = millis();
rpmcount = 0;
}
}
void rpm_fun()
{
rpmcount ++;
}
//-----------------------------------------------
I am using the standard set up with a 10K resistor.
My problem is with between 3 to 5 slow turns ie pulses it goes through the if (rpmcount >= 10) so it must be getting noise. I have tried a push button switch and get the same problem. I have also tried a 10ms delay with similar results.
Would it be better to use an analoge pin and use PMW? or what other way to get clear pulse signals?
Thank you