Boards not in sync

Hey,

I'm working on a project for my year 12 systems class were two boards will be sending data to each other, its meant to resemble something like a basic computer but It probably won't be that successful. I tried to send numbers in binary from board A to board B, The way I went about this was for board A to send through a pulse to board B when there was a 1 in the binary and send through no pulse when there was a 0. for example if board A wanted to send through the number 1 (which is 00110001 in binary) the output pin would be low for two cycles of the code, then be high for two cycles, then be low for three more and then be high one more time, and board B would read this as a 1. This didn't work however.

I wrote a piece of code to test if the boards were in sync.
I gave a long a value of 50000 and subtracted 1 from this every cycle and when it reached 0 It would turn an led on and reset the value of the long to 50000 which would again be subtracted by 1 until it got to 0 and then the led would then turn off. Basically a blinking led but with 50000 cycles and not with seconds. I put this code on both my broads and they did NOT blink in time with each other, same code but difference blink timers. Could this be why my binary 1 didn't work? because board A is sending over 00110001 but board B is reading to slow and reads it something like 001001 skipping over important pulses that would complete the number? Is there anyway around this or to fix this?

Also if you haven't guessed i'm pretty new to Arduino and any suggestions on how I should go about this project would be appreciated.

The boards I'm using are a Mega 2560 clone and a UNO R3 clone.

Include the Sketches for Board A and B but PLEASE use code tags ( </> )

Also if you have a schematic it may also be useful.

Alright, but perhaps I should also explain what I was doing a bit better, I built a 3 by 3 display out of LEDs and was trying to make board A tell board B which LED to light up, eg the top middle LED. To do this I assigned the value of 10 to the top row of LEDs, 20 to the middle row of LEDs and 30 to the bottom row of LEDs. I also gave the left column a value of 1, the middle column a value of 2 and the right column a value of 3. This is what board A is sending over to board B, a coordinate. So if board A sent over the number 31, the bottom left LED would light up. never got that far though.

heres the sketch for Board B

int reader = 13;
int computeCount = 1;
int digit = 2;
int charCount = 1;
int x = 2;
int y = 20;
bool charOne = false;
bool charTwo = false;
bool charThree = false;
bool charFour = false;
bool charFive = false;
bool charSix = false;
bool charSeven = false;
bool charEight = false;
bool compute = false;
int a1 = 0;
int a2 = 1;
int a3 = 2;
int b1 = 3;
int b2 = 4;
int b3 = 5;
int c1 = 6;
int c2 = 7;
int c3 = 8;
int ledControl = 9;

void setup() 
{
pinMode(reader, INPUT);
pinMode(a1,OUTPUT);
pinMode(a2,OUTPUT);
pinMode(a3,OUTPUT);
pinMode(b1,OUTPUT);
pinMode(b2,OUTPUT);
pinMode(b3,OUTPUT);
pinMode(c1,OUTPUT);
pinMode(c2,OUTPUT);
pinMode(c3,OUTPUT);
pinMode(ledControl, OUTPUT);
}

void loop() 
{
codeRead();       //This is the code from board A to let board B know its trying to send something to it.
codeStandby();    //This is the code from board A to let board B know its the end of the message.
readCode();       //This is where ardunio puts the pulses from board A together to form a number in binary.
convertBinary();  //This is where ardunio turns the number sent into a coordinates.
printXY();        //This is the output to the 3 by 3 led display I made.
}

void codeRead()
{
  if (digitalRead(reader) == HIGH and compute == false)
  {
    computeCount = computeCount + 1;
  }
  if (digitalRead(reader) == LOW and compute == false)
  {
    computeCount = 1;
  }
  if (computeCount == 8 and compute == false)
  {
    compute = true;
    computeCount = 1;
  }
}

void codeStandby()
  {
  if (digitalRead(reader) == LOW and compute == true)
  {
    computeCount = computeCount + 1;
  }
  if (digitalRead(reader) == HIGH and compute == true)
  {
    computeCount = 1;
  }
  if (computeCount == 8 and compute == true)
  {
    compute = false;
    computeCount = 1;
  }
  }

