I have a teensy 3.2 that I am using to count TTL pulses coming from a camera (one per frame, roughly 20Hz). I am using an interrupt to count rising edges on pin 8. Every 5 ms, the teensy reports the frame count to a computer via Serial. If for some reason the camera doesn't send a pulse for 2 seconds, the counter resets to zero.
I thought this was working perfectly (it seems to work great for periods up to a minute), but when I ran the camera for 30 minutes, I find that the frameNum counter seems to randomly reset to 0 two or three times (I can find no pattern to when this happens - it doesn't look like overflow or anything). I am quite certain that the reset is not occurring due to a two-second delay - the recorded timestamp on the computer shows beautiful 5ms spacing on the serial messages right around where the counter resets.
My best guess is that I have done something horrendously dangerous by setting tLastNewFrame=0 inside of my interrupt function. If this is the case, it'd be great if someone could tell me how to do this more safely? If that sounds unlikely, what else might cause such a rare reset behavior?
Thanks in advance!
I thought this was working perfectly (it seems to work great for periods up to a minute), but when I ran the camera for 30 minutes, I find that the frameNum counter seems to randomly reset to 0 two or three times (I can find no pattern to when this happens - it doesn't look like overflow or anything). I am quite certain that the reset is not occurring due to a two-second delay - the recorded timestamp on the computer shows beautiful 5ms spacing on the serial messages right around where the counter resets.
My best guess is that I have done something horrendously dangerous by setting tLastNewFrame=0 inside of my interrupt function. If this is the case, it'd be great if someone could tell me how to do this more safely? If that sounds unlikely, what else might cause such a rare reset behavior?
Thanks in advance!
Code:
volatile unsigned long frameNum;
const int broadcastPeriod = 5000; // us
const int resetTime = 2000; // ms
elapsedMicros t;
elapsedMillis tLastNewFrame;
void newFrame(){
frameNum++;
tLastNewFrame = 0;
}
void setup() {
Serial.begin(9600);
pinMode(1, OUTPUT);
frameNum=0;
attachInterrupt(8, newFrame, RISING);
t=0;
}
void loop() {
if (t > broadcastPeriod){
Serial.println(frameNum);
t=0;
}
if (tLastNewFrame > resetTime)
frameNum=0;
}