gear door sequencer

Hi,

For one of my RC airplanes I need a gear/door sequencer to control the doors and gears. I already managed to get the code compiled as I would like it to operate, not tested in the airplane yet. Now I need some extra features that I would work into it.
Every time the Arduino boots up there are some servo’s that move, I would like to adjust my code so that everything stays in place as the last time I switched of the plane. So that the gear and doors stays in place and not move during the startup of the board. Is this possible to write something into the code?

Also I want that when the switch that controls the gears has moved the Arduino don’t do anything unless the switch has been moved into the last position when the Arduino was shut down.

Here is the code I wrote so far:

#include <VarSpeedServo.h>

VarSpeedServo ServoWielen;
VarSpeedServo ServoDeur_1;
VarSpeedServo ServoDeur_2;
int ServoIn;
int ServoUp;
int ServoDown;

const int PinServoInput = 8; //aansluiting RX kanaal
const int PinServoWielen = 10; //aansluiting wielen
const int PinServoDeur_1 = 12; //aansluiting deur servo hoofdpoten
const int PinServoDeur_2 = 11; // aansluiting deur servo neuspoot

void setup(){
pinMode(PinServoInput, INPUT);
// Serial.begin(9600);
ServoWielen.attach(PinServoWielen);
ServoDeur_1.attach(PinServoDeur_1);
ServoDeur_2.attach(PinServoDeur_2);
ServoWielen.writeMicroseconds(1000);
ServoDeur_1.slowmove(2000,150);
ServoDeur_2.slowmove(2000,150);
ServoUp = 1600; //wielen omhoog
ServoDown = 1400; //wielen omlaag
delay(50);
}
void loop(){
ServoIn = pulseIn(PinServoInput, HIGH, 21000);

if (ServoIn >= 1600) { // als servo input van RX hoger is of gelijk aan ga naar GearUp
goto WielenOmhoog;
}
else if (ServoIn <= 1400) { // als de servo input van de RX lager is of gelijk aan ga naar GearDown
goto WielenUit;
}
goto EndLoop;

WielenOmhoog:
{

ServoDeur_1.slowmove(1000,50); //wieldeur hoofdpoten openen
if (ServoIn >ServoUp){
delay (2000);
}
ServoWielen.writeMicroseconds(2000); //wielen omhoog
if (ServoIn >ServoUp){
delay (2000);
}
ServoDeur_1.slowmove(2000,50); //wieldeur hoofdpoten dicht
ServoDeur_2.slowmove(1000,50); //wieldeur neuspoot dicht
if (ServoIn <ServoDown){

delay (20);
}

goto EndLoop;
}

WielenUit:
{
ServoDeur_1.slowmove(1000,50); //wieldeur hoofdpoten open
ServoDeur_2.slowmove(2000,50); //wieldeur neuspoot open
if (ServoIn <ServoDown){

delay (2000);
}

ServoWielen.writeMicroseconds(1000); //wielen omlaag

if (ServoIn <ServoDown){

delay (2000);
}
ServoDeur_1.slowmove(2000,50); //wieldeur hoofdpoten dicht
if (ServoIn <ServoDown){

delay (20);
}
goto EndLoop;
}
EndLoop:
ServoUp = ServoIn + 100; // adds 100 to the current servo in value to help detect if it has changed
ServoDown = ServoIn -100;

}

Kind regards,

Ben

Every time the Arduino boots up there are some servo’s that move, I would like to adjust my code so that everything stays in place as the last time I switched of the plane. So that the gear and doors stays in place and not move during the startup of the board. Is this possible to write something into the code?

Maybe.

The reason that the servos move is because you haven't told them where to be before you attach them. So, they start trying to get to the correct place. Then, you tell them where the correct place is.

If you store the servo positions in EEPROM as you switch the plane off, then you could read the positions in setup(), write the positions to the servos, and then attach them.

The challenge is going to be to record where the servos are when you switch off the plane. Switching off the power is going to stop the Arduino before it has time to do that, unless you add some extra circuitry. A large capacitor that can power the Arduino for a while would be one way. Detect when the capacitor charging circuit looses power, and do the save-the-servo-positions-in-EEPROM bit, before the capacitor runs out.

Perhaps the servos will always be in the same position when you power down the plane - for example flight controls neutral, gear down and doors open. If so you can build this into your program without needing to save the positions at shut-down.

...R

@ Robin2,

How should this be written into the code?

Ben

How should this be written into the code?

First, you should answer the question.

Then, you need to determine what should trigger the action. Then, you need to detect what that trigger occurs. When it does, it really doesn't seem all that difficult to command a servo or two to go to some position.

Of course, you need to also command the servos to go to those positions before you attach them next time the Arduino powers up.

First you will need to experiment with your servos to discover what settings correspond with the positions you want.

Then before you attach() the servos you would use servo.write to preset the servos at those positions. And when you attach() the servos they should stay where they are.

...R

benovitch:
@ Robin2,

How should this be written into the code?

Ben