1

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()  > 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() > 0){
            uint16_t gondola1 = Serial1.read(); 
            uint16_t gondola2 = Serial1.read();
            uint16_t crixus = gondola2 >> 8;
            crixus = (crixus | gondola1);

            gondola1 = Serial1.read(); 
            gondola2 = Serial1.read();
            uint16_t mario = gondola2 >> 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);
        }
    }
}
5
  • Please elaborate. What boards do you use? What libraries do you use?(code does not compile as it is) What connections you have made between the boards? Also, I cannot really understand what exactly you want to happen when which board recieves what from the other board? Send a message back to the sender's board? Send a message elsewhere?
    – foivaras
    Commented May 4, 2016 at 20:44
  • @foivaras..the boards in question are an arduino uno which the sensor is connected to and an arduino mega which is used to receive the data being sent, The connections are the usual tx/rx from the uno to serial1 on mega. there are no other libraries included. The Mega request for data from the uno..uno sees the request command and then sends data .. mega receives data and request for more data. Thats it in a nutshell Commented May 4, 2016 at 21:57
  • if you do not use any library then your code does not compile. Sketch one has an undeclared Servo type. Sketch two gives a redeclaration of uint16_t gondola1 error.
    – foivaras
    Commented May 4, 2016 at 22:52
  • @foivaras the Servo class I used is just a random name I adopted ...in hindsight I should have changed it to something else..i just wanted to show that there is a class that an object can be declared from, to call its respective methods. I'll edit the sketches out now Commented May 4, 2016 at 22:56
  • 1
    Setup() should be setup() in the first sketch? What exactly is Banana? Why complicate things, if all you want to do is send 'Hello world' on request? If that isnt your goal, then edit your post and say exactly what the sketches are for and exactly what you mean by it didn't work for me. Commented May 5, 2016 at 0:09

1 Answer 1

3

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() > 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.

1
  • generally speaking, if a given answer is satisfying you then you should mark it as accepted. See also faq about accepting answers. Thank you.
    – foivaras
    Commented May 5, 2016 at 8:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.