Landing gear

Hey everyone,
Im completly new in coding and i need some help…
Im trying to make landing gear for my plane but i have little trouble.

void loop() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration*0.034/2; // ground distance
  ch1 = pulseIn(4, HIGH, 25000);
  if ((ch1>=1100) && (distance>=300)){
    door_lock.write(40); // open front
    digitalWrite(mosfetPin, LOW); // led off
    delay(2000); //изчаква 
    front_left.write(60);  // gear up
    front_right.write(120);
    digitalWrite(in1_3, LOW); // gear up
    digitalWrite(in2_4, HIGH);
    delay(8000); // изчаква
    digitalWrite(in1_3, LOW); // stop gear up
    digitalWrite(in2_4, LOW);
    door_lock.write(140); // close front
    door_all.write(140); // close all
    
  }
  ch1 = pulseIn(4, HIGH, 25000);
  if(ch1<1100){
    door_lock.write(40); // open front
    door_all.write(40); // open all
    delay(2000); //wait
    front_left.write(180); // gear down
    front_right.write(0);           
    digitalWrite(in1_3, HIGH); // gear down
    digitalWrite(in2_4, LOW);
    delay(8000); // изчаква
    digitalWrite(in1_3, LOW); // stop gear down
    digitalWrite(in2_4, LOW);
    door_lock.write(140); // close front
    digitalWrite(mosfetPin, HIGH); // led on
  }     
}

Its working but i want constantly to be checking about the pulse(ch1) and height but to execute “if” only one time(not to be looping). I dont know how to do it. I tried with “if” and “for” outside loop but unsuccessfully. Thanks!

Save the state of the landing gear, if the gear is up and ch1 < 1100, lower the gear and change the state to down.

bool landingGearDown = false;

if (!landingGearDown && (distanceToGround < threshold))
{
  lowerTheLandingGear();
  landingGearDown = true;
}
bool LandGearUp = false;

void setup() {
  pinMode(trigPin, OUTPUT); // ultrasonic
  pinMode(echoPin, INPUT); // ultrasonic
  
  pinMode(4, INPUT); // приемник

  pinMode(in1_3, OUTPUT); // мотори
  pinMode(in2_4, OUTPUT); // мотори

  pinMode(mosfetPin, OUTPUT); // ЛЕД
  
  front_left.attach(9,600,2300);  // жълт кабел
  front_right.attach(10,600,2300);  // сив кабел
  door_all.attach(11,600,2300);  // всички капаци
  door_lock.attach(6,600,2300);  // капаци колесник и ключалка
  
  Serial.begin(9600);
}
void loop() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration*0.034/2; // разстояние на самолета от земята
  ch1 = pulseIn(4, HIGH, 25000);
  if ((LandGearUp = false) && (ch1>=1100)){
    door_lock.write(40); // отваря капак преден колесник и ключалка
    digitalWrite(mosfetPin, LOW); // изключва лампи
    delay(2000); //изчаква 
    front_left.write(60);  // качва колесник
    front_right.write(120);
    digitalWrite(in1_3, LOW); // качва заден колесник
    digitalWrite(in2_4, HIGH);
    delay(8000); // изчаква
    digitalWrite(in1_3, LOW); // спира качване заден колесник
    digitalWrite(in2_4, LOW);
    door_lock.write(140); // затваря капак предпазител преден колесник
    door_all.write(140); // затваря всички капаци
    LandGearUp = true;
  }
  if((LandGearUp = true) && (ch1<1100)){
    door_lock.write(40); // отваря капак преден колесник и ключалка
    door_all.write(40); // отваря всички капаци 
    delay(2000); //изчаква
    front_left.write(180); // свален преден колесник
    front_right.write(0);           
    digitalWrite(in1_3, HIGH); // сваля заден колесник
    digitalWrite(in2_4, LOW);
    delay(8000); // изчаква
    digitalWrite(in1_3, LOW); // спира сваляне заден колесник
    digitalWrite(in2_4, LOW);
    door_lock.write(140); // затваря капак предпазител преден колесник
    digitalWrite(mosfetPin, HIGH); // включва лампи
    LandGearUp = false;
  }     
}

Like that? Its not working again… Its looping again
EDIT: Acctually my mistake… Its working now. Thank you! +Karma

This is an assignment "(LandGearUp = false)", you must use the compare operator "==" instead. Same error two places in your code.

if ((LandGearUp = false

Oops

Yes, my mistake i corrected it and its working

I have another question.
Is it possible to make 2 servos(running in exact opposite directions) to run on the same time. Because now they are on different functions and there is little delay between them.
Thanks.

Yes, just do back-to-back writes.

AleksBG: Is it possible to make 2 servos(running in exact opposite directions) to run on the same time. Because now they are on different functions and there is little delay between them.

Yes, but you need to provide the problematic code - a minimal sample will do - in order to get qualified help.

Sorry. Its part of this code that i uploaded before. Im talking about this:

front_left.write(180);
    front_right.write(0);

They are in oposite direction and they are rotating one part. There is little delay when i start it.