Aircraft flap simulation problem

Hey,

I'm a student in Aircraft Maintenance. For school we must simulate an aicraft flap.

We want to control the servo positions/anlges with an arduino.
As switch to control the analog inputs we use a rotary cam switch. The inputs voltage is devided by 9V battery regulated by a LM2596 buck converter.

The case:
If the input voltage on A0 is above 2,5V (511) the servo must stay at angle of 0 degrees
If the input voltage on A1 is above 2,5V (511) the servo must go to an angle of 20 degrees
If the input voltage on A2 is above 2,5V (511) the servo must go to an angle of 40 degrees
If the input voltage on A3 is above 2,5V (511) the servo must go to an angle of 60 degrees.
If the input voltage on any of the analog pins is underneath 2,5V to servo must go back to an angle of 0 degrees.

I've tried several codes, but non of them are working with multiple angles. I found one on this forum which is working with one angle.

This is de code:

#include "Servo.h" // include the servo library

Servo servo1; // creates an instance of the servo object to control a servo
int posServo1 = 0;
int analogPin = A0; // the analog pin that the sensor is on
int analogValue = 0; // the value returned from the analog sensor
int servoPin = 9; // Control pin for servo motor

void setup()
{
servo1.attach(servoPin); // attaches the servo on pin 4 to the servo object
Serial.begin(9600);
servo1.write(45);
delay(2000);
}

void loop()
{
analogValue = analogRead(analogPin); // read the analog input (value between 0 and 1023)
Serial.println(analogValue);
if(analogValue==0)
{
posServo1=0;
}
else if(analogValue>=511)
{
posServo1+=1;
}
else
{
posServo1-=1;
}
posServo1=constrain(posServo1,0,180);
servo1.write(posServo1); // write the new mapped analog value to set the position of the servo
delay(50); // waits for the servo to get there

Could someone explain me how to program multiple angles which I could control with the analog pins on the arduino.

Thankyou.

How is the switch wired? I ask because if the switch connected to A0 is closed and A1 - A3 are open, those inputs can be floating and a floating input can read anything. So the open switches (or sections of the switches need to be tied to ground (or a known voltage).

It is a rotary switch, so wired like this:

1 is Input voltage (VCC)
2, 3 and 4 are output

pos 0 interrupts circuit
pos 1 = 2,5V over 1 to section 2
pos 2 = 2,5V over 1 to section 3
pos 3 = 2,5V over 1 to section 4

section 2 is wired to A0 on arduino
section 3 is wired to A1 on arduino
section 4 is wired to A2 on arduino.

When the switch is in pos 1 there is nog analog input on the arduino.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

Hi,

When the switch is in pos 1 there is nog analog input on the arduino.

As groundfungus has said, you cannot leave an active input pin open circuit, the input impedance is so high that it floats and acquires any value between 0 and 5v, usually fluctuating.

If you disconnect it from you 2,5v and higher source you must ground the analog input for it to read 0V.

Tom... :slight_smile:
PS, why are you using analog inputs if you are only expecting digital response, ie ON > 2.5V or OFF < 2.5V?

I am a bit thick this morning and having trouble understanding the description. Can you show, on a schematic, how the switch is wired? If you can't do it in CAD just draw by hand and post an image.

We need to get a unambiguous output from the switch before coding to select flap position.

Here is a wiring diagram of our project. I know I could get problems with the power supply. I'm not a electrical technician so any improvements are welcome.

Hi,

The inputs voltage is devided by 9V battery regulated by a LM2596 buck converter.

What sort of 9V battery, are you aware of the current that a servo draws when it moves or needs to hold position against a torque?

Tom... :slight_smile:

Yup, thats why I said the power supply could give some trouble. But we do also have some Lithium Polymer batteries.

Hi, OPs circuit.


Do you have a position 0 for the switch, so 0,1,2,3.
What voltage are they switching, if 9V, you can use a voltage divider to rescale it to 5V for your three inputs and use digital mode.

Tom... :slight_smile:
PS I'm off to bed.. will check in the morning.. :sleeping:

Hi,
Do not draw power from the 5V on the UNO for the servo, the UNO is not built for the sort of currents that the servo uses.

Tom... :sleeping:

Allright, so I have to power the servo external?
Position 0 (common) is GND

Ok, I see what you are doing. The question, now, is what is the state of the LM2596 output when it not powered. It may be floating and also when the regulator is turned off, if there is nowhere for the stored power to go the output will stay high (analog inputs are very high resistance > 10M). The solution, i think, is pull down resistors on the regulator outputs (and therefore analog inputs) to ground to bleed the power and pull the analog inputs low. I suggest 10K (or higher if they don't affect operation).

If the voltage from the switch is known, those regulators could be replaced with resistor voltage dividers.

The output from the switch is either 0V or one (and only one) of 3 contacts (to A0, A1, A2 through the regulators) is closed. Is that right?

If so, analog inputs are not really needed. The analog input pins default to digital input on reset so you don't have to use pinMode to set them (doesn't hurt though).

So the code could be like (pseudo code):

digital read A0, A1, A2
if(A0 == HIGH) set servo to 20
else if(A1 == HIGH) servo to 40
else if(A2 == HIGH) servo to 60
else servo to 0

The LM2596 is mounted on a module. (DC Buck Converter) because the the minus is soldered to GND I guess it is not floating when the power is turned off.

I already tried digital input but for some reason it does not work. That is the reason why I asked if anybody has experience with analog inputs.

Hi,
Did you make your potential dividers like this?

Tom... :slight_smile:

Hi,
Then you code.

If input 1 and 2 and 3 ==0
servo position o
If input 1 == 1
servo position 1
If input 2 == 1
servo position 2
If input 3 == 1
servo position 3

Tom... :slight_smile:

JoopvdW:
The LM2596 is mounted on a module. (DC Buck Converter) because the the minus is soldered to GND I guess it is not floating when the power is turned off.

Plus may be floating regardless of the state of minus. There may be nothing inside the LM2596 that draws it down to ground. Have you verified the voltages go to zero when that converter is NOT selected?

I know that there could be some trouble with the potential deviders.

But could someone explain how to write a code that the arduino continuously read the analog inputs and write a signal to the servo?

My code does not seems to work..

Thanks

People use voltage dividers all the time to reduce higher voltages to voltages that the Arduino can handle. Voltage dividers are far and away the most popular method. If you show what you tried we can help you make it work. Same with your code. We are here to help you learn, not write code for you.