I have attached the grounds on the two arduino boards to each other.
The xbees can send data fine, but it appears the second arduino in the chain is not outputing to the serial at all.
I assume the problem is in the code, but it is not obvious to me. Any advice would be much appreciated!
Code of first 'sender' arduino:
// Sender:
int analogValue0, val0;
void setup() {
// Serial port enable
Serial.begin(115200);
}
void loop() {
// read analog pin 0
analogValue0 = analogRead(0);
// send the value to the serial port
Serial.println(analogValue0, DEC);
}
Code of second 'receiver' arduino:
// RECIEVER
int incomingByte;
void setup() {
// Serial port enable
Serial.begin(115200);
}
void loop() {
// if there is bytes available coming from the serial port
if (Serial.available()) {
// set the values to the 'incomingByte' variable
incomingByte = Serial.read();
Serial.println(incomingByte, DEC);
}
}
@MikMo: Thanks for your message, the xbee ground is connected to the second arduino. I have tested the xbee part of the set-up and it appears to be working.
Hi cr0sh, that is a good question. The reason is that once I have this setup working I am going to replace the first arduino in the chain with this:
It has a ATmega168 on board, and code can be uploaded to it via the arduino IDE. Rx/Tx appears to be the only form of communication available on this board.
@Mikmo: thanks for your message. I tested all the connections as you suggested and they are working as expected. I rearranged the power supply (so both boards are powered from the same battery), and I think this was the trick for being able to receive the first board's input.
I also had to change the receiving board's code:
// RECIEVER
int incomingByte1, incomingByte2, incomingByte3, incomingByte4, incomingByte5;
void setup() {
// Serial port enable
Serial.begin(115200);
}
void loop() {
// if there is bytes available coming from the serial port
if (Serial.available() > 0) {
// set the values to the 'incomingByte' variable
incomingByte1 = Serial.read();
incomingByte2 = Serial.read();
incomingByte3 = Serial.read();
incomingByte4 = Serial.read();
incomingByte5 = Serial.read();
}
Serial.print(char(incomingByte1));
Serial.print(char(incomingByte2));
Serial.print(char(incomingByte3));
Serial.print(" ");
Serial.println(analogRead(0), DEC);
}
These changes were required because the serial.read command is outputting the three digits of the original int in three bytes and two formatting characters. It seems that the formatting characters need to be read from the serial buffer or the whole sequence gets mixed up.