Interrupt Driven Serial Communication

Hello all, I'm having a problem with my serial communications but first let me start by describing my setup:

I have a UAV that has 3 Arduino microcontrollers and a BeagleBoard. The three microcontrollers serve as follows: 1. ArduPilotMega (APM) - flight controller 2. Arduino Pro mini 16MHz - analog sensor reading 3. SeeeduinoMega 2560 - Serial communications relay between APM, Pro mini, BeagleBoard, and Xbee 900MHz

I'm wanting to put the analog sensor reading on the Seeeduino and get rid of the Pro mini. I did this and it works but as the Seeeduino reads the analog pins it misses some serial communications. The serial communications need to take priority over the analog readings and so an interrupt popped into my mind. I have used interrupts before on Arduino and they worked out fantastically. I found an example on avrfreaks.net for serial driven serial communication so the hardware I'm using should support it but I have been unsuccessful in my attempts to get it to work. I also came across an Arduino library called fastSerial which seemed like a possibility but I was unable to get that to compile.

Can you give me any help with how I may be able to give priority to the serial communication or speed up the analog reads?

Thanks in advance, lightbulb14

Serial comms on the Arduino already are interrupt driven.

So the following code will essentially do nothing until a byte is sent to a serial port? The way I understand it the program will be checking the if statements, which in my UAV case would be 4 if statements each time around which takes some time. I was looking for something similar to how the attachInterrupt() function works by breaking out of the main loop, executing some portion of code, and then returning to the main loop again.

void setup() {
// initialize both serial ports:
Serial.begin(9600);
Serial1.begin(9600);
}

void loop() {
// read from port 1, send to port 0:
if (Serial1.available()) {
int inByte = Serial1.read();
Serial.write(inByte);
}

// read from port 0, send to port 1:
if (Serial.available()) {
int inByte = Serial.read();
Serial1.write(inByte);
}
}

which in my UAV case would be 4 if statements each time around which takes some time.

About 20 microseconds for the four, I'm guessing.

lightbulb14: So the following code will essentially do nothing until a byte is sent to a serial port? Correct. The way I understand it the program will be checking the if statements, which in my UAV case would be 4 if statements each time around which takes some time. I was looking for something similar to how the attachInterrupt() function works by breaking out of the main loop, executing some portion of code, and then returning to the main loop again.

The hardware serial library already uses interrupts to both send and receive characters and save incoming and outgoing into small memory buffers (32 bytes each maybe?, it's in the library and can be increased if needed if you have SRAM to spare) So you only possibly run into trouble if your loop time consumes more time then 32 X 1 millisec (assuming your 9600 baud example) which is quite a long time normally, but only you know what your loop() cycle time is. The length of your serial messages is also a factor but it's all knowable if you do the basic math.

[/quote]