Running two Arduinos in sync

Hello,
I need to have two Arduinos running the same sketch start the same function in the code at the same time. The Arduinos are about 10 feet apart. What I thought might work would be to connect them in some way and have them react when I detach them from each other. The thing is, they don't have to be aware of each other in any way.
I'm not very adept with momentary switches, etc. just yet, but it seems that i might be able to emulate a button press or the like and then when the wire(s) connecting the two is detached, the sketch could be prompted to enter a new function.
Could someone suggest a way to do this, along with some code? I'd really appreciate it!

Thank you,
-Jody

Jflorkow:
The thing is, they don't have to be aware of each other in any way.

I don't tunderstand this bit?

If that is true, why do they need to be in sync?

How closely in sync do you want them to be?
Would it matter if one of them was 5 seconds ahead of the other?
Would it matter if the time difference changed?

...R

Thank you for your reply. In the code that I have now, neither board needs to know where in the code the other is, or even that there is an Arduino on the other end of the wire. I just need it to react when I disconnect the two from each other. Basically, I'd like something like this:

void loop() {
//Turn on some LEDs

//when you see that the wire has been disconnected, do this:

someFunction();

}

someFunction() {
//Turn on some different LEDs
}

Again, I don't know much about momentary switches, etc., (I'm not even sure if I'm going to phrase this correctly) but I was thinking that is I could make a pin HIGH by attaching to another Arduino and then have it go LOW when detaching, then there would be some way to have the code wait for the pin to go LOW and then go into another function.

If the same sketch is running on both boards, then they will both react at the same time to them being disconnected, so there should be no lag time.
To answer your question, seconds would be too long of a lag. Some milliseconds might be ok.

(I can try to briefly describe exactly what I'm doing:
I have two set pieces in a play. They both have some NeoPixel LEDs in them. The program they are running lights up the strings. (So far very simple - one command to tell them what colors to light up in. I need the strings to light up in an animated pattern at a certain time. I can do the pattern, but it is prompting them to start that pattern that is the issue. The set pieces are moved away from each other, and that is the time that I need the new pattern to start. Since they are moving away from each other, I thought it might be simple for one of the people moving them to simply disconnect a cable between the two. When the Arduino sees that it is no longer connected to something, it jumps to another function. Since they both get disconnected from each other at the same moment, they will be (should be) in sync. I hope that helps.)

Thank you,
-Jody

Set up a light detector on each board, in the base of a length of tube to restrict the view. Both point at a light that is OFF. When the light comes on, the boards may react in 10 usecs or less with the right code.
But expect one board to run faster especially UNO with non-crystal clock source. They can differ by a few seconds per day.

Just use an interrupt connection.
One board triggers a interrupt on the other, if the code is simple you can do it in the interrupt, if more complicated have it set x =1, when x is normally 0. If the value of x changed, run your code.
Depending on what your application is this won't be exactly the same time due to the processing of the interrupts and/or the time required to loop back for the value check. But as long as 5-30mSec isn't a big deal you won't be able to see the difference.

I thought of that but it is biased Interrupt has a cost that only one board pays. Make them both have the same simple trigger code.

Isn't it easier to di the job with asynch code?

Well, it seems like this would fit within the timing accuracy of millis(). The action only has to run once, after a period of seconds to minutes and the period is fixed in programming in advance.

Don't plug them into "each other". Think of having a switch on the Arduino which starts the timer. Then imagine that instead of a pushbutton switch, you plug it into a short circuit on the other half of the pair. The two Arduinos aren't aware of each other but they are aware of a shorting plug across their own input.

(See schematic attached)

The code for this is:

/* Two identical Arduinos Synchronised.
  The input is on pin 13 - grounded means don't do anything
  When pin 13 is allowed to float (unplugged) then wait 10 seconds and start doing stuff
  The cycle will repeat when pin 13 is grounded again.
*/
#define SYNC_PIN 13
void setup() {
  pinMode(SYNC_PIN, INPUT_PULLUP);
}
void loop() {
  while( ! digitalRead(SYNC_PIN)) {
    //do nothing while pin is held low
  }
  delay(10000); //10000ms = 10 seconds

  //do stuff!

  while(digitalRead(SYNC_PIN)) {
    //stop doing stuff and wait here until the plug is re-inserted
  }
}

Jflorkow:
neither board needs to know where in the code the other is, or even that there is an Arduino on the other end of the wire. I just need it to react when I disconnect the two from each other.

This is an entirely different requirement from what you said in your original post.

A simple way would be to have the two Arduinos exchange a character at regular intervals - maybe every 200 millisecs - (over a serial connection - wires or InfraRed) and when the link is broken they would both know that the communication had stopped.

If there is a person to do it, you could arrange for a switch on one of the Arduinos that caused it to stop sending the character.

...R

You could let 2 UNOs run Blink and see how long betfore they're not in synch.

I know it's an old thread, but I'm putting my solution on here because I've just come across this issue myself and needed a simple/beginner style solution. I'm running the same LED sequences on 2 Nano's so they work in tandem and some of the patterns merge at the correct time. I have a sketch with about 30 different animations which are called by function name via void loop. I have a small 'interlude' in between each display which clears the led's and does a little sequence of it's own in order to seperate out the main animations.

I wanted the two nano's to 'check in' with each other during the interlude and make sure they start the next function at the same time.

I'm not suggesting this is millisecond precision timing or anything like that. But if you just need a couple of controllers to wait a moment now and again to check if their buddy is ready, then this works great for me.

To do this I just defined an output pin and an input put in my sketch. it's the same for both Nano's so still only one sketch. You just connect pin 9 of one Nano to PIN 10 of the other Nano in both cases.

#define SYNC_PIN_OUT 9
#define SYNC_PIN_IN 10
int SyncState = 0; //I use this to read PIN 10 for HIGH (Which is PIN 9 on the other Nano
int ReadyState = 0; //I use this to define the status of the current Nano which is local PIN 9

void interlude(){digitalWrite(SYNC_PIN_OUT, HIGH); // This set the current Nano to 'HIGH' e.g. ready to go
do { SyncState = (digitalRead(SYNC_PIN_IN)); // This reads the state of PIN 10
ReadyState = (digitalRead(SYNC_PIN_OUT)); // This reads the state of PIN 9
} while (SyncState == 0 || ReadyState == 0); // If one of them is LOW then keep looping

// When both are HIGH the sketch will move out of the loop above and finishes off below.

// Run my functions in sync.
twizzle_my_shizzle();
eat_my_shorts();
peel_my_banana();

}

I popped some serial.print commands in there to see what it was doing.

SyncState - 0 ReadyState = 1
SyncState - 0 ReadyState = 1
SyncState - 0 ReadyState = 1
SyncState - 1 ReadyState = 1
twizzle_my_shizzle - complete
eat_my_shorts - complete
peel_my_banana - complete

SyncState - 0 ReadyState = 1
SyncState - 1 ReadyState = 1
twizzle_my_shizzle - complete
eat_my_shorts - complete
peel_my_banana - complete

Even 2 Nano's bought at the same place at the same time vary by 10/20/30 milliseconds each time.