RC Controller

Overview

The code below is a very simple control scheme using a RC transmitter and a solenoid valve. If you are interested in single-stick driving, read through Charles Guan's quick tutorial below.

See also: servo control.

Parts

-Arduino microcontroller and carrier board

-LiPo battery

-Servo

-Radio controller with receiver and transmitter

Prepare the Breadboard

Program the Microcontroller

Extended Range

/**
 * @file: RC control of servo
 *
 * @description 
 * expand range of non-cont. rot. servo
 */
#define RCpin 9
unsigned long duration;
void setup()
{
    pinMode(RCpin, INPUT);
}
void loop()
{
    duration = pulseIn(RCpin, HIGH);
  int expanded=((duration-1500)*2)+1500;
}

Solenoid

/**
 * @file: RC control of solenoid
 *
 * @description 
 * turn solenoid on/off with RC controller
 */
#define SOLENOID 5
#define RC 2

const int threshold = 1700;

//--- Function: Setup ()
void setup()
{              
    // initialize the digital pin as an output.
    pinMode(SOLENOID, OUTPUT);
    pinMode(RC, INPUT);
    Serial.begin(9600);
}
//--- Function: Loop ()
void loop()
{
    unsigned long duration;

duration=pulseIn(RC, HIGH, 25000); // read values (pin, value, timeout)

Serial.println(duration); // output values to serial monitor

   
    if(duration>threshold)
    {
        digitalWrite(SOLENOID, HIGH);   // turn the solenoid on
    }      
    else
    {
        digitalWrite(SOLENOID, LOW);     // turn the solenoid off
    }
}

Single-stick driving (by Charles Guan):

{1000-2000} refers to the microseconds count you would use with delayMicroseconds() on the left and right sides only if you are straight toggling the servo pin yourself. If you are using the Servo.h library, these would not apply. Note that the { } is not proper notation to use in Arduino code, just a convenient way of representing the two values.

"clamped" = "limited to"

"corners" = The corners of the joystick travel square

Generally, full left and full right should result in spinning the vehicle (full opposite directions). Corners result in only one side moving at full speed in either direction. Full forward and backward result in both sides moving. The way it's usually done is that up-down (ch2) adds an offset to both sides. That's how up-down moves both sides. In other words, if you have center = {1500,1500}, full forward adds offset {500,500} for {2000,2000}.

Left right only adds a *differential* to both sides, but of the same magnitude as the up-down. So full left (no up-down deflection) adds {-500,500} resulting in {1000,2000}, which is usually left side backward, right side forward. Any magnitude exceeding the limits is clamped to the limit.

So you see how corners result in one side moving only. Full forward adds {500,500} to {1500,1500} to get {2000,2000}. Adding left stick {-500,500} to full foward gives {1500,2500}, but the 2500 is clamped to 2000, resulting in {1500,2000} i.e. left side stationary, right side forward.

The clamping part is what confuses most people. It is the most common mixing algorithm, and is in fact topologically not a circle or square, but kind of a 3 dimensional inverted square pyramid. And so on.