Pages: [1]   Go Down
Author Topic: Simple timed serial event  (Read 505 times)
0 Members and 1 Guest are viewing this topic.
GTA, Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 10
The power is in the code.....
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all smiley

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
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 121
Posts: 8458
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Something like this.

Code:
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
« Last Edit: May 19, 2011, 11:26:44 pm by Graynomad » Logged

Rob Gray aka the GRAYnomad www.robgray.com

GTA, Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 10
The power is in the code.....
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)
Logged

Pages: [1]   Go Up
Jump to: