Serial Communication Between Two arduinos (Asking and Receiving) Can anyone help? I'm trying to develop two sketches, where data can only be sent when a request is made for it between two arduinos. A simple hello world message can suffice. I tried out the Serial Ascii Call and Response on the arduino IDE but it didn't work for me???. Do find below the code sketches

//Arduino Sketch 1
uint16_t alpha;
uint16_t lambo;
Banana nanas;    //to hold the Banana object
uint16_t dataArray[2];
void Setup(){
    Serial.begin(9600);
    establishContact();
}

void loop(){
    alpha=nanas.getRad;
    lambo = nanas.getBars;
    dataArray[0] = alpha;
    dataArray[1] = lambo;
    Serial.write((uint8_t*)dataArray, sizeof(dataArray));
    Serial.flush();   //clear the serial port         
}

void establishContact()
{
    if (Serial.available())
    {
        while (Serial.available()  &gt; 0)
        {
            int newByte = Serial.read();
            if (newByte == 'Q')
            {
                Serial.write(newByte);
                Serial.flush();
            }
        }
    }
}


The sketch for the other arduino is shown below

//Arduino sketch 2
static char buffer[2];
void setup(){
    Serial.begin(9600);
    Serial1.begin(9600);
    establishContact();
}
void loop(){
    if(Serial1.available())
    {
        while(Serial1.available() &gt; 0){
            uint16_t gondola1 = Serial1.read(); 
            uint16_t gondola2 = Serial1.read();
            uint16_t crixus = gondola2 &gt;&gt; 8;
            crixus = (crixus | gondola1);

            gondola1 = Serial1.read(); 
            gondola2 = Serial1.read();
            uint16_t mario = gondola2 &gt;&gt; 8;
            mario = (mario | gondola1);
            buffer[0] = crixus;
            buffer[1] = mario;
            Serial.write(buffer, sizeof(buffer);
            Serial1.flush();
        }
    }
}
void establishContact(){
    if (Serial.available()){
        uint16_t newByte = Serial.read();
        if(newByte == 'Q')
        {
            Serial.flush();
            Serial.write(newByte);
        }
    }
}
it is not really easy to follow your code but here is a "hello world" procedure to do an on demand data send between two arduinos:


upload this code to arduino uno before you make any connection on pins 0 and 1 of the board:

/**
 * Code for server side (arduino uno)
 */


uint8_t data2send = 'A'; //data to send on a received request 
uint8_t incomingByte;

void setup() {
        Serial.begin(9600);   
}

void loop() {
        // send data only when you receive data:
        if (Serial.available() &gt; 0) {

                // read the incoming byte:
                incomingByte = Serial.read();

                if(incomingByte=='Q'){  //if incoming data is the expected request (request code = 'Q' in this example)
                        Serial.write(data2send);        
                }
        }
}

connect:

UNO  | MEGA
------------
pin0 | pin18
pin1 | pin19
GND  | GND

upload to arduino mega this code:

 /**
 *  code for arduino mega.
 *  
 *  proxy between Serial0 and Serial1
 *  
 *  send data to Serial0 to forward it to Serial1.
 *  when data is received from Serial1 it will be forwarded to Serial0.
 */

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);
        }
}

open the mega's serial monitor and send some characters. Check that you receive back data only when you send a 'Q' character.