Delta_G:
Please don't go changing the first post as that makes all the rest of the discussion out of context. Just put what you learned at the end.
I respectfully disagree. My small contribution to the forum is that I try to pass the knowledge learned forward. Let me know if you disagree with the summary at the top.
Robin2:
The impression I have is that he just wants to use an interrupt to avoid the need to think carefully about how the rest of his program should be written.
I must say that I agree wholeheartedly with you sentiment to avoid complications in the main loop to avoid the need to think (did I just disagree?). However, "bperrybap" hit's the tail of the cat (or was it tail of the head, head of the snail ...) in that he realizes that we are not perfect. Arduino for me is convenient and fun and the fun part is not a perfect program ... mix in the hardware and convenient software ...
masse12345:
Just to make sure I haven't misunderstood ... the OCR0A / OCR0B values are used to define the PWM on/off point in the cycle? I.e., they change with analogWrite to define the on/off point? In turn this would affect when in the cycle the TIMER0_COMPA_vect interrupt occurs? But the interrupt will still occur once every cycle (irrespectible of the value in OCR0A)?
Yes that's correct. However if your reason for using an interrupt is to minimize the jitter in your code's timing, then using analogWrite would defeat that purpose. For example if you suddenly changed the analog output from 250 down to 10, then your next "one millisecond" would only be 60 microsecond! Similarly when changing the other way, from 10 to 250, your next "one millisecond" would be nearly two milliseconds.
My comment about not using analogWrite however was specific to my suggestion that if you really want then you can get exactly 1 ms by subtracting 6 from the relevant "on compare" register at each interrupt. This would absolutely play havoc with analog write on the associated pin.
Cattledog's comments summed it up pretty well. Your code can't define an interrupt on timer 0 overflow, because that vector is already in use by the system.
cattledog:
Even if you wanted to break the millis() function, it's not that simple to just simply write a new Timer0 overflow ISR.
There is a file named wiring.c which is part of the Arduno core and is used in all sketches. Wiring.c sets up and enables the Timer0 overflow interrupt used in the millis() function. You can't write a new ISR without removing this one or the compiler will complain.You can find wiring.c on your computer at a path like this
C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino\wiring.c
You can indeed modify the existing Timer0 overflow interrupt to add whatever functionality you want to the standard isr, and it will not interfere with millis(). However, modifying wiring.c does not make the code very portable.
I believe a better work-around would be to simply define the ISR inside the wiring.c file as weak:
SIGNAL( TIMER0_OVF_vect ) __attribute__((weak));
Then rewrite the ISR inside your sketch.
Another method would be to copy the wiring.c file into the sketch directory and modify as needed. Due to the method in which Arduino specifies the link order, you're able to "override" the behavior of core functions.