serial.available() from polling to interrupt

Hi everyone,
i'm working on a chat between my smartphone and arduino due. i'm using a bluetooth that communicates with arduino through the serial port.

in the loop function i've always used if(Serial.available) polling permanently the processor. how can i move from this polling procedure to an interrupt based function?

Look up serialEvent,

http://arduino.cc/de/Reference/SerialEvent

or roll your own ISP for the serial port.


Rob

serialEvent is checked after every loop, i need to know if i can trigger an interrupt when data arrive on serial port

That already happens in the Arduino core functions, if you want to grab that interrupt you may have to modify the ISR in HardwareSerial.cpp.

Another option may be to call serialEvent yourself, I don't think there's any reason to wait for the main loop to do it.


Rob

could you please give me a code example?
do tou think something like this could work bluetooth - Arduino Serial Interrupts - Stack Overflow

I don't have any example code I'm afraid, but that Stackoverflow thread seems about right, good place to start anyway.


Rob

what DO you think about connecting RX serial pin to a KNOWN pin, let's say pin P. and than i should attach an interrupt on pin P?

'and How do i call a serial event in the loop()?

It looks like void serialEvent() is weakly defined so you can add your own function

void serialEvent() {
   char c;
// what you want to do, for example
   if (Serial.available())  {
      c = Serial.read());
}

EDIT: Two minutes looking and I found this tute

You should learn to do research as well as coding.

about connecting RX serial pin to a KNOWN pin, let's say pin P. and than i should attach an interrupt on pin P?

That should work, you may also be able to use a pin-change interrupt on the Rx pin as well, but running Rx to one of the standard interrupt pins would be cleaner I think.


Rob

none of the above solutions has worked till now. thus i was thinking of editing USART/UARTClass cpp file, including a flag in the interrupt code of the usart that will help me wake up chip. what do you think about?

So you tried tying the Rx to an interrupt pin and that didn't work?


Rob

No, i couldn't becouse i'm not currently working on an Arduino DUE but on a SAM3X based board, therefor i can't make hardware changes, but i'll try that solution asap