IR Detector help!

Hello,

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:

int irdetector = 2;           // IR detector connected to digital pin 2 (interrupt 0)
int count; 
int count2;
int count3;
int diff;
int interrupt = 0;


///////////////////////////////////////////////////////////////////////////////
void setup()
{
  count = 0;
  count2 = 0;
  count3 = 0;
  diff = 0;
  Serial.begin(9600);               
  pinMode(irdetector, INPUT);      
  attachInterrupt(interrupt, pulseCounter, FALLING); 
}



////////////////////////////////////////////////////////////////////////////////
void loop()
{
  count2 = count;
  delayMicroseconds(500);
  count3 = count;
  diff = count3 - count2;
  Serial.println(count);
  Serial.println(count2);
  Serial.println(count3);
  Serial.println(diff);
  //Serial.println(int(count));      
  //delay(99);
  
  count2 = 0;
  count3 = 0;
  diff = 0;
  count = 0;
  delay(500);
}



///////////////////////////////////////////////////////////////////////////////
void pulseCounter()
{
 count++;
}

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?

Thanks for any help!!

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.

Use of the ‘volatile’ qualifier is recommended for “count”

Any possibility of covering 3 of them?

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:

count2 = count;
  delayMicroseconds(500);
  count3 = count;
  diff = count3 - count2;

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.

PaulS,

That worked perfectly! Thank you!!!