Problem: Syncing two Arduinos Master/Slave

Thread Starter

d333gs

Joined Nov 7, 2020
14
This is the problem I am trying to solve:
I have two Arduinos , side by side running the same sketch for led panels which are also side by side.
It is an art work hanging on a wall.
Each Arduino is running two 16X16 panels .
The Arduinos need to work in sync to keep the pattern changes correct. Over time they get progressively out of sync.
I found this Master/Slave Tutorial on the Arduino site BUT it is a bit vague to me. I posted questions there but only getting vague answers.

https://www.arduino.cc/en/Tutorial/LibraryExamples/MasterReader#code


Has anyone had a similar problem syncing boards?
Any soultions ?

Much thanks
John
 

Papabravo

Joined Feb 24, 2006
21,225
A simple handshake will get you a first order synchronization. This will be good for some period of time before you have to repeat the handshake. How fast the two boards lose that synchronization will depend on the actual clock frequencies of the two boards and if there are any conditions (interrupts) that will compel them to take alternate paths through the programs. Since this is a tricky thing to code and debug it would be better to avoid the problem altogether. It doesn't sound like that is an answer you are prepared for, so go ahead and crack your nut on this one.
 

Thread Starter

d333gs

Joined Nov 7, 2020
14
A simple handshake will get you a first order synchronization. This will be good for some period of time before you have to repeat the handshake. How fast the two boards lose that synchronization will depend on the actual clock frequencies of the two boards and if there are any conditions (interrupts) that will compel them to take alternate paths through the programs. Since this is a tricky thing to code and debug it would be better to avoid the problem altogether. It doesn't sound like that is an answer you are prepared for, so go ahead and crack your nut on this one.
Thanks for the response, how do Arduinos shake hands?
 

ElectricSpidey

Joined Dec 2, 2017
2,774
That code you linked to is only a very basic example of I2C, and contains nothing about syncing.

How long does the pattern run? You may be able to use a simple interrupt to reset the patterns.
 

Thread Starter

d333gs

Joined Nov 7, 2020
14
Ok, so that would not reset the clocks?
An interrupt is what I have been doing by pulling the plug .........
The pattern runs 2 minutes , then goes black for 50 milliseconds ? FastLED.delay(50); Then flashes another pattern forFastLED.delay(150); then restarts.
Perhaps I could try an interrupt at that point?
How could I trigger it in both at the same time?
Thanks
 

Deleted member 115935

Joined Dec 31, 1969
0
Your suffering the fact that the crystal clocks on the boards are at slightly different frequencies.
so an easy example, assuming the arduino is working through a sequence.
u need some connection between the two Arduinos , good old IO pins,
A sends to B and B sends to A , call them signal AB and BA
you need to code both to monitor their input,
normally both arduinos output '1' ,
which ever one gets to the end of the sequence first , sets its output '0'. and restarts its sequence.
when ever either arduino sees its input low, it restarts its sequence.

that way the arduinos will be synchronised at the start of every sequence,
 

Papabravo

Joined Feb 24, 2006
21,225
Thanks for the response, how do Arduinos shake hands?
Each Arduino has a pair ofGPIO pins; one configured configured as an output, the other configured as an input. They are cross connected. An Arduino that is Idle an ready to synchronize reads both the input and the output and makes a decision:

O I Action
1 1 The other system is not ready to SYNCH, Set output low
0 1 This system is ready to SYNC, the other system is not. Take no other action(s)
1 0 The other system is ready to synch, or I have completed my task, If tasks complete, wait for input to go high. If I have not completed the handshake, Set my output low
0 0 Handshake complete, proceed with synchronized tasks. When complete, set output high, wait for the Input to go high

There are lot's of ways to implement this algorithm. It is a pretty standard algorithm in communication systems of all kinds.
You could also do it by exchanging characters in a communication system with adjustments for transfer latency.
 

ElectricSpidey

Joined Dec 2, 2017
2,774
No, it would not reset the clocks.

My idea is during the blank period set an output pin on the master (either high or low) and start a delay, then watch that pin with the slave using an interrupt.

When the slave sees the interrupt it sets a delay then starts the pattern.

Then you adjust the delays so both units start the pattern at the same time.

If the patterns go out of sync before the entire sequence of patterns, then you may have to use multiple pins, and multiple sync points.

But I'm just a beginner at coding.
 

