This is a massive (!!!) no-no in an ISR. Never, absolutely never halt the code flow in an ISR for more than a couple of micro seconds.
You should also avoid Serial.print()
inside an ISR as these are relatively heavy calls.
It may be OK for testing, but should never be used in production code.
Yes, it must be declared as volatile
to prevent the optimizer from optimizing away actual data acquisition from RAM in the rest of your code where it otherwise would replace RAM access with internal register access for the sake of speed.
You also have an unwarranted space between %
and i
in this
snprintf (dataZ2, sizeof (dataZ2), "% i", AStatoZ2);
(besides many other superfluous spaces that don't really help readability - typically the parameter list of a function belongs to the function and is not separate from it as an extra space between would suggest)
To use a volatile
variable with Particle.variable()
you may want to read this thread
BTW, the suggestion in my second post in that thread works as expected. No need to use the deprecated syntax that is marked as the solution.
However, be aware that casting away volatile
may result in unexpected results when the interrupt routine is called while the Particle.variable()
is accessed.