PS2 Button controls to move servos using the PCA6985 16 channel PWM board

Hi All,

I am trying to control a small robot for my son. I am really new to coding and have figured out A LOT thus far hacking other peoples code (much appreciated Robojax and Brainy-Bits!!!). I am stuck interfacing the buttons on the PS2 controller with the PCA6985.

Any advise would be much appreciated as I am trying to get a 6 servo arm attachment working for him for Christmas.

Please see the attached project as apparently the code exceeded the character limit

PS2X_Servo_Test.ino (10.4 KB)

You state that you have made a lot of progress. Kudos! :slight_smile:
You state that you want to control a robot arm. Cool project!
You posted your code.

The only piece that looks like a description of your question or coding problems is that you are stuck interfacing PS2 buttons with the PCS6985.

That does not give us much detail about the difficulty you are having.

A better way to describe your issue is to state what you expect your code to do, what it actually does and how those two differ.

Edit: Do your debugging statements indicate that your code is properly reading the buttons on your controller?

Edit2: you have a zillion debug prints for a zillion buttons that you do not use. Maybe helpful for future work you plan to do. But when you get to the part where you are trying to move the servos, you are using buttons that were never tested using the serial print debug messages.

    //-----------------Servo Control-------------------------------
    if (ps2x.NewButtonState(PSB_L1)) {
      HCPCA9685.Servo(0, Servo0Position); // Move Servo 0
      Servo0Position++;
    }
    {
      delay(10); // Decrease to make Servos move faster or increase for slower movement

    }
    delay(100); // waits for the servo to get there
    if (ps2x.NewButtonState(PSB_L2)) {
      HCPCA9685.Servo(0, Servo0Position); // Move Servo 0
      Servo0Position--;
    }

Add a serial print for PSB_L1 to see if you can properly detect when it is pressed like you do for all of the other buttons.

Thanks vinceherman,

You are right, I need to print out what the buttons are doing.

I am trying to use all of the buttons (ie L1, L2, R1, R2, DPads, and Colored buttons) to control 6 servos using the PCA6985 board.

I would like to press say L1 and have servo 0 move in one direction while the button is pressed. Once released, servo 0 should stay at that position. Pressing and holding L2 should move servo 0 in the opposite direction until released. Once released, servo 0 should hold its position.

I’ve written the following code, but it breaks the motor control code for some reason…

 while (ps2x.Analog(PSAB_L1) == LOW) {
      if (angle > 0 && angle <= 180) {
        angle = angle - angleStep;
        if (angle < 0) {
          angle = 0;
        } else {
          delay(1); // Decrease to make Servos move faster or increase for slower movement
          HCPCA9685.Servo(0, angle); // Move Servo 0
        }
      }
      delay(100); // waits for the servo to get there
    }
    while (ps2x.Analog(PSAB_L2) == LOW) {
      if (angle >= 0 && angle <= 180) {
        angle = angle + angleStep;
        if (angle > 180) {
          angle = 180;
        } else {
          delay(1); // Decrease to make Servos move faster or increase for slower movement
          HCPCA9685.Servo(0, angle); // Move Servo 0
        }
      }
      delay(100);
    }

UPDATE…

I Was able to move servo 0 using L1 and L2 like i intended. While the buttons are pressed, the servo moves in the direction of which button is mapped to it. It stops and holds its position when the button is released, and keeps moving from its current position when the button is pressed again.

The only thing I need to figure out now is how to make make the “steps” larger.

 //-----------------Servo Control-------------------------------
    if (ps2x.Analog(PSAB_L1) > 0) {
      if (Servo0Position < 420) { // Check if maximum movement of Servo reached
        Servo0Position ++; // Increase Servo 0 Position variable by 1
      }
      delay(1); // Decrease to make Servos move faster or increase for slower movement
      HCPCA9685.Servo(0, Servo0Position); // Move Servo 0
      Serial.println(ps2x.Analog(PSAB_L1), DEC);
    }
    if (ps2x.Analog(PSAB_L2) > 0) {
      if (Servo0Position > 10) {
        Servo0Position --;
      }
      delay(1); // Decrease to make Servos move faster or increase for slower movement
      HCPCA9685.Servo(0, Servo0Position); // Move Servo 0
       Serial.println(ps2x.Analog(PSAB_L2), DEC);
    }
    delay(50);
  }
}

UPDATE #2

The code works as desired. Now I will copy and paste the code for the rest of the buttons (obviously changing the names for the buttons). Have a look at the code attached below.

PS2X_Servo_TestV2.ino (11 KB)

UPDATE #3

Code is complete!!! Feel free to use the project for yourselves.

Robot_FINAL.ino (13.3 KB)

Thanks for posting back. That will help future forum users.

If you get a chance, post some pics of your robot arm. It sounds like a cool project.