Problem updating interrupt driven counter

The counter is declared as "int Ticks" and it is incremented or decremented in an Interrupt Service Routine.

My problem is illustrated by this subroutine:

void Test()
{ int t1; int t2;
t1 = Ticks;
delay(500);
Serial.print("");
t2 = Ticks;
Serial.print(t1);
Serial.print(" ");
Serial.println(t2);
}

Without the bolded line the result is t1 = t2.
This seems to solve the problem but it would be nice to know why this happens and if there is a neater way to fix it.

Did you mean "volatile int Ticks;"?

Please remember to use code tags when posting ALL of you code.

Thank you, Shannon!

Arduino Reference:
"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."

Spot-on, It solved the problem!

Code tags, is that what you get by formatting with </>, like:
volatile int Ticks; ?

Last question:
Took some scrolling to find the answer. Is it possible to get email notification when a question is responded to?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.