Code Example

/*
  Testing RC Connection
  3/25/14
  Captures the Pulse signal from a RC controller
  Feel free to do whatever you want with this code example
 */
 #include <Servo.h>

// Create Variables to hold the Receiver signals 
int Ch1,Ch2,Ch3,Ch4,Ch5,Ch6; 
int CalcHold;        //Variable to temp hold calculations for steering stick corections

// Create Servo Objects as defined in the Servo.h files
Servo R_DCMotor;  // Servo DC Motor Driver (Designed for RC cars)
Servo L_DCMotor;  // Servo DC Motor Driver (Designed for RC cars)


void setup() {
// Set the pins that the transmitter will be connected to all to input 
  pinMode(13, OUTPUT);
  pinMode(12, INPUT); //I connected this to Chan1 of the Receiver
  pinMode(11, INPUT); //I connected this to Chan2 of the Receiver
  pinMode(10, INPUT); //I connected this to Chan3 of the Receiver
  pinMode(9, INPUT); //I connected this to Chan4 of the Receiver
  pinMode(8, INPUT); //I connected this to Chan5 of the Receiver
  pinMode(6, INPUT); //I connected this to Chan6 of the Receiver
// Attach Speed controller that acts like a servo to the board  
  R_DCMotor.attach(1); //Pin 1
  L_DCMotor.attach(0); //Pin 0

  Serial.begin(9600); 
}
void DriveServosAuto()
{
 //lots of code here
  digitalWrite(13, HIGH);    // sets the LED off
  delay(100);
  digitalWrite(13, LOW);    // sets the LED off
  delay(100);
 
}


void DriveServosRC()
{
  if (Ch1 < 1550 && Ch1 > 1450)
    R_DCMotor.writeMicroseconds(Ch2);  // sets the servo position
    L_DCMotor.writeMicroseconds(Ch2);  // Set so that stearing would be horz stick

  if (Ch1 >1550)
    L_DCMotor.writeMicroseconds(Ch2);  // Set so that stearing would be horz stick
    CalcHold = 1450-Ch1;
    R_DCMotor.writeMicroseconds(Ch2-CalcHold);  // sets the servo position

  if (Ch1 <1450)
    L_DCMotor.writeMicroseconds(Ch2);  // Set so that stearing would be horz stick
    CalcHold = Ch1-1550;
    R_DCMotor.writeMicroseconds(Ch2-CalcHold);  // sets the servo position
}

void PrintRC()
{ // print out the values you read in:
 Serial.println(" RC Control Mode "); 
 Serial.print("Value Ch1 = "); 
 Serial.println(Ch1);
 Serial.print("Value Ch2 = "); 
 Serial.println(Ch2);
 Serial.print("Value Ch3 = "); 
 Serial.println(Ch3); 
 Serial.print("Value Ch4 = "); 
 Serial.println(Ch4);
 Serial.print("Control = "); 
 Serial.println(Ch5);
 Serial.print("Value Ch6 = "); 
 Serial.println(Ch6);
 Serial.println(" ");
 delay(1000);
}

void loop() 
{
  Ch5 = pulseIn(8, HIGH, 21000); 
  
  if(Ch5 <= 1600) //Going to be in Autonomous Mode
    {
//      L_PhotoValue = analogRead(L_PhotoPin); //Read the value of the sharp sensor
//      C_SharpValue = analogRead(C_SharpPin); //Read the value of the sharp sensor
//      R_PhotoValue = analogRead(R_PhotoPin); //Read the value of the sharp sensor
      DriveServosAuto();  //Drive Motors under Autonomous control   
//      PrintAuto(); //Print Values for Auto Mode Diagnostics
    }
  if(Ch5 > 1600) //Going to be in RC Control Mode
    {
      Ch1 = pulseIn(12, HIGH, 21000); 
      Ch2 = pulseIn(11, HIGH, 21000); 
      Ch3 = pulseIn(10, HIGH, 21000);
      Ch4 = pulseIn(9, HIGH, 21000);
//      Ch5 = pulseIn(8, HIGH, 21000);
      Ch6 = pulseIn(7, HIGH, 21000);       
      DriveServosRC(); // Drive Motors under RC control
    }
//    PrintRC(); //Print Values for RC Mode Diagnostics
}