I have an Arduino (Pro Mini) connected to an HMI display over serial. When the user pushes a button on the HMI screen, it activates a relay attached to one of the digital pins on the Arduino.
For safety reasons, if the Arduino loses communications with the HMI, I want it to turn off the relay. So, when the button has been pushed, it triggers a polling sequence to verify that the button is still being pushed (I'm not relying on the button release event, again for safety).
The following code works perfectly if I disconnect either of the signal lines (tx or rx) or ground from the HMI. If I disconnect the +5 volts, however, the Arduino freezes completely, and therefore never shuts down the relay.
What could be causing this, and what can I do to overcome the problem?
The code is as follows, however, it works as expected if the data lines or ground are severed.
int getNumber(const char *reqString)
{
sendString(reqString);
long startTime = currentMillis;
long waitTimer = currentMillis - startTime;
bool responded = false;
int endcount = 0;
byte inbyte;
byte bytebuffer[80];
int bytecount = 0;
while (waitTimer < 100 && !responded)
{
if (HMI.available())
{
inbyte = HMI.read();
if (inbyte == 0xFF)
{
endcount++;
if (endcount == 3)
{
responded = true;
endcount = 0;
}
}
else
{
endcount = 0;
}
bytebuffer[bytecount] = inbyte;
bytecount++;
}
// Update the wait Timer.
waitTimer = millis() - startTime;
}
// If we received a response, return the first data byte. If not, return 0
if (responded)
{
Serial.println("Received response");
return bytebuffer[1];
}
else
{
Serial.println("No response");
return 0;
}
}
