 
 YaesuCAT FT817(txSerial);
 
+/** Request TX frequency and mode settings from time to time. */
+static void txRequestFrequencyAndMode(YaesuCAT& ft817,unsigned long interval=200) {
+  
+  static unsigned long r_time = millis();
+  
+  if ( (millis() - r_time) > interval ) {
+
+    ft817.requestFrequencyAndMode();
+    
+    r_time = millis();
+  }
+}
+
+/** Set the desired frequency and mode of the TX (one quantity at a time). */
+static void txSetFrequencyAndMode(YaesuCAT& ft817, 
+                                  uint32_t& desired_frequency,
+                                  byte& desired_mode,
+                                  unsigned long interval=500) {
+  
+  static unsigned long s_time = millis();
+
+  uint32_t frequency = ft817.getFrequency();
+  byte mode = ft817.getMode();
+  
+  // send message to the rig
+  if ( millis() - s_time > interval ) {
+    
+    s_time = millis();
+
+    if ( frequency != desired_frequency )
+      ft817.writeFrequency(desired_frequency);
+    else if ( mode != desired_mode )
+      ft817.writeMode(desired_mode);
+  }
+}
+
+// the program's main loop
 void loop() {
 
   uint32_t frequency = FT817.getFrequency();
   byte mode = FT817.getMode();
-  
+
+  static byte txState = 0;
+
   static unsigned long loop_init = millis();
-
-#if 1
-  // request frequency and mode settings...
-  static unsigned long r_time = millis();
-  
-  if ( (frequency == YaesuCAT::ILLEGAL_FREQ || mode == YaesuCAT::ILLEGAL_MODE)
-       && (millis() - r_time > 200 ) ) {
-
-    FT817.requestFrequencyAndMode();
-    
-    r_time = millis();
-  }
-#endif
+  static unsigned long tx_init = millis();
 
   uint32_t desired_frequency = 28175000; // 70.175 MHz transverted into 10m Band
   byte desired_mode = YaesuCAT::eModeUSB;
 
+  // set the rxState appropriately
+  if ( frequency == YaesuCAT::ILLEGAL_FREQ || mode == YaesuCAT::ILLEGAL_MODE )
+    txState = 0;
+  else if ( frequency != desired_frequency || mode != desired_mode )
+    txState = 1;
+  else
+    txState = 2;
+
+  switch ( txState ) {
+
+    case 0:
+      txRequestFrequencyAndMode(FT817, 200);
+      break;
+
+    case 1:
+      txSetFrequencyAndMode(FT817, desired_frequency, desired_mode, 500);
+      txRequestFrequencyAndMode(FT817, 200);
+      break;
+
+    case 2:
+      if ( (millis() - tx_init) > 5000 ) {
+        FT817.requestFrequencyAndMode();
+        tx_init = millis();
+      }
+      break;
+
+    default:
+      ;
+  }
+
+  if ( txState == 2 )
+    digitalWrite(TX_LOCKED_LED, HIGH);
+  else
+    digitalWrite(TX_LOCKED_LED, LOW);
+
 #if defined (__AVR__)
   txSerial.listen();
 #endif // (__AVR__)

#endif
}
#endif // (__AVR__)



#define DEBUG

#include <CATutil.h>
#include <YaesuCAT.h>

/** LED which indicates that TX loop is 'locked'. */
#define TX_LOCKED_LED     13

#ifdef DEBUG
 // http://arduiniana.org/libraries/streaming/
