I have an IR photodiode driving an interrupt on an Arduino UNO. I added TimerOne to get regular output reports, but TimerOne kidnapped my interrupt port. Instead of seeing the IR interrupt, the UNO only sees the timer signal and calls both ISR routines when the timer triggers.
How do I use a timer and another interrupt at the same time?
My code has changed about 10 times since I had a working interrupt with the IR photodiode. It originally had a simple counter that printed every 100 times it triggered the interrupt. That worked great. I added the timer and the IR counter stopped working. So, this code is a summary of the key pieces:
#include <TimerOne.h>
int count = 0;
float time = 0.0;
int runCalc = 0;
void counter() {
count++;
}
void stepTime() {
time += 0.2;
runCalc = 1;
}
void calc() {
const int tab = 9;
Serial.print(time);
Serial.write(tab);
Serial.println(count);
count = 0;
runCalc = 0;
}
void setup() {
Serial.begin(9600);
Timer1.initialize(200000); // set a timer to 5 Hertz
Timer1.attachInterrupt(stepTime);
attachInterrupt(0, counter, FALLING);
}
void loop() {
if (runCalc) calc();
}