I am busy writing code for a project that uses one channel of remote control pwm signal as an input signal. I know there is a good library for this, but still I like to write my own ISR routine for this since I only use one channel it will save me on overhead.
What I wonder is: given the code below, should I define the variable pwmInValue as volatile or not?
A variable should be declared volatile whenever its value can be changed by something beyond the control of the code section in which it appears, such as a concurrently executing thread. In the Arduino, the only place that this is likely to occur is in sections of code associated with interrupts, called an interrupt service routine.
Protect the above critical section as follows so that the chance for the variable pwmInValue to undergo change by the PWMTStamos() ISR is nil while being accessed in the loop() function.
rather then just disabling and restarting interrupts?
The thing behind my initial question: I studied Arduino-PWM-Reader-master library and noticed that nowhere in that library interrupts are disabled or enabled nor is the atomic_block macro used (or advised to use in the main loop). Yet this library seem to work quite good I am puzzeled why... I must be overlooking something, but I don't see it. Hopefully someone can enlighten me.
However... if I look at the example given on that webpage called "Example code of a pump timer" I noticed that the variable
volatile boolean buttonPressed;
Yet, nowhere in the main loop of that piece of code I see interrupts being disabled and re-enabled when the variable buttonPressed is read or changed. These are exactly the kind of examples that confuse me...