Go Down

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

btsp

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()
   {
     Serial.begin(9600);
     pinMode(6, OUTPUT);
     Timer1.initialize(1000000);        
     Timer1.attachInterrupt(callback);  
     Timer1.stop();
     
   }
   
   void callback()
   {
     digitalWrite(6, HIGH);
     delay(10);
     digitalWrite(6, LOW);
     FLAG = 1;
     Timer1.stop();
    }
   
   void loop()
   {
     Serial.println("SEND MESSAGE");
     Timer1.start();
     Serial.println("RECEIVING");
     while(FLAG == 0);
     FLAG = 0;
     delay(10000);      
   }
   


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?


Anachrocomputer

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

Code: [Select]
volatile byte FLAG = 0;

PaulS

Code: [Select]
      delay(10);
In an interrupt handler? I don't think so!

btsp

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!

Arduino
via Egeo 16
Torino, 10131
Italy