Pages: [1] 2   Go Down
Author Topic: Two Arduinos, synchronize variables  (Read 1135 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 0
Posts: 147
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi guys.
I have Arduino Mega1280 and Arduino Uno r3.
I want both of them to use same variables with same values. If variable changes in one Arduino, I need it to be changed in the other Arduino also.
This communication should be two-way. Both can change and read all variables.
Is there any simple way to do that? I think serial communication would work, if I only knew how to set it up.
Thanks!
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26248
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How you go about this, and how successful it will be, depends on how much latency your system can tolerate.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Full Member
***
Karma: 0
Posts: 147
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Latency could be up to half seconds. Less is always better. If it's more than half seconds, then the system could be uncomfortable to use.
Most variables are booleans and there are about 60 of them. I'd also like to use about 15 integers.
Serial communication should be fast enough to achieve under half seconds with this much data?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26248
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

60 booleans is only eight bytes worth of data.
You don't say how big the integers are.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Full Member
***
Karma: 0
Posts: 147
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Integers should be 2 bytes?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26248
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

"int"s on the Arduino are two bytes, but "long"s are integers too, but are four bytes.
"char" is also an integer datatype.

After a little thought, so is "bool".
« Last Edit: September 08, 2012, 02:57:25 pm by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Full Member
***
Karma: 0
Posts: 147
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Alright.
I use unsigned ints with value up to 65535.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 481
Posts: 18737
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You could use I2C. Both ends can take turns being the master and thus initiate transfer of variables to the other side. The maximum number of bytes you can send/receive with the library in one "packet" is 32, however you could break things up, or maybe send something like 1-byte "variable number" followed by a value.

http://www.gammon.com.au/i2c
Logged


Smithfield, Rhode Island
Offline Offline
God Member
*****
Karma: 3
Posts: 843
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Create a C++ class to house all your data, with methods to set and get the data. The only way this data is written or read is through this class.

Now you have an easy way to know when a value has changed, since your code called a setter method. The setter method can call a private method in the class (or a friend class) to notify the other Arduino of the change. I would use I2C as nick suggested. You have the same class on the other end that reads the data and updates its copy.

This makes it sound easier than it is. If there is a master where the data always changes its not too bad to do. If the data can change on either end then you have to decide how to handle conflicts. Also, what if the data changes on side A while side B is in the process of using it? You'll need some kind of locking scheme.

I am actually working on something similar. The bulk of the data is on Arduino A and Arduino B is notified when a change happens. Data on Arduino B is in the nature of commands, so its not really the same data being changed.


 
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 147
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I already have program that works on Arduino1. It uses some variables to set output status. Now I need to change those same variables with Arduino2 that has ethernet shield connected to it. I want to change output statuses with direct inputs on Arduino1 and ethernet requests on Arduino2. Arduino2 also must monitor those variables and output their statuses. Arduino1 should always have higher priority. If time between changing same variable on both Arduinos in less than 3 seconds, then change should work only on Arduino1. If Arduino1 changes output status and after 3 seconds Arduino2 changes the same output status, then Arduino2 should take priority. All physical outputs are on Arduino1 and it does not have spare pins. I could get serial or I2C link, but that's all I have. I can't connect ethernet module to Arduino1.
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17293
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi guys.
I have Arduino Mega1280 and Arduino Uno r3.
I want both of them to use same variables with same values. If variable changes in one Arduino, I need it to be changed in the other Arduino also.
This communication should be two-way. Both can change and read all variables.
Is there any simple way to do that? I think serial communication would work, if I only knew how to set it up.
Thanks!

You might want to check out Bill Porter's 'EasyTransfer' library. It amounts to having both arduino sketches each define a structure of identical number of elements of identical lengths and the library handles the protocol of the automatic transfer. I believe his latest version(s) can use I2C as well as the original serial version. Good stuff.

 http://www.billporter.info/easytransfer-arduino-library/

Lefty
« Last Edit: September 08, 2012, 09:28:14 pm by retrolefty » Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 147
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I got Easytransfer working only one way. Can't get both sending and both receiving.
I just try to listen input 8 and then light LED.

Code for one Arduino:
Code:
#include <Wire.h>
#include <EasyTransferI2C.h>

EasyTransferI2C ET;

byte out11;

struct SEND_DATA_STRUCTURE{
  byte out11;
};
struct RECEIVE_DATA_STRUCTURE{
  byte out11;
};

SEND_DATA_STRUCTURE txdata;
RECEIVE_DATA_STRUCTURE rxdata;

void setup(){
  Wire.begin(1);
  ET.begin(details(txdata), &Wire);
  ET.begin(details(rxdata), &Wire);
  Wire.onReceive(receive);
  pinMode(13, OUTPUT); 
  pinMode(8, INPUT);
}

void loop() {
  txdata.out11 = digitalRead(8);

  ET.sendData(2);
 
  if(ET.receiveData()){
    out11 = rxdata.out11;
    if(out11 == 1) {
      digitalWrite(13, HIGH);
    } else {
      digitalWrite(13, LOW);
    }
  }
}

void receive(int numBytes) {}
Other:
Code:
#include <Wire.h>
#include <EasyTransferI2C.h>

EasyTransferI2C ET;

byte out11;

struct SEND_DATA_STRUCTURE{
  byte out11;
};
struct RECEIVE_DATA_STRUCTURE{
  byte out11;
};

SEND_DATA_STRUCTURE txdata;
RECEIVE_DATA_STRUCTURE rxdata;

void setup(){
  Wire.begin(2);
  ET.begin(details(txdata), &Wire);
  ET.begin(details(rxdata), &Wire);
  Wire.onReceive(receive);
  pinMode(13, OUTPUT); 
  pinMode(8, INPUT);
}

void loop() {
  txdata.out11 = digitalRead(8);

  ET.sendData(1);
 
  if(ET.receiveData()){
    out11 = rxdata.out11;
    if(out11) {
      digitalWrite(13, HIGH);
    } else {
      digitalWrite(13, LOW);
    }
  }
}

void receive(int numBytes) {}
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 147
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Any hints?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26248
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Any observations?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Full Member
***
Karma: 0
Posts: 147
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

1 way communication was working, but it had 1-2 second delay between pushing button on one Arduino and lighting a led on the other Arduino. I tried with 2 variables and both of them changed values.
Currently not working 2 way code isn't changing any variable.
I'm afraid I am doing something fundamentally wrong making it 2 way, but I can't figure out, what it is.
Logged

Pages: [1] 2   Go Up
Jump to: