     Serial.println(local_results[6]);
     
     processRC(local_results);
+    delay(200);
 }
 
 void startCapturingRCState()
 {
-  synced = false;
-  rc_index = 1;
-  rc_state = RC_PROCESSING;
-}
-
+  for (int i = 1; i < 7; i++) {
+    RC_Done[i] = 0;
+  }
+}
+
+// processRC modified by TCF  9/17/15
 void processRC (unsigned long *RC_results)
 {   
     // 1st pulse is aileron (position 5 on receiver; controlled by Right left/right joystick on transmitter)
     //     used for Steering
-    int aileron = RC_results[1];
-    RC_results[1] = liveTurn(aileron)? convertTurn(aileron): STRAIGHT_TURN_OUT;
+    int aileron = RC_results[RC_TURN];
+//    RC_results[RC_TURN] = liveTurn(aileron)? convertTurn(aileron): STRAIGHT_TURN_OUT;
+    RC_results[RC_TURN] = convertTurn(aileron);
     
     /* 2nd pulse is aux (position 1 on receiver; controlled by flap/gyro toggle on transmitter) 
        will be used for selecting remote control or autonomous control. */
-    RC_results[2] = (RC_results[2] > MIDDLE? HIGH: LOW);
+    if (NUMBER_CHANNELS > 5) 
+        RC_results[RC_AUTO] = (RC_results[RC_AUTO] > MIDDLE? HIGH: LOW);
 
     /* 4th pulse is gear (position 2 on receiver; controlled by gear/mode toggle on transmitter) 
     will be used for emergency stop. D38 */
-    RC_results[4] = (RC_results[4] > MIDDLE? HIGH: LOW);
-    
-    if (RC_results[4] == HIGH)
+    RC_results[RC_ESTP] = (RC_results[RC_ESTP] > MIDDLE? HIGH: LOW);
+    
+    if (RC_results[RC_ESTP] == HIGH)
     {
         E_Stop();
-        if (RC_results[2] == HIGH) // under RC control
-            steer(RC_results[1]); 
+        if (RC_results[RC_AUTO] == HIGH  && NUMBER_CHANNELS > 5) // under RC control
+            steer(RC_results[RC_TURN]); 
         return;
     }
-    if ( RC_results[2] == LOW)
+    if ( RC_results[RC_AUTO] == LOW  && NUMBER_CHANNELS > 5)
         return;  // not under RC control
 
     /*  6th pulse is marked throttle (position 6 on receiver; controlled by Left up/down joystick on transmitter). 
     It will be used for shifting from Drive to Reverse . D40
     */
-    RC_results[6] = (RC_results[6] > MIDDLE? HIGH: LOW);
+    RC_results[RC_RVS] = (RC_results[RC_RVS] > MIDDLE? HIGH: LOW);
         
-// TO DO: Select Forward / reverse based on RC_results[6]
+// TO DO: Select Forward / reverse based on RC_results[RC_RVS]
 
        
     /*   3rd pulse is elevator (position 4 on receiver; controlled by Right up/down.  
        will be used for throttle/brake: RC_Throttle
     */
     // Braking or Throttle
-    if (liveBrake(RC_results[3])) 
-        brake(convertTurn(RC_results[3]));
+    if (liveBrake(RC_results[RC_GO])) 
+        brake(convertBrake(RC_results[RC_GO]));
     else
         brake(MIN_BRAKE_OUT);
     //Accelerating
-    if(liveThrottle(RC_results[3]))
-        moveVehicle(RC_results[3]);
+    if(liveThrottle(RC_results[RC_GO]))
+        moveVehicle(RC_results[RC_GO]);
     else
         moveVehicle(MIN_ACC_OUT);
         
-    steer(RC_results[1]); 
+    steer(RC_results[RC_TURN]); 
     
     /* 5th pulse is rudder (position 3 on receiver; controlled by Left left/right joystick on transmitter) 
     Not used */
-    RC_results[5] = (RC_results[5] > MIDDLE? HIGH: LOW);  // could be analog
-
-}
-//Converts RC values to corresponding values for the DAC
+    RC_results[RC_RDR] = (RC_results[RC_RDR] > MIDDLE? HIGH: LOW);  // could be analog
+
+}
+//Converts RC values to corresponding values for the PWM output
 int convertTurn(int input)
 {
-      //full turn right = 250, full turn left = 126
-      int dacRange = RIGHT_TURN_OUT - LEFT_TURN_OUT;
-      int rcRange = MAX_RC - MIN_RC;
-      input-= MIN_RC;
-      // TO DO: No doubles
-      double output = (double)input /(double)rcRange;
-      //swap left and right
-      output = 1 - output;
-      output *= dacRange;
-      output += LEFT_TURN_OUT;
-      //set max and min values if out of range
-      int trueOut = (int)output;
-      if(trueOut > RIGHT_TURN_OUT)
-          trueOut = RIGHT_TURN_OUT;
-      else if(trueOut < LEFT_TURN_OUT)
-          trueOut = LEFT_TURN_OUT;
-      return trueOut;
+     long int steerRange, rcRange;
+     long output;
+     int trueOut;
+//  Check if Input is in steer dead zone
+      if (input <= MIDDLE + DEAD_ZONE && input >= MIDDLE - DEAD_ZONE)
+       return STRAIGHT_TURN_OUT;
+       // MIN_RC = 1 msec = right; MAX_RC = 2 msec = left
+      // LEFT_TURN_OUT > RIGHT_TURN_OUT
+      if (input > MIDDLE + DEAD_ZONE)
+      {  // left turn
+         steerRange = LEFT_TURN_OUT - STRAIGHT_TURN_OUT;
+         rcRange = MAX_RC - (MIDDLE + DEAD_ZONE);
+        input-= MIDDLE + DEAD_ZONE;
+        output = STRAIGHT_TURN_OUT + input * steerRange / rcRange;
+        //set max and min values if out of range
+        trueOut = (int)output;
+        if(trueOut > LEFT_TURN_OUT)
+            trueOut = LEFT_TURN_OUT;
+        if(trueOut < STRAIGHT_TURN_OUT)
+            trueOut = STRAIGHT_TURN_OUT;
+        return trueOut;
+    }
+      if (input < MIDDLE - DEAD_ZONE)
+      {  // right turn
+         steerRange = STRAIGHT_TURN_OUT - RIGHT_TURN_OUT;
+         rcRange = MIDDLE - DEAD_ZONE - MIN_RC;
+        input = input - DEAD_ZONE - MIDDLE;  // input is negative
+        output = STRAIGHT_TURN_OUT + input * steerRange / rcRange;
+        //set max and min values if out of range
+        trueOut = (int)output;
+        if(trueOut < RIGHT_TURN_OUT)
+            trueOut = RIGHT_TURN_OUT;
+        if(trueOut > STRAIGHT_TURN_OUT)
+            trueOut = STRAIGHT_TURN_OUT;
+        return trueOut;
+    }
 }
 int convertBrake(int input)
 {

volatile int rc_state = RC_WAIT;
volatile boolean synced = false;
volatile unsigned long last_fallingedge_time = 4294967295; // max long

void setup()
{ //Set up pins

          digitalWrite(SelectCD,HIGH);
      }
}
void moveVehicle(int acc)
{
      /* Observed behavior on ElCano #1 E-bike no load (May 10, 2013, TCF)
      0.831 V at rest 52 counts
      1.20 V: nothing 75
      1.27 V: just starting 79
      1.40 V: slow, steady 87
      1.50 V: brisker 94
      3.63 V: max 227 counts
      255 counts = 4.08 V
      */
      DAC_Write(DAC_CHANNEL, acc);
}
