I'm attempting to control 6 servo motors in a 6-axis robotic arm.
Video of the problem I'm having: https://youtu.be/vQaEbI6lFIM
Schematic:
Everything seems ok, except I'm having trouble controlling RDS3218 with RV6_base potentiometer.
Code:
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
#define MIN_PULSE_WIDTH 650
#define MAX_PULSE_WIDTH 2350
#define FREQUENCY 50
#define CONVERSION_RATE 4096
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
// pins on Uno
int potWrist = A0;
int potForearm = A1;
int potElbow = A2;
int potShoulder = A3;
int potBase = A4;
// pins on driver board
int hand = 0;
int wrist = 1;
int forearm = 2;
int elbow = 3;
int shoulder = 4;
int base = 5;
void setup() {
delay(3000);
pwm.begin();
pwm.setPWMFreq(FREQUENCY);
pwm.setPWM(hand, 0, 90); // hand close at 90°
pinMode(13, INPUT_PULLUP); // switch for hand
Serial.begin(9600);
Serial.println("Initialized.");
}
void moveMotor(int controlIn, int motorOut, bool isBase) {
float mappedValue;
int pulse_width, potVal;
potVal = analogRead(controlIn); //read potentiometer
if (isBase) {
mappedValue = map(potVal, 0, 1023, 500, 2500); //RDS3218's PWM values are between 500~2500uS
pulse_width = int(mappedValue / 1000000 * FREQUENCY * CONVERSION_RATE);
} else {
mappedValue = map(potVal, 150, 900, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);
pulse_width = int(mappedValue / 1000000 * FREQUENCY * CONVERSION_RATE);
}
pwm.setPWM(motorOut, 0, pulse_width);
}
void loop() {
moveMotor(potWrist, wrist, 0);
moveMotor(potForearm, forearm, 0);
moveMotor(potElbow, elbow, 0);
moveMotor(potShoulder, shoulder, 0);
moveMotor(potBase, base, 1);
int pushButton = digitalRead(13);
if (pushButton == LOW) {
pwm.setPWM(hand, 0, 180); //hand closed if pin is closed
}
else {
pwm.setPWM(hand, 0, 90); //hand open if pin is open
}
}
The problem I'm experiencing:
All servo motors only move between 0~180°, except the one for base. It should move from 0~270°, and according to its data, PWM should be 500us~2500us.
The way I set it now, when I turn RV6_base potentiometer to a certain degree, everything stops working, other motors included. I have to reset Arduino for it to work, sometimes even that doesn't work. I'm guessing the problem is within the code, but I'm out of idea.




