I am trying to learn state machines and the interrupt function and implement them into a bigger project. The following code is for blinking an led on and off and the frequency changes depending on a state variable that is either SLOW or FAST.
When I upload this to my Due the led connected to pin 13 just stays on and isn’t blinking. On my hardware I have a button whose signal is going through an inverting schmitt trigger and is then fed into digital pin 2 for the interrupt. I tried to follow along with the blink without delay program in this program but this program seems to have some syntax problem. Anybody know what could be wrong?
In most cases, data that is shared between setup / loop and an interrupt service routine has to be protected from the interrupt service routine when it is accessed in setup / loop. One case where data must be protected is when the data is stored in multiple bytes. enum is two bytes in the AVR / Arduino world. state is an enum so state has to be protected.
A "critical section" is a section of code that runs to completion without being interrupted. A critical section is how you protect state when it is accessed from setup / loop. In the AVR / Arduino world, a critical section is composed of: save the global interrupt flag, disable interrupts, access the data, restore the global interrupt flag.
If millis() - time is less than 2000 it will also be less that 4000 so both tests will be true and the second one will always be acted upon. Same problem in the FAST case.