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.