Hello - I got a very good start on Serial communication between Ardunios thanks to this thread. I have successfully connected two nanos:
- Nano1 reads a potentiometer and sends a two-digit number with a line break over hardware serial to Nano2, every 250ms.
interval = 250;
void loop() {
if ((millis() - potentiometerMillis) > potentiometerIntervals) {
potentiometerMillis = millis();
Serial.println(potentiometerValue);
}
}
- Nano2 runs the recvWithStartEndMarkers() function from the link above, in an environment like this:
void loop() {
for (int i = 0; i < 10; i++) {
recvWithEndMarker();
doSomeLEDstuff(); //my function which alters the status of 20 LEDs
delay(50);
}
showNewNumber();
}
The above flow works perfectly. I can turn the potentiometer knob and see the serial data being updated in short order. It can run thousands of loops in a row without errors.
Now I am trying to add another Arduino in between them. This is an Arduino Mega and the goal would be for it to process inputs and send outputs over its various hardware serials. The reason for this, in my newbie brain, is that I have very limited memory (using around 80%) in the Nano2 which drives 200 LEDs.
My process now looks like this:
- Nano1 reads a potentiometer and sends a two-digit number with a line break over hardware serial to MEGA, every 250ms.
interval = 250;
void loop() {
if ((millis() - potentiometerMillis) > potentiometerIntervals) {
potentiometerMillis = millis();
Serial.println(potentiometerValue);
}
}
- MEGA reads the value and transmits it out to Nano 2, via code like this:
void loop() {
recv1WithEndMarker();
show1NewNumber();
Serial1.println(value1ToSend); //this sends the 2-digit number onwards to Nano2
}
- Nano2 runs the exact same loop as was working in the 2-Arduino example:
void loop() {
for (int i = 0; i < 10; i++) {
recvWithEndMarker();
doSomeLEDstuff(); //my function which alters the status of 20 LEDs
delay(50);
}
showNewNumber();
}
This 3-Ardunio chain works perfectly for a few seconds, and then the data gets garbled forever onwards. For example, if the number being sent from Nano1 is 49, Nano2 will ingest something like:
49
49
49
49
4994
444
4
444999994
etc.
I have confirmed that the Nano1 --> MEGA --> portion is working perfectly. That is, I can plug the MEGA into my laptop and use the serial monitor and see it is spitting out the proper 2-digit number (in this case, 49) over and over and over. So it seems like there is an issue with the Nano2 reading the data and processing it. I just cannot figure out why it works perfectly when Nano1 and Nano2 are connected, but fails when MEGA is in between but receiving and transmitting data at a very rapid pace.
Thank you for any tips you might have. I am also happy to paste the code from all 3 Arduinos if helpful, but there is a ton of unrelated stuff in there (like the LEDs) which I thought would be distracting.