scolland:
Thanks for your message. I've updated my code as follows
void countRoutine(){
static unsigned long last_interrupt_time = 0;
unsigned long interrupt_time = millis();
if(interrupt_time-last_interrupt_time>25){
last_interrupt_time = interrupt_time;
noInterrupts();
count++;
interrupts();
}
}
Is there something else that I am doing wrong?
Could be. Could be.
If the interrupt routine is working on a thing do and another interrupt comes in the interrupt handler may get confused and just poo the bed.
Just for kibbles and bits have you reduced your interrupt code to a simple counts++;
to check if there are too many things going on in the interrupt routine?
In the main loop put a print count statement to see if the count is going up.
The interrupt code should be as minimal as possible.
If the count is increasing as the loop code runs, the interrupt is working fine and needs to have less stuff to do.
If you move the code into another function that gets called by the interrupt you may run into some collisions.
For instance if a interrupt happens and a function is called, but, whiles the function is doing the thing, another interrupt comes in to call the function something in the chain is going to poo the bed. As may be obvious a calling function from an interrupt may need some traffic control.
You may want to look into a small / light weight task scheduler that has mutex or semaphore functions. Using a mutex or semaphonre will prevent the collisions and keep the interrupt handler from pooping the bed.
I use uMT for the DUE, which I think would tax the Mega, in my opinion. There ae other lightweight task schedulers with mutex and semaphonre capibilities. Do a search using your favorite search engine on "arduino using mutex."
For sure, start with thinning out the interrupt handler and see how well it works with minimal thing do's. The code does not have to do what you want, you are just troubleshooting, and reducing operations to their simpliest components.
If I remember correctly the MEGA is a 8 bit machine and asking a 8 bit machine to do this kind of math
static unsigned long last_interrupt_time = 0;
unsigned long interrupt_time = millis();
if(interrupt_time-last_interrupt_time>25){
last_interrupt_time = interrupt_time;
in an interrupt routine is asking for a lot.