volatile byte _incomingFlag = 0;
void onIncoming () {
_incomingFlag = 1;
Serial.println(F("You got an interrupt"));
mySerial.println("ATH0");
}
void setup () {
/* First Start the module */
Serial.begin(9600);
Serial.println(F("Initializing module..."));
powerOn();
mySerial.begin(9600);
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);
delay(100);
}
void loop() {
if (_incomingFlag == 1) {
manageIncoming();
Serial.print(F("_incomingFlag: "));
Serial.println(_incomingFlag);
} else {
Serial.println(_incomingFlag);
Serial.println(F("not getting value"));
}
get_currentLocation();
send_Location();
}
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()
.
1 Answer 1
volatile byte _incomingFlag = 0;
void onIncoming () {
_incomingFlag = 1;
Serial.println(F("You got an interrupt"));
mySerial.println("ATH0");
}
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
or
0
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.
Explore related questions
See similar questions with these tags.
send_Location
function personally, because you did not post it.