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.


//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);  


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

 Serial.print("Switch Position = ");  //Print text
 Serial.println(Val);                      //Print value
 if(Val <200){digitalWrite(9,HIGH); 


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.


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