volatile byte _incomingFlag = 0;

void onIncoming () {
    _incomingFlag = 1;
    Serial.println(F("You got an interrupt"));

void setup () {
    /* First Start the module */
    Serial.println(F("Initializing module..."));



    sendcommand(F("AT"), F("OK"), 1000);
    sendcommand(F("AT+CLIP=1"), F("OK"), 1000);
    sendcommand(F("AT+CMGF=1"), F("OK"), 1000);

    /* We need to attach ringing Interrupt */
    attachInterrupt(M_RING_INTERRUPT, onIncoming, FALLING);


void loop() {
    if (_incomingFlag == 1) {
        Serial.print(F("_incomingFlag: "));
    } else  {
        Serial.println(F("not getting value"));

I am trying to check the value of incomingFlag from the loop() . When I get a call or an SMS the interrupt function is getting executed and incomingFlag is set to 1 but I am not getting that value from the loop(). Everytime it's getting 0 if I check the value while the send_Location function is executing (data uploading part). How can I access the value of incomingFlag from the main loop().

  • 3
    Using serial inside an interrupt is not advisable. Especially if you talk to the GSM module inside an ISR. The rest of the code looks fine, though I'd like the see the send_Location function.
    – Gerben
    Commented Aug 6, 2015 at 13:35
  • Hi Gerben, thanks for your reply. Please see my send_Location function and that serial functions inside interrupt is just for testing purpose.
    – Sachin
    Commented Aug 6, 2015 at 13:43
  • I cannot see your send_Location function personally, because you did not post it.
    – Nick Gammon
    Commented Aug 6, 2015 at 22:26
  • do not use leading underscores in symbol names. that can confuse the compiler, which uses 1 or 2 leading underscores for special purposes. Commented Aug 8, 2015 at 2:13
  • where does the code reset the _incomming_flag to 0 after finding the flag ==1? Never call any kind of print operation from within a interrupt. Where is the code resetting the 'interrupt pending' bit for that interrupt? Commented Aug 8, 2015 at 2:20

1 Answer 1

volatile byte _incomingFlag = 0;       
 void onIncoming () {
             _incomingFlag = 1;
            Serial.println(F("You got an interrupt"));


Regardless of what your purpose is there, you cannot successfully do serial prints inside an ISR. All bets are off if you do that.

In fact you are doing two prints - is one SoftwareSerial?

If you must have a debugging "flag" turn on an LED. Doing serial printing requires interrupts to be on, and they are off inside an ISR.

Note: You may get the display once, and therefore you think it works. As soon as the serial buffer fills up (probably 64 bytes) then it will just block indefinitely. Your message "You got an interrupt" is already 22 bytes if you count the trailing carriage-return/linefeed.

More detailed explanation

In your main loop, every time through it, you are printing either:

_incomingFlag: 1


not getting value

Therefore the serial buffer will be definitely full (or almost full). It can't empty the buffer if you are cramming this stuff into it every time through loop.

So now you have an incoming call. You set _incomingFlag to 1, then try to do this:

    Serial.println(F("You got an interrupt"));

That blocks, because if can't fit all that into the output buffer, and the program then hangs, because the buffer is not emptied inside an ISR.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.