Turning on/off 2 leds from different codes

Hi,
I have 2 sets of code(diffrent arduino(both mega), different timing), both are starting to do things once a button is pressed. I want both leds to turn on and off at the same time each loop, the button is connected to the same pin(with shared ground).
Both leds are connected to pin no.3.
What happens in my code is that it starts coordinated but with time I get a certain drift, and don't understand why. I tried to use the button timing to set them to start again each loop.
This is the code, the second code is complelty identical besides the second delay which is 5 instead of 2000:

int buttonPin = 22;

unsigned long roundStart = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("LEDS");
  setupPins();
  Serial.println("You can press now");
}

void loop() {
if (digitalRead(buttonPin) == LOW ) {
    Serial.println("start: ");
    digitalWrite(3,HIGH);
    Serial.println(String(millis()));
    roundStart = millis();
    while(1) {
        digitalWrite(3,HIGH);
        delay(2000);
        digitalWrite(3,LOW);
        delay(2000);
        Serial.println(String("time since beggening of session: " + String(millis() - roundStart)));
        resetTimes();
        roundStart = millis();
        Serial.println("round start: " + String(roundStart));
      } 
  }
}

bool resetTimes() {
  static unsigned long reachTime = 10000;
  static bool entered = false; 
  Serial.println("entering while loop " + String(millis()) + " " + String(roundStart + reachTime));
  while (millis() < roundStart + reachTime) {
     entered = true;
  }
  if (!entered) {
    Serial.println("Didn't go in!!!");
  }
  entered = false;
  Serial.println(String(millis() - roundStart) + " " + String(millis()));
  return true;
}

void setupPins() {
  pinMode(buttonPin, INPUT_PULLUP);
  
  for (int  i = 2 ; i < 16; i++ ) {
      pinMode(i, OUTPUT);
      digitalWrite(i, LOW);  
  }
  for (int  i = 44 ; i < 47; i++ ) {
      pinMode(i, OUTPUT);
      digitalWrite(i, LOW);  
  }
}

Code2:

int buttonPin = 22;

unsigned long roundStart = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("LEDS");
  setupPins();
  Serial.println("You can press now");
}

void loop() {
if (digitalRead(buttonPin) == LOW ) {
    Serial.println("start: ");
    digitalWrite(3,HIGH);
    Serial.println(String(millis()));
    roundStart = millis();
    while(1) {
        digitalWrite(3,HIGH);
        delay(2000);
        digitalWrite(3,LOW);
        delay(5);
        Serial.println(String("time since beggening of session: " + String(millis() - roundStart)));
        resetTimes();
        roundStart = millis();
        Serial.println("round start: " + String(roundStart));
      } 
  }
}

bool resetTimes() {
  static unsigned long reachTime = 10000;
  static bool entered = false; 
  Serial.println("entering while loop " + String(millis()) + " " + String(roundStart + reachTime));
  while (millis() < roundStart + reachTime) {
     entered = true;
  }
  if (!entered) {
    Serial.println("Didn't go in!!!");
  }
  entered = false;
  Serial.println(String(millis() - roundStart) + " " + String(millis()));
  return true;
}

void setupPins() {
  pinMode(buttonPin, INPUT_PULLUP);
  
  for (int  i = 2 ; i < 16; i++ ) {
      pinMode(i, OUTPUT);
      digitalWrite(i, LOW);  
  }
  for (int  i = 44 ; i < 47; i++ ) {
      pinMode(i, OUTPUT);
      digitalWrite(i, LOW);  
  }
}

Thanks!

Yeah, they will drift. I found out the hard way when doing an LED display with multiple processors. The simple fix was to make one the master processor. Then when it triggered the display, it flipped a digital pin high. All the rest of the processors started their display routines when they saw the digital line go high. Kept everything in sync.

The dragon fire display.

Above took something like six Adafruit pro-trinkets (Tiny Arduinos)

-jim lee

executing various delays and prints within a timing loop are going to affect it.

why not use millis() to wait a specific amount of time to toggle the LED state

#define pinLed  13
#define pinBut  A1

byte butStateLst;

void setup (void) {
    Serial.begin (115200);
    pinMode (pinLed, OUTPUT);
    pinMode (pinBut, INPUT_PULLUP);
    butStateLst = digitalRead (pinBut);
}

// -----------------------------------------------------------------------------
void loop (void) {
    static unsigned long msecLst = 0;
           unsigned long msec    = millis ();

    byte butState = digitalRead (pinBut);
    if (butStateLst != butState)  {
        butStateLst = butState;
        msecLst = millis ();
    }

#define Period 500
    if (0 < msecLst && msec - msecLst >= Period)  {
        msecLst = msec;
        digitalWrite (pinLed, ! digitalRead (pinLed));

        int sec  = msec / 1000;
        int min  = sec  / 60;

        char s [80];
        sprintf (s, "%2d:%02d:%03ld", min, sec % 60, msec % 1000);
        Serial.println (s);
    }
}

As suggested in the first reply, you will need a way to synchronize the two arduinos because the crystal/resonator frequency will be slightly different and affect the timing over a long term.

Why are you doing this with two boards? One arduino can easily blink multiple LEDs at different rates simultaneously.

Hi, thanks for all the replies, the problem was indeed drift between the arduinos.
I solved this by adding a shared digital IO as suggested, that only the master controlled(I distinguished between them with pull-down resistors) and other read in specific timing.
The master was the last arduino finishing the session, while the other finished and went for waiting a specific time until the digital pin was released.
Thanks again

Out of curiosity. Why do you use two Arduino-Megas for this project?
a single Arduino-Mega has 53 IO-pins So you could easily blink lot's of LEDs each at a different frequency
even so fast that you don't see them blink.
Even the smallest Arduino-Nano can blink hundreds of LEDs when using IO-expanders or shiftregisters.
So what's the reason to use two Arduino Megas?
best regards Stefan