Recently I got two XBee modules. And after some struggle with serial comunication I finally understand how to share data between two Arduinos.
As a start I used 2 potenciometers to control the intensity of two LEDs and it works pretty well. :]
I noticed some lack of information about serial and wireless comunication between Arduinos, so, here´s my code, I hope you find it usefull
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// SENDER
int analogValue2, analogValue5, val2, val5;
void setup()
{
// start serial port at 19200 bps
Serial.begin(19200);
}
void loop()
{
// read analog input
analogValue2 = analogRead(2);
analogValue5 = analogRead(5);
val2 = map(analogValue2, 0, 1023, 253, 0); // 254 and 255 for SYNC
val5 = map(analogValue5, 0, 1023, 253, 0);
Serial.print(254, BYTE); //SYNC char
Serial.print(val2, BYTE);
Serial.print(255, BYTE); //SYNC char
Serial.print(val5, BYTE);
delay(150);
}
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// RECIEVER
byte incomingByte, sensor1, sensor2;
void setup() {
// start serial port at 19200 bps
Serial.begin(19200);
Serial.println(”Ready!”);
pinMode (5, OUTPUT);
pinMode (6, OUTPUT);
delay(1000);
}
void loop() {
if (Serial.available()) {
incomingByte = Serial.read();
Serial.print(int(incomingByte));
if ((int(incomingByte) == 254)) {
sensor1 = Serial.read();
Serial.print(”Sensor 1 = “);
Serial.print(int(sensor1));
}
if ((int(incomingByte) == 255)) {
sensor2 = Serial.read();
Serial.print(” Sensor 2 = “);
Serial.print(int(sensor2));
}
}
analogWrite (5, sensor1);
analogWrite (6, sensor2);
}
https://www.youtube.com/watch?v=1PxjZglyXYU