Synchronizing multiple arduino (using interrupts)

Hello all,

I am new in this forum, so maybe my question is at the wrong subforum.

I am trying to sync multiple arduino with eachother, starting with two ofcourse.
My plan is to keep the code modular, meaning the code of one arduino is the same as the one of the other.

My problem: I found a very simple way to synchronize a arduino to another one. But, this is using the master/slave principle. One arduino his Looptime is set whereas the other has it time changed.
Is there a way to make it more modular?

Code:

int  LoopTime = 5000; 
volatile unsigned long InterruptTime = 0;//Looptime in microseconds
unsigned long  Starttime = 0;

void setup() {
  Serial.begin(115200) ;
  attachInterrupt(0, InterruptBaton, RISING); //Watchdog
  pinMode(9, OUTPUT);
}

void loop() {
  Starttime = micros();

  delay(2);

  while(micros() <= (InterruptTime))  { } //waiting till time has passed
  
  digitalWrite(9,255); //Generate pulse for other arduino
  digitalWrite(9,0);

  Serial.println((micros()-Starttime)); //measure Looptime

}

void InterruptBaton() {
  InterruptTime = LoopTime+micros();
}

Hi, welcome to the forum.

This part of the code is not okay. I don't know about the rest of the code.

  digitalWrite(9,255); //Generate pulse for other arduino
  digitalWrite(9,0);

A digitalWrite uses HIGH or LOW, not 255 and 0.

A single Master and many modules would be my first choice. Or do you want to synchronize just any modules with each other ? That would alsmost be like a software PLL.

Can you tell about your project ? What needs to be synchronized ? How much delay is allowed ? How many modules ? How long are the wires between them ? What is the timing range of the sync signal ? and so on.

Well, in theory it would be possible. But each arduino would have 2 sub-routines and a master will always be created.

On startup the arduino will "listen" on the bus for a duration of twice the normal time between each sync pulse. When no pulse is detected that arduino will act as master (Subrouting A) and give out the pulses. When a pulse is detected, it will act as slave and sync on those pulses (sub-routine B).

So it's do-able!

Peter_n:
A digitalWrite uses HIGH or LOW, not 255 and 0.

Don’t they mean the same in output? Changed them now though.

Peter_n:
Can you tell about your project ? What needs to be synchronized ? How much delay is allowed ? How many modules ? How long are the wires between them ? What is the timing range of the sync signal ? and so on.

Project: synchronize two or more actuator position with eachother, position and speed will be controlled by CAN-bus
Modules: 2+
Wires: For now not that long <0.5m
Sync signal: I want to keep my looptime as low as possible (for better controlling) probably 5 - 15ms

C-F-K:
On startup the arduino will “listen” on the bus for a duration of twice the normal time between each sync pulse. When no pulse is detected that arduino will act as master (Subrouting A) and give out the pulses. When a pulse is detected, it will act as slave and sync on those pulses (sub-routine B).

Adjusted it, do you mean something like this? Or am I missing something

volatile unsigned long  LoopTime = 5000; 
volatile unsigned long InterruptTime = 0;//Looptime in microseconds
volatile unsigned long  Starttime = 0;
volatile bool Master = 0;

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

  attachInterrupt(0, InterruptMasterSlave, RISING); //Master/Slave????
  for(int i = 0; i <= 1000; i++){
    Starttime = micros();
    while(micros() <= (Starttime+LoopTime))  { }
  }

  attachInterrupt(0, InterruptBaton, RISING); //Watchdog
  pinMode(9, OUTPUT);
}

void loop() {
  Starttime = micros();

  //Do stuff

  while(micros() <= (InterruptTime))  { }
  digitalWrite(9,HIGH);
  digitalWrite(9,LOW);
  Serial.println((micros()-Starttime));

}

void InterruptBaton() {
  if(Master) {
    InterruptTime = LoopTime+Starttime;
  }
  else {
    InterruptTime = micros()+LoopTime;
  }
}

void InterruptMasterSlave(){
  Master = 1;
}

What exactly do you mean by "sync" ?

What do you want to keep in step?

What amount of out-of-sync error is acceptable ?

How often do wish to update the sync signal - perhaps just once when the system starts?

What do you mean by "looptime"?

If you mean the time between successive iterations of the loop() function then I would say that 5 millisecs is a long interval - that would only be 200 iterations per second.

On the other hand, I can't immediately see a need to update a sync signal more than (say) once per minute - maybe only once per 30 minutes.

...R

I would like to synchronize the loop iterations. Why?: The position the actuator follows is dependent on the loop iteration of the arduino.

Reason why I want to update the sync signal every iteration: I also use it as watchdog > No signal means that the previous arduino failed > thus the system has to stop and produce an error via the CAN-bus.

5 millisecs is long but, I have some floating point calculations and I increase the sensor input by oversampling (takes 0,5ms per total measurement)

maybe a couple of iteration per hour, maybe a bit more is acceptable, because they won’t be driven all the time