Robotic arm not working

Hello, I have been working on an Arduino robotic arm mechanism that uses two servos, a potentiometer, and a rotary encoder. The potentiometer controls a servo motor that moves the arm up and down, while the rotary encoder controls another servo that spins the arm forward or backward when the rotary encoder is adjusted. I have thoroughly tested all components using various programs that I found online, so I know that both servos as well as all other components work fine. However, when I uploaded my program (which is basically a combination two different programs I found on the internet) Only the servo that was controlled by the potentiometer worked. The servo that was controlled by the rotary encoder worked correctly in other programs, but not in this one for some reason. here is the code:

#include <Servo.h>

#define CLK_PIN 2
#define DT_PIN 3
#define SW_PIN 4
#define SERVO_PIN 10

#define DIRECTION_CW 0   
#define DIRECTION_CCW 1  

int counter = 0;
int direction = DIRECTION_CW;
int CLK_state;
int prev_CLK_state;
int val = 0;
Servo servo;  
Servo myservo;
void setup() {
  Serial.begin(9600);
  pinMode(CLK_PIN, INPUT);
  pinMode(DT_PIN, INPUT);
  myservo.attach(9);
  prev_CLK_state = digitalRead(CLK_PIN);
  servo.attach(SERVO_PIN);  
  servo.write(0);
}

void loop() {
   val = analogRead(5);            
   val = map(val, 0, 1023, 0, 180);     
   myservo.write(val);                
   delay(15);
  
  CLK_state = digitalRead(CLK_PIN);

  
  if (CLK_state != prev_CLK_state && CLK_state == HIGH) {

    if (digitalRead(DT_PIN) == HIGH) {
      counter--;
      direction = DIRECTION_CCW;
    } else {
     
      counter++;
      direction = DIRECTION_CW;
    }

    Serial.print("DIRECTION: ");
    if (direction == DIRECTION_CW)
      Serial.print("Clockwise");
    else
      Serial.print("Counter-clockwise");

    Serial.print(" | COUNTER: ");
    Serial.println(counter);

    if (counter < 0)
      counter = 0;
    else if (counter > 180)
      counter = 180;

   
    servo.write(counter);
  }

  
  prev_CLK_state = CLK_state;
}

Any help is appreciated.

I see lots of various serial.Print() throughout the program, but I see NONE that are related to you trying to debug your logic. Why not?

Re-write your code to remove "magic numbers" - what is "5" encoder or potentiometer?

  val = analogRead(5);

Remove "delay()" anything... it does nothing.

  delay(15);

What do you see in the Serial Monitor?

As I have said previously, this program wasn't made by me and is just a combinations of two codes I found online. I am a novice when it comes to certain areas in Arduino programming, especially serial print commands.

So, what are you looking for? Someone to duplicate your robotic arm so they can test your code?

Thank you so much for the tip about removing the delay! The robot now works perfectly!

Meh. I do not believe this, but if it works for you, then let it ride. As you discover more techniques in writing programs for multiple device interaction, you will want to learn "Arduino multitasking" to direct any device to do any task at any time. Here is a link to a thorough tutorial. Six videos with accompanying code examples and description...