KeithWalker

Joined Jul 10, 2017
3,090
Papabravo suggested the simplest solution.
Each arduino uses one input pin and one output pin for handshaking. The output pin of each is connected to the input pin of the other.
The handshake parts of the program on each arduino are identical. The output pins are initialized low.
At the beginning of the program loop, the output pin is set high, and the input pin is read.
The program on each arduino waits until its input pin goes high. When this happens, it initiates a short delay and then sets its output pin low and runs the program loop. The delay is to give the other arduino time to read its high input pin.
Which ever arduino program finishes first will wait for the other to finish before they both start again.
It's a very simple but elegant solution.
Regards,
Keith
 
Last edited:

Thread Starter

d333gs

Joined Nov 7, 2020
14
Thanks Papabravo & Keith

That would be like pin 4 connected to 5 then 5 to 4 on the slave?
Plus a GND connect wire?

Am I getting close here??????????????????

Master
void setup() {
pinMode(4, OUTPUT);
pinMode(5, INPUT);

Slave
pinMode(4,INPUT );
pinMode(5, OUTPUT);
}
// then ????????? Lost here
void loop()
digitalWrite
 

KeithWalker

Joined Jul 10, 2017
3,090
No. They are not master and slave. The handshake is identical in each program. Whichever one gets there first will wait for the other one. The "while" statement waits until pinin goes HIGH. The code below should be added to each arduino 's program. Pin 4 on each should be connected to pin 5 on the other.
Regards,
Keith

// Include any libraries, define variables and input/output pins used

#define pinin 4
#define pinout 5

void setup() {

pinMode (pinin, INPUT);
pinMode (pinout, OUTPUT);
digitalWrite(pinout, LOW);

// Setup anything else that needs to be setup.
}

void loop() {

digitalWrite(pinout, HIGH);

while (digitalRead (pinin) == (LOW));

delay (10);
digitalWrite(pinout, LOW);

// Run the rest of the program

}
 

Thread Starter

d333gs

Joined Nov 7, 2020
14
Big thanks Keith, but it is stalling after about 10 minutes .
This is how I added your code to my code ; https://pastebin.com/GB0td0N2
Connected digital pin 4 to 4 and D pin 5 to 5. For the hell of it I tried the Analog pins too....same stall happens.
I will get at it again tomorrow morning
Best
John
 

djsfantasi

Joined Apr 11, 2010
9,160
Big thanks Keith, but it is stalling after about 10 minutes .
This is how I added your code to my code ; https://pastebin.com/GB0td0N2
Connected digital pin 4 to 4 and D pin 5 to 5. For the hell of it I tried the Analog pins too....same stall happens.
I will get at it again tomorrow morning
Best
John
In a quick scan, it’s difficult to analyze without the led functions defined... in particular, this line looks funky...

Code:
CRGBArray<NUM_LEDS> leds;

I don’t recognize the use of <>; Did you mean []?

Secondly, are you sure that your braces are matched up correctly? There are a couple of places that don’t look right. Like here...

Code:
for(int i = -5; i<=3;i++){
  leds (0,95)= CHSV(-5,255,150);
  leds (464,512)= CHSV(-5,255,150);
   {for(int i = 3; i>=-5;i--){
   }
 

KeithWalker

Joined Jul 10, 2017
3,090
It's stalling because you are supposed to cross connect them 4 to 5 (output pin of one to the input pin of the other) NOT input to input and output to output!.
 

Thread Starter

d333gs

Joined Nov 7, 2020
14
Nuts lol, OK tried switched the pins, no stall but no sync........
Made two videos so you can see:

Start up

15Min latter
 

KeithWalker

Joined Jul 10, 2017
3,090
You may have damaged the output pins. When they were connected, with one high and the other low, there was nothing to limit the current between them.
Are you running both arduinos from the same supply? if not, then your pins will be OK but you will need to connect the two grounds to get it working. If they are on the same supply, all I can suggest is that you try a different pin for the output on each of them. Just Never connect two outputs together.
Good luck.
Keith
 

Thread Starter

d333gs

Joined Nov 7, 2020
14
Are you sure it didn't go full circle?
Hi ES Probably .......... I rewired the output to pin 3 from 5 incase it got damaged Re Kieth but no change. Now I extended the delay in Keiths code to delay (50) from (10) ......I watching it now and I will keep you updated
 
Top