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!
How you go about this, and how successful it will be, depends on how much latency your system can tolerate.
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?
60 booleans is only eight bytes worth of data.
You don't say how big the integers are.
Integers should be 2 bytes?
"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".
Alright.
I use unsigned ints with value up to 65535.
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.
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.
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.
Ragnar:
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.
Lefty
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:
#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:
#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) {}
Any hints?
Any observations?
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.
Ragnar:
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.
Does the library download have any example sketches showing how to implement 2 way communications?
Lefty