Is there a way to have the arduino jump out of it's normal loop when it is receiving serial information in order to update local variables with what is being sent to it through serial? I just don't want the arduino to be going through it's loop and happen to miss what is being sent to it over serial due to timing issues. I have searched around for a while and there is a lot of conflicting information.
loop() already is interrupted when serial data is being received.
Maybe Robin2's Serial input basics can be of help.
The standard HardwareSerial library uses a 64 byte buffer so you will only miss something if that buffer overflows.
And to answer the actual question, you can use return to get out of loop().
void loop()
{
static char ch;
if(Serial.available() > 0)
{
ch = Serial.read();
return;
}
...
...
// for demo purposes, a very long process
// the above will NOT save you !!
for(int cnt=0;cnt<10000;cnt++)
{
delay(10000);
}
}
I don't think that you will easily miss stuff due to timing issues (except for the stupid delay loop that I implemented above and as stated, the 'return' solution will not help you in that case); only due to poor design or poor definition of a protocol.
Thank you AWOL and sterretje, I just wanted to be sure before I started writing a long complex script. I wasn't sure if it was already buffered, I appreciate the help.
The best way to think of it is to remember that serial is S. L. O. W.
Your main loop, even a relatively complex one, could be doing 5-500 iterations for every serial character. So the loop can do a lot of work while it's waiting for the next serial character to arrive.
before I started writing a long complex script
What is this script of which you speak ? You are writing a program in C++ and compiling it, not interpreting it at runtime.
Sorry UKHeliBob, I meant program... I was typing quick. And thanks MorganS, I never realized that serial was that slow.