When the setup with two Arduino Unos communicating serially stops working after a period, especially in conjunction with receiving data from a PC (in your case, via Unity), several issues could be at play. Identifying the exact cause requires a bit of troubleshooting, but here are some common problems and potential solutions:
If the Arduinos are not sufficiently powered, communication can fail over time. This can happen if you’re powering one Arduino from another or if either is doing something power-intensive.
- Solution: Ensure both Arduinos have a stable and sufficient power supply. It might help to power each Arduino independently from a stable source.
The serial buffer on the Arduino is limited (64 bytes by default). If data is sent faster than it’s read, the buffer can overflow, causing data loss and potentially causing the system to hang.
- Solution: Ensure your code processes data quickly and efficiently. Implement checks to ensure the buffer does not overflow, and consider adding a delay if necessary to slow down the data transmission rate.
A common ground is essential for serial communication. However, if there are ground loops or interference, communication can be disrupted.
- Solution: Ensure a solid, common ground between the two Arduinos and the PC. Keep the wiring as short as possible to reduce the potential for interference.
ESD or other forms of electrical noise can disrupt serial communication, especially in environments with a lot of electronic devices or long cable runs.
- Solution: Use shielded cables for connections, especially if they are long. Also, consider adding ferrite beads or chokes to reduce noise.
SoftwareSerial has limitations, particularly at higher baud rates or under certain conditions where it might not reliably receive data.
- Solution: Ensure the baud rate is set to a value that is reliable for
SoftwareSerial (9600 or 19200 are usually safe). Avoid using pins that are known to be less reliable for SoftwareSerial. On the Uno, pins 10 and 11 are usually good choices, but performance can vary.
Loose connections or damaged wires can cause intermittent communication issues.
- Solution: Check all your connections and cables for any signs of wear or loose connections. Sometimes simply reseating a connection can resolve the issue.
If the protocol for sending and receiving data is not robust, errors can accumulate over time, causing failures.
- Solution: Implement error checking and possibly a handshaking protocol where the receiver sends back an acknowledgment or ready signal before the next piece of data is sent.
Unplugging the RX/TX cables causing the system to work again suggests a potential issue with the Arduinos entering a bad state.
- Solution: Implement a watchdog timer or a manual reset button in your setup that can reset the Arduinos if they stop responding to ensure they return to a known good state.
- Isolate the Problem: Try to determine if the issue lies with the sending or receiving side. You can do this by using simple test sketches that only send or only receive data.
- Simplify Your Setup: Reduce your setup to the simplest form that still exhibits the problem. This might mean testing each Arduino independently with the PC.
- Monitor Serial Traffic: If possible, use a serial monitor or a logic analyzer to see what’s happening on the serial lines when the failure occurs.
If none of these solutions resolve the issue, consider providing more detailed information about your setup, including how data is sent from Unity, the specifics of the wiring, and any other components involved in the setup.