In a project, I am receiving the serial data at Arduino UNO (Receive Only Mode through PIN 0) and processing/printing the received data continuously. I want to detect serial receive failure. Means, in case it (Receiver Arduino) did not receive any serial data for 5 seconds, I want to turn its PIN 3 as HIGH. If it is receiving any serial data then I want to keep PIN 3 as LOW.
Failure/interruption in serial receive data may be due to any external reasons like physical disconnection of Receive wire, failure of Sender/transmitter Arduino etc.
Please guide, Thanks in Advance.
If Serial.available() returns zero save the value of millis() to a variable as the start time, else set the variable to zero and pin 3 to LOW.
Later in loop(), if the start time is not zero (ie timing is taking place) test how long has elapsed since the start time and if it is greater than the required period then set pin 3 HIGH
UKHeliBob:
If Serial.available() returns zero save the value of millis() to a variable as the start time, else set the variable to zero and pin 3 to LOW.
Later in loop(), if the start time is not zero (ie timing is taking place) test how long has elapsed since the start time and if it is greater than the required period then set pin 3 HIGH
I think that is missing the necessary part that the timing variable must be set to 0 whenever Serial.available() is not zero.
Also, I think I would do the logic the other way round - something like this
if (Serial.available() > 0) {
lastTimeDataReceived = millis();
// other code to deal with the received data
}
if (millis() - lastTimeDataReceived >= maxInterval) {
// nothing has arrived for some time
}