controlling servo through joystick using Pca9685 servo driver

Hii Sir I wanted to control my servos using pca9685 servo driver & I want exactly same control like using arduino's digital pins but I'm not able to combine this servo driver to fit in my arduino code..
How do I combine them ??
Here's is my normal arduino based code-

#include <VarSpeedServo.h>

//Joystick controlled robot
VarSpeedServo myServo1; // eyeblink right
VarSpeedServo myServo2; // eyeblink left
VarSpeedServo myServo3; // eye look right
VarSpeedServo myServo4; // eye look left
VarSpeedServo myServo5; // eye look up down
int servo1 = 7;
int servo2 = 5;
int servo3 = 6;
int servo4 = 9;
int servo5 = 11;
int joyX = 0;
int joyY = 1;
int button1 = 2;
int press1 = 90;
int JoySlack = 25; // Dead spot size
int center = 519; // Reading when joystick centered

void setup() {
  Serial.begin(9600);
  myServo1.attach(servo1);
  myServo2.attach(servo2);
  myServo3.attach(servo3);
  myServo4.attach(servo4);
  myServo5.attach(servo5);
  pinMode(button1, INPUT);
  digitalWrite(2, HIGH);
}

void loop() {
  Serial.print("X-axis: ");
  Serial.print(analogRead(joyX));
  Serial.print(" | ");
  Serial.print("Y-axis: ");
  Serial.print(analogRead(joyY));
  Serial.println(" | ");
  delay(200);
  press1 = digitalRead(button1);
    if (press1 == LOW)
    {
      myServo1.write(0);
      myServo2.write(0);
      myServo3.write(0);
    }
    else {
      myServo1.write(180);
      myServo2.write(180);
      myServo3.write(180);
    }
  int valX = analogRead(joyX);
  int valY = analogRead(joyY);
  if ((abs(valX - center) > JoySlack) || (abs(valY - center) > JoySlack))
  {
    valX = map(valX, 50, 1023, 0, 180);
    valY = map(valY, 50, 1023, 0, 180);

    myServo2.write(valX);
    myServo1.write(valX);
    myServo3.write(valY);
    myServo4.write(valX);
    myServo5.write(valY);
  }
  else
  {
    myServo2.write(90);
    myServo1.write(90);
    myServo3.write(90);
    myServo4.write(90);
    myServo5.write(90);
  }
  delay(200);
}

And here is I tried for pca9685 multi servo driver but got confused and not able to write to fit in my previous code

#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
//Joystick controlled robot
Adafruit_PWMServoDriver myServo1; // eyeblink right
Adafruit_PWMServoDriver myServo2; // eyeblink left
Adafruit_PWMServoDriver myServo3; // eye look right 
Adafruit_PWMServoDriver myServo4; // eye look left
Adafruit_PWMServoDriver myServo5; // eye look up down
int servo1 = 7;
int servo2 = 5;
int servo3 = 6;
int servo4 = 9;
int servo5 = 11;
int joyX = 0;
int joyY = 1;
int button1 = 2;
int press1 = 90;
int JoySlack = 25; // Dead spot size
int center = 519; // Reading when joystick centered

void setup() {
  Serial.begin(9600);
  myServo1.attach(servo1);
  myServo2.attach(servo2);
  myServo3.attach(servo3);
  myServo4.attach(servo4);
  myServo5.attach(servo5);
  pinMode(button1, INPUT);
  digitalWrite(2, HIGH);
}

void loop() {
  Serial.print("X-axis: ");
  Serial.print(analogRead(joyX));
  Serial.print(" | ");
  Serial.print("Y-axis: ");
  Serial.print(analogRead(joyY));
  Serial.println(" | ");
  delay(200);
  press1 = digitalRead(button1);
    if (press1 == LOW)
    {
      myServo1.write(0);
      myServo2.write(0);
      myServo3.write(0);
    }
    else {
      myServo1.write(180);
      myServo2.write(180);
      myServo3.write(180);
    }
  int valX = analogRead(joyX);
  int valY = analogRead(joyY);
  if ((abs(valX - center) > JoySlack) || (abs(valY - center) > JoySlack))
  {
    valX = map(valX, 50, 1023, 0, 180);
    valY = map(valY, 50, 1023, 0, 180);

    myServo2.write(valX);
    myServo1.write(valX);
    myServo3.write(valY);
    myServo4.write(valX);
    myServo5.write(valY);
  }
  else
  {
    myServo2.write(90);
    myServo1.write(90);
    myServo3.write(90);
    myServo4.write(90);
    myServo5.write(90);
  }
  delay(200);
}

Could you please help me on this??

Your code contains VarSpeedServo and there's no easy Adafruit_PWMServoDriver equivalent to that. But I can't see that you're using any of the VarSpeedServo specific features so I'm not sure exactly what you're looking for. If you're still looking to control the speed of servo movement then that involves a lot of work.

The basics of driving servos is covered very well in the examples that come with the Adafruit_PWMServoDriver particularly the one called "servo". Essentially servo.write() is replaced with pwm.setPWM() but the parameters for that are very different. Or to replace servo.writeMicroseconds you can use pwm.writeMicroseconds() which is perhaps a bit simpler.

Steve

Thanks @Slipstick Steve sir for your reply
I can use normal servo.h instead ofVarspeedservo.h no issue

What I wanted is that When I'm cotrolling servo with normal arduino digital pins using joystick is working fine

But I want to controll 12servo with arduino using same sketch but its not possible with arduino So I tried using pca9685 for controlling 12servo with same sketch/code but I'm not able to configure my code according to pca module

Like how do I control servo in similar way that controlling through arduino digital pins but now using pca9685?????????????

Have you seen the tutorial at Using the Adafruit Library | Adafruit PCA9685 16-Channel Servo Driver | Adafruit Learning System Have you looked at the servo example program that came with that Adafruit driver? What other information do you need?

BTW any Arduino can control 12 servos using the normal Servo library. If you need more or have run out of digital pins (remembering that you can use the analog pins as digital if needed) then a Mega has more pins and can handle a lot more servos.

Steve

Yes I looked at codes but didn't understand that how to assign pins in arduino for pca9685 and how to assign/attach pins for servo in pca9685??

I want my code to run with pca9685 but didn't understand how to configure
so can anyone rewrite the code but with pca9685??

If you follow the instructions in that tutorial and use the example program that will do most of it for you.

But if you just want to someone to write a program for you without you putting in any effort then you're in the wrong place. Try posting in the Gigs and Collaborations forum and be prepared to pay for the work.

Steve

Thanks Mr @slipstick Steve sir. I will be going to learn arduino programming on udemy :slight_smile:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.