void readCode()
{
  if (digitalRead(reader) == LOW and compute == true and charCount == 1)
  {
    charOne = false;
  }
  if (digitalRead(reader) == HIGH and compute == true and charCount == 1)
  {
    charOne = true;
  }
  if (digitalRead(reader) == LOW and compute == true and charCount == 2)
  {
    charTwo = false;
  }
  if (digitalRead(reader) == HIGH and compute == true and charCount == 2)
  {
    charTwo = true;
  }
  if (digitalRead(reader) == LOW and compute == true and charCount == 3)
  {
    charThree = false;
  }
  if (digitalRead(reader) == HIGH and compute == true and charCount == 3)
  {
    charThree = true;
  }
  if (digitalRead(reader) == LOW and compute == true and charCount == 4)
  {
    charFour = false;
  }
  if (digitalRead(reader) == HIGH and compute == true and charCount == 4)
  {
    charFour = true;
  }
  if (digitalRead(reader) == LOW and compute == true and charCount == 5)
  {
    charFive = false;
  }
  if (digitalRead(reader) == HIGH and compute == true and charCount == 5)
  {
    charFive = true;
  }
  if (digitalRead(reader) == LOW and compute == true and charCount == 6)
  {
    charSix = false;
  }
  if (digitalRead(reader) == HIGH and compute == true and charCount == 6)
  {
    charSix = true;
  }
  if (digitalRead(reader) == LOW and compute == true and charCount == 7)
  {
    charSeven = false;
  }
  if (digitalRead(reader) == HIGH and compute == true and charCount == 7)
  {
    charSeven = true;
  }
  if (digitalRead(reader) == LOW and compute == true and charCount == 8)
  {
    charEight = false;
  }
  if (digitalRead(reader) == HIGH and compute == true and charCount == 8)
  {
    charEight = true;
  }
  charCount = charCount + 1;
  if (charCount == 9)
  {
    charCount = 1;
    digit = digit + 1;
    if (digit == 3)
    {
      digit = 1;
    }
  }
}

void convertBinary()
{
  if (digit == 1 and charOne == false and charTwo == false and charThree == true and charFour == true and charFive == false and charSix == false and charSeven == false and charEight == true)
  {
    x = 1;
  }
  if (digit == 1 and charOne == false and charTwo == false and charThree == true and charFour == true and charFive == false and charSix == false and charSeven == true and charEight == false)
  {
    x = 2;
  }
  if (digit == 1 and charOne == false and charTwo == false and charThree == true and charFour == true and charFive == false and charSix == false and charSeven == true and charEight == true)
  {
    x = 3;
  }
  if (digit == 2 and charOne == false and charTwo == false and charThree == true and charFour == true and charFive == false and charSix == false and charSeven == false and charEight == true)
  {
    y = 10;
  }
  if (digit == 2 and charOne == false and charTwo == false and charThree == true and charFour == true and charFive == false and charSix == false and charSeven == true and charEight == false)
  {
    y = 20;
  }
  if (digit == 2 and charOne == false and charTwo == false and charThree == true and charFour == true and charFive == false and charSix == false and charSeven == true and charEight == true)
  {
    y = 30;
  }
}

void printXY()
  {
  if (x + y == 11)
  {
    digitalWrite(a1, HIGH);
  }
  else 
  {
    digitalWrite(a1, LOW);
  }
  if (x + y == 21)
  {
    digitalWrite(a2, HIGH);
  }
  else
  {
    digitalWrite(a2, LOW);
  }
  if (x + y == 31)
  {
    digitalWrite(a3, HIGH);
  }
  else
  {
    digitalWrite(a3, LOW);
  }

  if (x + y == 12)
  {
    digitalWrite(b1, HIGH);
  }
  else 
  {
    digitalWrite(b1, LOW);
  }
  if (x == 22)
  {
    digitalWrite(b2, HIGH);
  }
  else
  {
    digitalWrite(b2, LOW);
  }
  if (x == 32)
  {
    digitalWrite(b3, HIGH);
  }
  else
  {
    digitalWrite(b3, LOW);
  }

  if (x + y == 13)
  {
    digitalWrite(c1, HIGH);
  }
  else 
  {
    digitalWrite(c1, LOW);
  }
  if (x + y == 23)
  {
    digitalWrite(c2, HIGH);
  }
  else
  {
    digitalWrite(c2, LOW);
  }
  if (x + y == 33)
  {
    digitalWrite(c3, HIGH);
  }
  else
  {
    digitalWrite(c3, LOW);
  }
}

And here's the sketch for board A

int writer = 52;
int x = 1;
int y = 10;
int charCount = 1;
int digit = 1;
int computeCount = 1;
bool isWriting = false;
bool startReading = true;
bool startWriting = false;

void setup() 
{
pinMode(writer, OUTPUT);
}

