Problem with Serial interrupt

Hi all,

I try to get my incoming serial data to change the variable value during my main function is in loop. But I dont understand how to interrupt the loop with arduino compiler. I tried search with google and used the serial event function but it cannot interrupt my loop.
the function is just get the serial data during the main function is not looping. Any thing I am wrong please help.

If with another MCU like PIC, they will have the serial interrupt function to interrupt any working process to get the serial data and then go back to processing again. Does the arduino has any function like that? Please help.

Thank you in advance,

Here is some part of my code :

void loop(){ 
  if(j==1)
  {
    while(!(limitswitch==0)){
    if(abs(encoderValue)<pulse){ 
      backward_motor();
    }
    else if(abs(encoderValue)==pulse){
      stop_motor();
      delay(shakingdelay*1000);
    }
    j = 0;
  }
}

void serialEvent(){
  while (Serial.available()) {
    char inChar = Serial.read();
    if(inChar == SOP){
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP){
       ended = true;
       break;
    }
    else{
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }
  if(started && ended){
    // The end of packet marker arrived. Process the packet
    sscanf(inData,"%d %d %d %d %d %d %d",&delaystart,&totaldistance,&totalshot,&interval,&shakingdelay,&shutterspeed,&savedelay);
    j=1;
    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}

I try to get my incoming serial data to change the variable value during my main function is in loop.

It doesn't happen that way. The serialEvent() function is called at the end of each pass through loop, and only if there is serial data to process.

Your serialEvent() method may be called 20 or 30 times before all the data has arrived, and loop() may have executed 50,000 times while serial data was coming in.

Since serial data is only captured after loop() ends, but before it executes again, the key is to make loop() iterate more often. That means NO delay() calls. You appear to just be running a motor and checking a limit switch, neither of which is appropriate with delay() anyway.

A restructure of your program is in order, after reading, understanding, and embracing the blink without delay example/concept.

Concur with PaulS. I think you would be better off integrating your if (Serial.available())… clause into the loop() function and managing the concurrency there.

-br

Thanks PaulS and billroy,

Actually my code is more complicated than this. This is just example.
So it is very difficult to manage the code to check the serial data during my process.

If the arduino has the interrupt function for the incoming serial, my work will be much simply.
Does the arduino has any function like that? CCS C for PIC, Keil ARM, AVR have the function like that.

Any suggestion would help much. Thank in advance.

Serial data IS collected in an interrupt handler. A byte arrives, triggering an interrupt, which suspends execution of the code, after the current instruction completes. The ISR copies the serial data into a buffer, and ends. The code that was running resumes at the next instruction.

You could, though it is not recommended, alter that ISR to do more stuff. What you can NOT do is change where the code resumes executing from. Also, keep in mind that the interrupt handler only processes ONE byte.