Go Down

Topic: SOLVED:Checking variable for while loop, updated from Interrupt (Read 748 times) previous topic - next topic


I am using a TIMER1 overflow interrupt,
I have written a quick code to test to see if i can update a variable with the interrupt as its being checked by a while statement.

Code: [Select]

   #include "TimerOne.h"
   byte FLAG = 0;
   void setup()
     pinMode(6, OUTPUT);
   void callback()
     digitalWrite(6, HIGH);
     digitalWrite(6, LOW);
     FLAG = 1;
   void loop()
     Serial.println("SEND MESSAGE");
     while(FLAG == 0);
     FLAG = 0;

It doesn't notice the interrupt change in the FLAG status with :   while(FLAG == 0);

It does notice the flag change however if i put something in the while loop to check eg:

while(FLAG ==0){
Serial.println(FLAG, DEC);

Does anyone know why this is happening?
Doesn't  " while(FLAG == 0);" continuously check the state of FLAG anyways?


Any variable that's shared between interrupt code and the main code must be declared 'volatile':

Code: [Select]
volatile byte FLAG = 0;


Code: [Select]
In an interrupt handler? I don't think so!
The art of getting good answers lies in asking good questions.


thanks the volatile byte worked.

yes i know putting a delay in there is bad, in my actual program there is nothing like that.

thanks again

Go Up