void loop() 
{
codeRead();     //This is the code board A sends to board B to let it know its trying to send something to it.
writeDigits();  //This is in control of the output pin to board B.
}

void codeRead()
{
  if (startReading == true)
  {
    digitalWrite(writer, HIGH);
    computeCount = computeCount + 1;
    isWriting = true;
  }
  if (computeCount == 9)
  {
    digitalWrite(writer, LOW);
    computeCount = 1;
    startReading = false;
    startWriting = true;
    isWriting = false;
  }
}
void calculateCord()
{
  
}

void writeDigits()
{
  if (startWriting == true and digit == 1 and x == 1 and charCount == 1)
  {
  digitalWrite(writer, LOW);
  charCount = charCount + 1;
  isWriting = true;
  }
  if (startWriting == true and digit == 1 and x == 1 and charCount == 2)
  {
  digitalWrite(writer, LOW);
  charCount = charCount + 1;
  }
  if (startWriting == true and digit == 1 and x == 1 and charCount == 3)
  {
  digitalWrite(writer, HIGH);
  charCount = charCount + 1;
  }
  if (startWriting == true and digit == 1 and x == 1 and charCount == 4)
  {
  digitalWrite(writer, HIGH);
  charCount = charCount + 1;
  }
  if (startWriting == true and digit == 1 and x == 1 and charCount == 5)
  {
  digitalWrite(writer, LOW);
  charCount = charCount + 1;
  }
  if (startWriting == true and digit == 1 and x == 1 and charCount == 6)
  {
  digitalWrite(writer, LOW);
  charCount = charCount + 1;
  }
  if (startWriting == true and digit == 1 and x == 1 and charCount == 7)
  {
  digitalWrite(writer, LOW);
  charCount = charCount + 1;
  }
  if (startWriting == true and digit == 1 and x == 1 and charCount == 8)
  {
  digitalWrite(writer, HIGH);
  charCount = 1;
  }

  if (startWriting == true and digit == 1 and x == 2 and charCount == 1)
  {
  digitalWrite(writer, LOW);
  charCount = charCount + 1;
  isWriting = true;
  }
  if (startWriting == true and digit == 1 and x == 2 and charCount == 2)
  {
  digitalWrite(writer, LOW);
  charCount = charCount + 1;
  }
  if (startWriting == true and digit == 1 and x == 2 and charCount == 3)
  {
  digitalWrite(writer, HIGH);
  charCount = charCount + 1;
  }
  if (startWriting == true and digit == 1 and x == 2 and charCount == 4)
  {
  digitalWrite(writer, HIGH);
  charCount = charCount + 1;
  }
  if (startWriting == true and digit == 1 and x == 2 and charCount == 5)
  {
  digitalWrite(writer, LOW);
  charCount = charCount + 1;
  }
  if (startWriting == true and digit == 1 and x == 2 and charCount == 6)
  {
  digitalWrite(writer, LOW);
  charCount = charCount + 1;
  }
  if (startWriting == true and digit == 1 and x == 2 and charCount == 7)
  {
  digitalWrite(writer, HIGH);
  charCount = charCount + 1;
  }
  if (startWriting == true and digit == 1 and x == 2 and charCount == 8)
  {
  digitalWrite(writer, LOW);
  charCount = 1;
  }

  if (startWriting == true and digit == 1 and x == 1 and charCount == 1)
  {
  digitalWrite(writer, LOW);
  charCount = charCount + 1;
  isWriting = true;
  }
  if (startWriting == true and digit == 1 and x == 1 and charCount == 2)
  {
  digitalWrite(writer, LOW);
  charCount = charCount + 1;
  }
  if (startWriting == true and digit == 1 and x == 1 and charCount == 3)
  {
  digitalWrite(writer, HIGH);
  charCount = charCount + 1;
  }
  if (startWriting == true and digit == 1 and x == 1 and charCount == 4)
  {
  digitalWrite(writer, HIGH);
  charCount = charCount + 1;
  }
  if (startWriting == true and digit == 1 and x == 1 and charCount == 5)
  {
  digitalWrite(writer, LOW);
  charCount = charCount + 1;
  }
  if (startWriting == true and digit == 1 and x == 1 and charCount == 6)
  {
  digitalWrite(writer, LOW);
  charCount = charCount + 1;
  }
  if (startWriting == true and digit == 1 and x == 1 and charCount == 7)
  {
  digitalWrite(writer, HIGH);
  charCount = charCount + 1;
  }
  if (startWriting == true and digit == 1 and x == 1 and charCount == 8)
  {
  digitalWrite(writer, HIGH);
  charCount = 1;
  }

  if (startWriting == true and digit == 2 and y == 10 and charCount == 1)
  {
  digitalWrite(writer, LOW);
  charCount = charCount + 1;
  }
  if (startWriting == true and digit == 2 and y == 10 and charCount == 2)
  {
  digitalWrite(writer, LOW);
  charCount = charCount + 1;
  }
  if (startWriting == true and digit == 2 and y == 10 and charCount == 3)
  {
  digitalWrite(writer, HIGH);
  charCount = charCount + 1;
  }
  if (startWriting == true and digit == 2 and y == 10 and charCount == 4)
  {
  digitalWrite(writer, HIGH);
  charCount = charCount + 1;
  }
  if (startWriting == true and digit == 2 and y == 10 and charCount == 5)
  {
  digitalWrite(writer, LOW);
  charCount = charCount + 1;
  }
  if (startWriting == true and digit == 2 and y == 10 and charCount == 6)
  {
  digitalWrite(writer, LOW);
  charCount = charCount + 1;
  }
  if (startWriting == true and digit == 2 and y == 10 and charCount == 7)
  {
  digitalWrite(writer, LOW);
  charCount = charCount + 1;
  }
  if (startWriting == true and digit == 2 and y == 10 and charCount == 8)
  {
  digitalWrite(writer, HIGH);
  charCount = 1;
  isWriting = false;
  }

  if (startWriting == true and digit == 2 and y == 20 and charCount == 1)
  {
  digitalWrite(writer, LOW);
  charCount = charCount + 1;
  }
  if (startWriting == true and digit == 2 and y == 20 and charCount == 2)
  {
  digitalWrite(writer, LOW);
  charCount = charCount + 1;
  }
  if (startWriting == true and digit == 2 and y == 20 and charCount == 3)
  {
  digitalWrite(writer, HIGH);
  charCount = charCount + 1;
  }
  if (startWriting == true and digit == 2 and y == 20 and charCount == 4)
  {
  digitalWrite(writer, HIGH);
  charCount = charCount + 1;
  }
  if (startWriting == true and digit == 2 and y == 20 and charCount == 5)
  {
  digitalWrite(writer, LOW);
  charCount = charCount + 1;
  }
  if (startWriting == true and digit == 2 and y == 20 and charCount == 6)
  {
  digitalWrite(writer, LOW);
  charCount = charCount + 1;
  }
  if (startWriting == true and digit == 2 and y == 20 and charCount == 7)
  {
  digitalWrite(writer, HIGH);
  charCount = charCount + 1;
  }
  if (startWriting == true and digit == 2 and y == 20 and charCount == 8)
  {
  digitalWrite(writer, LOW);
  charCount = 1;
  isWriting = false;
  }

  if (startWriting == true and digit == 2 and y == 10 and charCount == 1)
  {
  digitalWrite(writer, LOW);
  charCount = charCount + 1;
  }
  if (startWriting == true and digit == 2 and y == 10 and charCount == 2)
  {
  digitalWrite(writer, LOW);
  charCount = charCount + 1;
  }
  if (startWriting == true and digit == 2 and y == 10 and charCount == 3)
  {
  digitalWrite(writer, HIGH);
  charCount = charCount + 1;
  }
  if (startWriting == true and digit == 2 and y == 10 and charCount == 4)
  {
  digitalWrite(writer, HIGH);
  charCount = charCount + 1;
  }
  if (startWriting == true and digit == 2 and y == 10 and charCount == 5)
  {
  digitalWrite(writer, LOW);
  charCount = charCount + 1;
  }
  if (startWriting == true and digit == 2 and y == 10 and charCount == 6)
  {
  digitalWrite(writer, LOW);
  charCount = charCount + 1;
  }
  if (startWriting == true and digit == 2 and y == 10 and charCount == 7)
  {
  digitalWrite(writer, HIGH);
  charCount = charCount + 1;
  }
  if (startWriting == true and digit == 2 and y == 10 and charCount == 8)
  {
  digitalWrite(writer, HIGH);
  charCount = 1;
  isWriting = false;
  }
}

You will have great difficulty syncing two boards that do not run at exactly the same frequency and that are not started at exactly the same time using your approach.

All serial protocols (i2c, spi, normal serial) implement a form of sync.