Go Down

Topic: Lifi synchronisation with Mega and Nano (Read 119 times) previous topic - next topic

timtimtim

Feb 21, 2018, 11:53 am Last Edit: Feb 21, 2018, 10:42 pm by Coding Badly
Hello,

i was trying to synch my Arduinos for measuring applications. You can see the schematic in the attachments.

Here's the code from the Mega:


Code: [Select]
void setup() {
  // put your setup code here, to run once:
  Serial.begin(1200);
  Serial1.begin(1200);
}

bool start = false;
char StartSignal = '\0';
char c = '\0';
byte go = 0b00000000;
byte ResetArduino = 0b00000000;
char RESARD = '\0';

void loop(){
  while (start == false)
  {
    StartSignal = Serial.read();
    if (StartSignal  == '1')
    {
      //StartSignal = '\0';
      start = true;
      Serial1.println('L');
    }
  }
  while (start == true)
  {
    if (Serial1.available())
    {
      go = Serial1.read();   
      c = go;
      go = 0b00000000;
     
      if (c == 'L')
      {
        c = '\0';
 
        //dosomestuff
       
        Serial.println('L');
        Serial1.println('Z');
       
      }
      else if (c == 'Z')
      {
        c = '\0';
       
        //dosomestuff
       
        Serial.println('Z');
        Serial1.println('L');
      }
    }
  if (Serial.available())                       
    {
      ResetArduino = Serial.read();
      RESARD = ResetArduino;
      if (RESARD = '!')
      {
        //RESARD = '\0';
        EmptyBuffer();
        asm volatile ("  jmp 0");
      }
      else{}
    }
  }
}



and this is the code from the Nano:


Code: [Select]
void setup() {
  // put your setup code here, to run once:
  Serial.begin(1200);
}

byte c = 0b00000000;
char arr = '\0';

void loop() {
  // put your main code here, to run repeatedly:
 
      if (Serial.available())
      {
        c = Serial.read();
        arr = c;
        c = 0b00000000;
 
        if (arr == 'L')
        {
          arr = '\0';
          Serial.println('L');
        }
        else if (arr == 'Z')
        {
          arr = '\0';
          Serial.println('Z');
        }
       
     }
}



the idea of the code is to start the measurement with a '1' send to the mega and stop it with a '!'. yes i know that
Code: [Select]
asm volatile ("  jmp 0");

isn't pretty but it works for me.

now, my problem is that i expect to recieve L and Z alternating. It works sometimes, but often  i recieve two 'L' or 'Z' and i cant explain myself why this is happening. If i stop and restart the sketch with a '!' and a '1' and repeat it multiple times, i will recieve L's and Z's alternating but as long as I recieve two of each it means the Arduinos aren't perfectly synchronized.

Output looks like this:
(perfect synch)
L
Z
L
Z
L
Z
...

(not perfect)
L
L
Z
Z
L
L
Z
Z
...


Does anyone has an idea why this happens?



Moderator edit: image embedded in post

Go Up