Two arduino serial connection

I kinda have a problem connecting two arduino's with each other. I've searched quite some time but so far I haven't found a solution (also not on the web). :-/

Anyway, this is my situation:

I've got two Arduino's which I want to communicate with each other (1-way communication) and with Max MSP (via USB).

  • 'The master Arduino': This Arduino obtains sensor data and sends this data to both Max MSP (by use of USB) and to the slave Arduino (I've connected TX with RX and the grounds)
  • 'The slave Arduino': This Arduino receives the sensor data and uses this data to adjust LED drivers. While testing this arduino was USB connected and powered, but in the final state it will be powered externally.
  • Data stream: The data stream is the same for the master-maxmsp commication and the master-slave communication and consists out of the following format:
a b c d e

Where a is a value between 0 and 360 (no fixed amount of digits!), b: 0-70, c: 0-70, d: 0 or 1, e: 1-20

So an example of the datastream is:

98 34 29 0 6
99 4 39 1 7
100 34 9 0 16
1 34 39 0 16

etc.

I got a test-setup of this working with the following source-codes:

Master:

#include <Wire.h>

void setup() 
{ 
  Serial.begin(9600); // start serial communication at 9600bps 
} 

void loop(){

Serial.print(random(360));
Serial.print(" ");
Serial.print(random(100));
Serial.print(" ");
Serial.print(random(70));
Serial.print(" ");
Serial.print(random(2));
Serial.print(" ");
Serial.println(random(7));

}

Slave:

int angle, radius, z, record, MaxPatch;
boolean Start = false;

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


void loop()
{
getMasterValues();
}

void getMasterValues(){//Version 2
  boolean Finish = false;
  int out = 0;
  int sp_counter = 0;
  int getValue;

  //Synchronize
  while (Serial.available() > 0 && Start == false) { 
    getValue = Serial.read();

    if (getValue == 10){
      Start = true;
    }  //Allow start! 
  } //End synchronize

    if (Start){//Allowed to start

      while ( Serial.available() > 0 && Finish == false) { 

      getValue = Serial.read();

      if ( getValue > 33){//Convert ascii numbers to dec

        out = out * 10 + ( getValue - 48 ); 

      }
      else if ( getValue == 32){//A space is send

        sp_counter++;

        if (sp_counter == 1){
          angle = out;
          out = 0; 
        } 
        else if (sp_counter == 2){
          radius = out;
          out = 0;  
        } 
        else if (sp_counter == 3){
          z = out; 
          out = 0;
        } 
        else if (sp_counter == 4){
          record = out;
          out = 0;  
        }
      }
      else if( getValue == 10 ){//A new line is send
        MaxPatch = out;
        sp_counter = 0;
        Finish = true;
        
      }
    }
  }
}

Anyway, this works perfect, until I add more code to this; code for obtaining the sensor data and for adjusting the led drives. The data on the slave side occasionally differs from the data send by the master. For example
Master send:

100 58 6 0 2
216 40 35 0 0
47 60 41 1 6
268 88 13 1 3
16 15 15 1 6
10 17 40 1 1
230 45 51 1 2
294 16 31 1 4
90 55 50 1 5
200 2 60 1 4
73 44 13 1 4
343 42 17 1 1

Slave receives:

100 58 6 0 0
216 40 35 0 0
47 60 41 1 -30303
6 15 15 1 6
1 17 40 1 1
23045 51 1 1 2
294 6 31 1 4
90 5 50 1 5
200 260 1 1 4
73 443 1 1 4
343 42 0 1 1

I've tried adding a delay to the master.

I think the problem is either in the code or in that the master arduino sends data but the slave arduino isn't ready to receive data yet, because it's busy with other functions. Anyone have a solution (for this) or a suggestion for a different approach?
:slight_smile: