"Arming" algorithm for RC Landing Gear Controller?

Hi all;

This is my first post here (and also my first Arduino project!). I’m hoping someone here can help me, as I’m not really sure how to proceed.

I’ve programmed a simple landing gear sequencer / controller that reads the PWM from a RC reciever, and then uses IF / Else If to drive a Switch Case that writes the servos and gear doors into their up or down positions, with appropriate delays so everything doesn’t hit everything else! I’m using the VarSpeedServo library, which is magic for slowing down the servos to the speed I need.

So far, it’s working very well, as seen here: Youtube - Arduino Gear Sequencer

The challenge (for me, I expect it’s nothing for you Code Gods…) is this. I want to write an “Arming” section that prevents the gear from moving on power up unless the landing gear switch on the Transmitter is in the same position as the Landing gear is. Most of the time, this would obviously be down, but the flexibility to have it either way would be useful.

I know it’s NOT the right way to do it, but something like an IF statement that sits at the end of void setup() and prevents void loop() from running unless the gear position and switch position are in agreement… Does that make sense?

Here is my code (try not to cringe to much!):

  Beginnings of the Gear Sequencer / Controller Code

#include <VarSpeedServo.h>

int Rx;  // This is input from the selected Gear Channel on the Reciever.
int GearStatus;  // Variable to keep GearStatus seperate from switch position
const int UpLight = 13 ; //Red Light to indicate gear up, will flash if in between positions
const int DownLight = 12 ; //Green LED indicating gear is down
VarSpeedServo FLDoor; //Front Left Gear Door Servo
VarSpeedServo FRDoor; //Front Right Gear Door Servo
VarSpeedServo RLDoor; //Rear Left Gear Door Servo
VarSpeedServo RRDoor; //Rear Right Gear Door Servo
VarSpeedServo NoseGear; //Nose Gear Servo
VarSpeedServo MainGear; //Main Gear Servo
int CurrentSwitchState = 0;  //Used in Case statements to prevent them from running more than once
int PreviousSwitchState = 0;

void setup()
  //Serial.begin(9600); // Serial Monitor init
  pinMode(2, INPUT); // Rx is input on Pin 2
  pinMode (UpLight, OUTPUT);
  pinMode (DownLight, OUTPUT);
  FLDoor.attach(11);  //FLDoor Servo attached to Pin 11
  FLDoor.write (70);  //All write statements in this section are to initialize the gear & doors in the Down position.
  FRDoor.attach(10); // FRDoor Servo attached to Pin 10
  FRDoor.write (116);
  RLDoor.attach(9); // RLDoor Servo attached to Pin 9
  RLDoor.write (148);
  RRDoor.attach(8); //RRDoor Servo attached to Pin 8
  NoseGear.attach(3); //NoseGear Servo attached to Pin 3
  MainGear.attach(4); //MainGear Servo attached to Pin 4

void loop()
  Rx = pulseIn(2, HIGH, 20000); // Read the pulse width of the channel
  if (Rx > 1900)
    GearStatus = 'U'; // Landing Gear to Up Position
  else if (Rx < 1250 && Rx > 1000)
    GearStatus = 'D'; // Landing Gear to Down Position
  else if (Rx > 1251 && Rx < 1899)
    GearStatus = 'T'; // Landing gear in transition
  else if (Rx < 1000) //Rx failsafe on signal loss will set 925uS pulse.
    GearStatus = 'E'; // Signal loss from transmitter based on Rx failsafe, Emerg Gear Down.

  switch (GearStatus)
    case 'D':                          // Sequence to bring gear and doors DOWN
      CurrentSwitchState = GearStatus;
      if (PreviousSwitchState != CurrentSwitchState)
        RLDoor.write(80, 38);           // Rear Doors open
        RRDoor.write(110, 38);          //Rear Doors open
        NoseGear.write(43, 60);         //Nose Gear Down
        delay (200);                    //allows rear doors to clear
        FLDoor.write(70, 15);           //Main Doors open
        FRDoor.write(116, 15);          //Main Doors open
        delay (200);                    //letting Main Doors clear gear legs.
        MainGear.write(120, 15);        //Main landing gear Down
        delay (1600);                   //travel time for Main Gear at 15 speed
        RLDoor.write(148, 30);          //Rear Doors close
        RRDoor.write(46, 28);           //Rear Doors close
        digitalWrite(UpLight, LOW);
        digitalWrite (DownLight, HIGH);  //Green LED on indicating gear down.
      PreviousSwitchState = CurrentSwitchState;

    case 'U':                          // Sequence to bring gear and doors UP
      CurrentSwitchState = GearStatus;
      if (PreviousSwitchState != CurrentSwitchState)
        RLDoor.write(80, 30);           // Rear Doors Open
        RRDoor.write(110, 28);         // Rear Doors Open
        delay (500);
        MainGear.write(34, 13);
        NoseGear.write(125, 60);      //Nose Gear Up
        delay (850);
        FLDoor.write(14, 12);         //Main Doors Close
        FRDoor.write(175, 12);        //Main Doors Close
        delay (2000);
        RLDoor.write(148, 30);        //Rear Doors Close
        RRDoor.write(46, 28);         //Rear Doors Close
        digitalWrite(UpLight, HIGH);
        digitalWrite (DownLight, LOW);
      PreviousSwitchState = CurrentSwitchState;

    case 'T':
      // turn all the LEDs off:
      digitalWrite(DownLight, LOW);   //In between state.  Can probably be removed
      digitalWrite(UpLight, HIGH);    //from final, as it only flashes the Red
      delay (200);                    //Up light.
      digitalWrite(UpLight, LOW);
      delay (200);

    case 'E':
      Serial.println ("Emerg!!!");
      Serial.println (Rx);
      Serial.println ("E");
      //Eventually will contain code to drive the gear down
      //if the Rx goes into failsafe.
  delay(500);// Slow down the program during testing, comment out when in aircraft, slows stuff down too much.

The capabilities of this little microcontroller are incredible… Just wish my knowledge of programming was better (some punch cards when I was a kid… Pascal when I was a teen… Maybe some Basic around then, too…) I can usually understand the logic of the “decision tree”… usually…

Any help and knowledge you can provide is most gratefully appreciated.