Multi-variable communication between two Arduinos [RESOLVED]

I am currently constructing an ROV(Remotely Operated Vehicle) and am having some trouble with my code. I am using one Arduino Mega 2560 in my control board which will read the outputs of the various switches and potentiometers, and then send that information serially to my other Mega 2560 on the vehicle. This board on the vehicle will use the information to perform various tasks.

My problem is that so far, I've only been able to figure out how to send 8 bytes of data at a time. This is not enough because I need to send multiple potentiometer values and several High or Low switches.

My second thought was to try and send multiple serial transmissions one after the other and organize the data into arrays. (See Transmitter Code). While I have been able to receive this data on the receiving board, the information comes in a sporadic order, which is not useful to me. I'm guessing that is because one of my programs goes faster than the other. This is the code I've come up with so far.

Note: I'm using the normal Serial port to communicate with my laptop, and Serial1 to communicate between the Arduinos.

Transmitter Code

int outByte[4];
int x;

void setup() {
  Serial.begin(9600);
  Serial1.begin(115200);
  pinMode(11,INPUT);
  pinMode(12,INPUT);
}

void loop() {
  int y = 1;
  for (int i = 0; i > -1; i = i + y){
    outByte[0] = (digitalRead(11)); //Controls LED1 and is High or Low
    outByte[1] = (digitalRead(12)); //Controls LED2 and is High or Low
    outByte[2] = i;                 //Controls LED3 and is from 0 to 255
    for (x = 0; x <= 2; ++x){
      Serial.print(outByte[x]);     //Communicate with computer
      Serial1.write(outByte[x]);    //Communicate with other Arduino
      delay(1); 
    }
    Serial.println();
    if (i == 255) y = -1;
  }
}

Receiver Code

const int led1 = 11;
const int led2 = 12;
const int led3 = 13;
int values[4];
int i;
int incomingValue;

void setup(){
  int pin;
  for (pin = 11; pin <= 13; ++pin){
    pinMode (pin, OUTPUT);            //Set pins 11 to 13 as outputs
  }
  Serial.begin(9600);
  Serial1.begin(115200);
}
void loop(){
  if(Serial1.available()){
    for(i = 0; i < 3; i++){
      values[i] = Serial1.read();     //Read the three pieces of data and organize them in the array
      Serial.println(values[i]);      //Print these values to the Serial Monitor
      delay(1);
    }
  }
  digitalWrite(led1, values[0]);
  digitalWrite(led2, values[1]);
  analogWrite(led3, values[2]);
}

To make things simple, I've coded so that two led's on the receiving board are controlled by two switches, and the last led is supposed to fluctuate from 0-255.

Like I mentioned before, this code gives me sporadic results on my receiving board. Sporadic meaning that each of the three leds choose one data point randomly and display that value. I need help figuring out how to make sure that I receive the instructions for the leds in the correct order (led1, led2, led3, repeat).

Thank you for any help.

You need to assume that the sender and receiver will get out of sync due to lost bytes and errors, and design in some way for them to get back into sync. The usual way is to include some message framing so that the receiver can detect the start and end of a message. I see that you are using a binary encoding scheme rather than a textual one. That's fine, but means that it's harder to test and rules out the conventional approaches you'll see used in textual encoding schemes. One approach is to define a value to be used as the start of message marker - for example byte value zero. Write that at the start of your message, and at the end. In the receiver, discard your message until you reach a start-of-message marker, then read the expected number of bytes and confirm that the following byte contains the start-of-message marker - if you don't find it then assume the message may have been corrupted and discard it, and go back to looking for the next start-of-message.

Given that your transfer mechanism is unreliable you must design the higher level protocols on the assumption that any message could get lost.

You may want to work on the receiving code first, then develop the code that will send the commands. Below is some servo code that receives a command value from the sending application. The first part of the data is the servo command value, the second part is the servo identifier for the command, and the third part is the data string delimiter telling the arduino to act on the sent data. The code to send this data from pot positions should be fairly simple to develop.

//zoomkat 11-22-12 simple delimited ',' string parse 
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added 

String readString;
#include <Servo.h> 
Servo myservoa, myservob, myservoc, myservod;  // create servo object to control a servo 

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

  //myservoa.writeMicroseconds(1500); //set initial servo position if desired

  myservoa.attach(6);  //the pin for the servoa control
  myservob.attach(7);  //the pin for the servob control
  myservoc.attach(8);  //the pin for the servoc control
  myservod.attach(9);  //the pin for the servod control 
  Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded
}

void loop() {

  //expect single strings like 700a, or 1500c, or 2000d,
  //or like 30c, or 90a, or 180d,
  //or combined like 30c,180b,70a,120d,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        Serial.println(readString); //prints string to serial port out

        int n = readString.toInt();  //convert readString into a number

        // auto select appropriate value, copied from someone elses code.
        if(n >= 500)
        {
          Serial.print("writing Microseconds: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
          if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
          if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
          if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
        }
        else
        {   
          Serial.print("writing Angle: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.write(n);
          if(readString.indexOf('b') >0) myservob.write(n);
          if(readString.indexOf('c') >0) myservoc.write(n);
          if(readString.indexOf('d') >0) myservod.write(n);
        }
         readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

I got it to work! Thanks to both of you for replying. Your inputs were very useful. Here's my working code for anyone else who has the same problem.

Transmitter Code:

void setup() {
  Serial1.begin(115200);
  pinMode(11,INPUT);
  pinMode(12,INPUT);
}

void loop() {
  int y = 1;
  for (int i = 0; i > -1; i = i + y){
    byte outByte[5] = {255,digitalRead(11),digitalRead(12),i};  //Define the array, the first value is the message marker
    Serial1.write(outByte,5);                                   //Send the array.
    if (i == 254) y = -1;
    delay(5);                                                   //Delay so that you can see led3 fluctuating.
  }
}

Receiver Code:

const int led1 = 11;
const int led2 = 12;
const int led3 = 13;
int i;
int inval[5];
byte values[5] = {0,0,0,0};            //Start all bytes of the values array to 0.
  
void setup(){
  int pin;
  for (pin = 11; pin <= 13; pin++){
    pinMode (pin, OUTPUT);             //Set pins 11 to 13 as outputs.
  }
  Serial1.begin(115200);
}
void loop(){ 
  while (Serial1.available()<4) {}     // Wait until there are 4 bytes waiting.
  for(int n=0; n<4; n++){
    inval[n] = Serial1.read();         // Read the 4 bytes and put them in the incoming values array.
  }
  if (inval[0] == 255) {               //Check to make sure the message marker is the first byte.
    for (i=1; i<4; i++) {              //If it is, set the values array equal to the incoming values array.
      values[i] = inval[i];
    }
  }
  digitalWrite(led1, values[1]);
  digitalWrite(led2, values[2]);
  analogWrite(led3, values[3]);
}

I'm welcoming any suggestions on how to make this code better or any changes I should make. If you have any questions about this code, let me know.