Custom Electronic locking mechanism for 120r John Deere front loader

I am trying to make a mechanism that allows me to lock in attachments from the seat without getting off of the tractor and doing to manually. I am planning on using a servo or motor for the locking mechanism. The problem is I have a problem in my code/ I don’t know how to do it.

The code is supposed to sense when the switch is flipped and lock the attachment into place by moving the motor for 1 sec then stopping and the same thing when the switch is flipped off.

I am using a 2 color LED to simulate the motor running one way and the other.

The problem is that the LED just flickers and I don’t know how to fix it.

HERE IS MY CODE:

//Tractor Test Code
//By: Jackson Poer
const int SwitchPos = A0;
byte Val = 0;
const int delay1 = 1000; //change this number to lengthen the servo duration!
void setup() {                     
 Serial.begin(9600);                       //Open terminal window
 pinMode(SwitchPos, INPUT);  
 pinMode(9,OUTPUT);
 pinMode(10,OUTPUT);


}

void loop() {
 Val = analogRead(A0);                     //Measure Value

 Serial.print("Switch Position = ");  //Print text
 Serial.println(Val);                      //Print value
 delay(100);
 if(Val <200){digitalWrite(9,HIGH); 
              digitalWrite(10,LOW);
 delay(delay1);
              digitalWrite(9,LOW); 
              digitalWrite(10,LOW);
   }
 else{digitalWrite(10,HIGH); 
      digitalWrite(9,LOW);
 delay(delay1);
      digitalWrite(10,LOW); 
      digitalWrite(9,LOW);
 
 }





}

Im using a Arduino UNOr3/ thats what it says on it

What sort of switch are you using? How is it connected to the Arduino?

It seems strange to be using analogRead() to detect a switch - digitalRead() would be more usual. You can use digitalRead(0 digitalRead() on an analog pin as well as on digital pins.

...R

i have messed with the code for a hour now and have gotten it to work along with added your tips/suggestions

thx