Simple timed serial event

Hi all :slight_smile:

I'm trying to come up with a snipet that looks for an incoming string on the serial port. If the code does not show up every second, the do something. It would be a failsafe for a serial port controlled R/C car. The controller would send out "something" every second to verify that there is radio connection.(Xbee) Arduino on the other side would look for this signal and if it didn't show for a sec, center the servo (throttle).

thanx

Something like this.

loop () {

   static long LastDataRx;

  if (Serial.available() > 0) {
      Serial.flush(); // assuming you don't care what the data is, just that there was some
      LastDataRx = millis();
   }

   if (millis() - LastDataRx >= 1000) {
      doSomething();
   }

}

Rather than flush the data it would be better to at least test for a valid character if you have control of the transmitted data and/or know what to expect.


Rob

thanx I'll try this.
I know exactly what the string is as it would be sent from the transmitter and it would be just the text "failsafe" coming every 500ms. If it doesn't come, just stop. (Center servos)