I am trying to measure RPM of a flywheel attached to a DC motor. The flywheel has four holes evenly spaced near the edge. I am using a IR emitter and IR detector for the measurement. I have tried pulseIn() but the time between holes is to small for the pulseIn() function to measure.
I have also tried using attachInterrupt() to add to a counter variable each time a hole passes by the detector. I tried calculating the difference between the counter at two different times. The interrupt code can be seen below:
The output from the detector is 0.5V and 5V depending on if the hole is in front of the IR LED. Does anybody have any suggestions on how to read a signal that changes faster than the pulseIn() limit of 10microseconds?
The flywheel has four holes evenly spaced near the edge.
Any possibility of covering 3 of them? What would increase the time between interrupts?
I have also tried using attachInterrupt() to add to a counter variable each time a hole passes by the detector. I tried calculating the difference between the counter at two different times. The interrupt code can be seen below
You also failed to say what the problem is with this method.
I tried taping over three of the holes and now pulseIn works but its not very accurate. The RPM is off by 300-1000rpm and the error is not consistent. The error gets larger as the RPM increases.
You also failed to say what the problem is with this method.
Sorry, I forgot to include the problem! Whoops! The problem I was having was between the following lines of code from my original post:
The only way I am getting a value other than '0' for 'diff' is to make the delay at least 1sec. 'count2' and 'count3' are the same value when the delay is anything less than 1sec.
I can't afford to have such a large delay in my program. I thought I could make that delay very small because the time between interrupts is very small (10us-10ms). Not sure why its not saving the correct 'count' values in 'count2' and 'count3'.
Rather than any delay, look at the blink without delay example. You could simply look at the value of count every so often (currMillis - prevMillis > interval), and compute RPM based on the difference between the oldCount and count. Then, reset count and prevMillis to 0 and currMillis.