Go Down

Topic: SOLVED:Checking variable for while loop, updated from Interrupt (Read 688 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

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131