Aircraft flap simulation problem

Because of the effort we needed to put in a voltage divider (soldering etc.), we've chosen for the DC to DC buck converter. Those Buck converters are even more efficient than a linear voltage regulator, like the LM7805 we also al lot of people use in their circuits. But I have to admit that a voltage divider is also a good option.

Furthermore I wrote several codes to make the arduino control the servo.

This is the first one:

#include <Servo.h>
Servo myservo;          // Creates an instance of the servo object to control a servo
int pos0myservo = 0;   // Defines position 0
int pos1myservo = 0;  // Defines position 1
int pos2myservo = 0; // Defines position 2
int pos3myservo = 0;    // Defines position 3
int analogPin1 = A0;    // The analog pin that the cam switch is on 
int analogPin2 = A1;    // The analog pin that the cam switch is on 
int analogPin3 = A2;    // The analog pin that the cam switch is on 
int analogPin4 = A3;    // The analog pin that the cam switch is on 
int analogValue0 = 0;   // The value returned from the analog sensor
int analogValue1 = 0;   // The value returned from the analog sensor
int analogValue2 = 0;   // The value returned from the analog sensor
int analogValue3 = 0;   // The value returned from the analog sensor
int servoPin = 9;       // Control pin for servo motor


void setup() 
{
myservo.attach(servoPin);  // Attaches the servo on pin 9 to the servo object
myservo.write(179);
Serial.begin(9600);        // Opens serial port, sets data rate to 9600 bps
delay(2000);               // 2000ms For servo to get there


}

void loop ()
{
analogValue0 = analogRead(analogPin1);     // read the analog input (value between 0 and 1023)
analogValue1 = analogRead(analogPin2);     // read the analog input (value between 0 and 1023)
analogValue2 = analogRead(analogPin3);     // read the analog input (value between 0 and 1023)
analogValue3 = analogRead(analogPin4);     // read the analog input (value between 0 and 1023)


Servoposition1();
Servoposition2();
Servoposition3();
Servoposition4(); 
}

void Servoposition1() {
Serial.println(analogValue0);
 if(analogValue0==0)
   {
    pos0myservo=0;
   }
else if(analogValue0>511)
   {
    pos0myservo=0;
   }
else 
   {
   pos0myservo=0;
   }
pos0myservo=constrain(pos0myservo,0, 179);  
              myservo.write(pos0myservo);     // write the new mapped analog value to set the position of the servo
              delay(50);                   // waits for the servo to get there

}

void Servoposition2() {
Serial.println(analogValue1);
if(analogValue1>511)
   {
    pos1myservo=1;
   }
else 
   {
   pos2myservo=0;
   }
  pos1myservo=constrain(pos1myservo,0, 179);  
              myservo.write(pos1myservo);     // write the new mapped analog value to set the position of the servo
              delay(50);                   // waits for the servo to get there
}

void Servoposition3() {
Serial.println(analogValue2);
if(analogValue2>=511)
   {
    pos2myservo+=1;
   }
else 
   {
   pos2myservo-=0;
   }
 pos2myservo=constrain(pos2myservo,0, 179);  
              myservo.write(pos2myservo);     // write the new mapped analog value to set the position of the servo
              delay(50);                   // waits for the servo to get there
}

void Servoposition4() {
Serial.println(analogValue3);
if(analogValue3>=511)
   {
    pos3myservo+=1;
   }
else 
   {
   pos3myservo-=0;
   }
pos3myservo=constrain(pos3myservo,0, 179);  
              myservo.write(pos3myservo);     // write the new mapped analog value to set the position of the servo
              delay(50);                   // waits for the servo to get there
}

Here is the second one

// Pin definitions
#define servoPin 9  // Use only pin 9 or 10 for servo!!!!
#define positie1Pin 1
#define positie2Pin 2
#define positie3Pin 3

// Servo angle positions (0..90)
#define positie1Angle 20
#define positie2Angle 60
#define positie3Angle 80

// Servo initialize position
#define servoInitPosition positie1Angle


// start software
// Use arduino's server library
#include <Servo.h>

// Create servo object
Servo controlServo;

// Create position values
int currentPos;
int setPos;

void setup() {
  // Attach the servo object to the servo pin.
  controlServo.attach(9);
  // Set to init position
  controlServo.write(servoInitPosition);

  // Init the button pins, enable input and pull up resistor (position switch should pull to gnd)
  pinMode(positie1Pin, INPUT_PULLUP);
  pinMode(positie2Pin, INPUT_PULLUP);
  pinMode(positie3Pin, INPUT_PULLUP);

  // reset position
  currentPos = 0;
  setPos = 0;
}

void loop() {
  // Start reading the position switch with a very simple debouncer
  int pos1Value1 = analogRead(positie1Pin);
  int pos2Value1 = analogRead(positie2Pin);
  int pos3Value1 = analogRead(positie3Pin);

  // wait +-50 ms, then if value is still the same, value should be right
  delay(50);
  
  // Start reading the position switch with a very simple debouncer
  int pos1Value2 = analogRead(positie1Pin);
  int pos2Value2 = analogRead(positie2Pin);
  int pos3Value2 = analogRead(positie3Pin);

  if (pos1Value1 == 0) // value 0 is button pressed (or pos switch on this position)
  {
    if (pos1Value1 == pos1Value2) // 50ms difference, same is really push
      setPos = 1;
  }
  if (pos2Value1 == 0) // value 0 is button pressed (or pos switch on this position)
  {
    if (pos2Value1 == pos2Value2) // 50ms difference, same is really push
      setPos = 2;
  }
  if (pos3Value1 == 0) // value 0 is button pressed (or pos switch on this position)
  {
    if (pos3Value1 == pos3Value2) // 50ms difference, same is really push
      setPos = 3;
  }

  if (setPos != currentPos)
  {
    currentPos = setPos;
    switch (currentPos)
    {
      case 1: controlServo.write(positie1Angle);
      case 2: controlServo.write(positie2Angle);
      case 3: controlServo.write(positie3Angle);
    }
  }

  // Wait for a while, we don't need to check this often if the position is changed
  delay(100);
  

}

Both are not working. Maybe any of you got some suggestions?

Thanks.

Have you installed the pull down resistors that I suggested?

Yes, 10K. I dont have 4K7.

What about the PULLUP and PULLDOWN functions that are built in de AtMega?

In the attachment you could see the setup you mentioned?

IMG_2327.PNG

Yes, those are pull down resistors. In your first code, you enable the internal pull ups on the switch pins, which defeats the purpose of the pull downs and is a reason that the code won't work.