I'm building a system that includes a motor that runs from 1000-7000 rpm.
On the shaft of the motor is a 36-1 wheel, that is a 36 toothed wheel with one tooth missing.
I have a slotted opto-switch picking up the passing of the teeth and whose output is fed into a Diecimila interrupt. The output of the switch is a 5V square wave.
All I am trying to do at the moment is prove that the Diecimila can reliably track an opto-switch interrupt. I monitor the Diecimila's output pin 13 with an oscilloscope.
If I run the following code I notice that occasionally the output will change phase, indicating it has missed an edge change point. ie if the output is in-phase with opto-switch input it will switch to out of phase. Then, after a few seconds, it will flip back to in-phase. The wheel change point and time of change seems to be random.
int pin = 13;
volatile int state = LOW;
void setup()
{
pinMode(pin, OUTPUT);
attachInterrupt(0, blink,CHANGE);
}
void loop()
{
digitalWrite(pin, state);
}
void blink()
{
state = !state;
}
However, if I run the following code the rate of random phase changes drops dramatically but still occurs.
int pin = 13;
volatile int state = LOW;
void setup()
{
pinMode(pin, OUTPUT);
attachInterrupt(0, blink,CHANGE);
}
void loop()
{
}
void blink()
{
state = !state;
digitalWrite(pin, state);
}
It occurs to me that the missed changes might be caused by timer interrupts. I don't need a timer for my intended application so how do I turn timer interrupts off?
Thanks,
Derry.