Volatile basically means that "things" may change the variable in a manner that's not obvious when the compiler looks at the code. Such as say a input register that changes state when inputs change. To quote from the Netrino site
C's volatile keyword is a qualifier that is applied to a variable when it is declared. It tells the compiler that the value of the variable may change at any time--without any action being taken by the code the compiler finds nearby.
Frankly I don't know why it should be required in your exact code because the compiler should know what's going on, and as it's working maybe that is in fact the case. Still, it's good practice to use the qualifier for vars shared with ISRs.
One place this will bite you is with optimisation
loop () {
long i;
for (i = 0; i < 100; i++ ) {
i = i;
}
pulse_pin ();
}
In this simple loop I am just trying to output some pulses to see some timing for whatever reason and I want to vary the loop time by changing the 100. But no matter what I change it to, even 1,000,000 the frequency of the pulse stays the same.
The compiler has decided that the loop actually doesn't do anything and has removed it from the run time code. Changing it to
volatile long i;
fixes the problem by causing the compiler to butt out.
wouldn't another if statement inside of the for loop add greater overhead?
Yes but there's plenty of fat because of the delay, so adjust the
delayMicroseconds(1931);
to accomodate the extra code in the loop.
______
Rob