Simple code for 360pwm servo using adafruit library and PCA9685 board

Its been awhile since I've had time to finish my grandsons request to make him a robot. Since then things have improved with controller boards such as PCA9685 from Adafruit. I have code working for 2 years using the IR remote code and now due to complexity of the robot now incorporate the Adafruit library using the PCA9685 boards to make it easier to add servos in the future. I know this topic is picked over many times and your most likely sick of the requests but all I need is a simple forward full speed PWM, stop and backward full speed code that I can add to my project.
Currently I'm using I2C in slot 0 for voice req module, "working", and the I2C1 for the PCA9685 board which works for the 0-180 SG90 servos but not the FS90R (360 type). I have tried so many code styles I am now completely lost. UGGGG
Any help with a simple pwm . setPWM (?.?.?) would be very much appreciated.

Please post the simplest possible but complete sketch that should drive the 360 degree "servo" at full speed that you say does not work. Either direction will do. Along with it please post a schematic showing how the components are connected and powered

Hello, thanks for the fast reply. Below is the part that doesn't work. I have two FS90R servos in mirror to each other so need to drive in CW and CCW. Example, from the back of the robot the left leg is left right is right, left would have to turn CCW and the right CW for the robot to "GO FORWARD" and reverse code to "GO BACKWARD"

the servo pins left in PCA9685 are pin 4 and the right in 5

// case 22: //Fixed command "Go Forward"
//pwm.setPWM(SERVOPINLT, 0, SERVO_REVERSE);
//pwm.setPWM(SERVOPINRT, 0, SERVO_FORWARD);
//delay(1000);
//Serial.println("received 'Go Forward',command flag '22'");
//pwm.setPWM(SERVOPINLT, 0, 1500);
//pwm.setPWM(SERVOPINRT, 0, 1500);
//delay(100);
//break;

Iam not sure how to work this code format thing sorry.

Posting a snippet of code rather than a complete sketch is usually a waste of time, as it is in this case

Posting your code using code tags prevents parts of it being interpreted as HTML coding and makes it easier to copy for examination

In my experience the easiest way to tidy up the code and add the code tags is as follows

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

It is also helpful to post error messages in code tags as it makes it easier to scroll through them and copy them for examination

UKHeliBob, I forgot to add this info...

Arduino Mega, PCA9685 board is plugged into the I2C1 port and Power is coming from the 5v pin and gnd to mega. The servo power is from separate battery pack into terminal on the PCA9685 board.

Ok I will send that code soon....

#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

// Define the PCA9685 address and PWM frequency
#define PCA9685_ADDRESS 0x40
#define PWM_FREQUENCY 50
#define SERVO_MIN 150  //150
#define SERVO_MAX 600  //600
//below for continuous leg servos
#define SERVO_FORWARD 125  //1000ms  125
#define SERVO_STOP 375     //1500ms  315
#define SERVO_REVERSE 450  //2000ms  405
#define SERVOPINLT 4       //left leg
#define SERVOPINRT 5       //right leg


// Create a PCA9685 object
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(PCA9685_ADDRESS);

//I2C communication
DFRobot_DF2301Q_I2C asr;
//Arduino mega 2560

void setup() {

  Serial.begin(115200);  //enable if plugged into pc to troubleshoot code
  Serial.println("Servo control with PCA9685");

  pwm.begin();
  pwm.setPWMFreq(PWM_FREQUENCY);



  //enable voice recognition
  while (!(asr.begin())) {
    Serial.println("Communication with Device failed, please check connection");
    delay(3000);
  }

  Serial.println("Begin ok!");
  asr.setVolume(7);     //value(1-7)
  asr.setMuteMode(0);   //Mute value 1, unmute value 0
  asr.setWakeTime(30);  //Wake duration (0-255)

  //below is for voice module dont edit
  uint8_t wakeTime = 0;
  wakeTime = asr.getWakeTime();
  Serial.print("wakeTime = ");
  Serial.println(wakeTime);

  //asr.playByCMDID(1);   //Wake-up command
  //asr.playByCMDID(20);  //Command word ID
}

void rotateServo(uint16_t pulseLength) {
  pwm.setPWM(SERVOPINLT, 0, pulseLength);
  pwm.setPWM(SERVOPINRT, 0, pulseLength);
}


void loop() {

  uint8_t CMDID = asr.getCMDID();

  switch (CMDID) {
    case 5:  //Custom command "Left Arm Up"
      for (uint16_t pulseLength = SERVO_MAX; pulseLength > SERVO_MIN; pulseLength--) {
        pwm.setPWM(0, 0, pulseLength);  // Channel 0, 0 for off, pulselen for on
        delay(5);                       //speed for action
      }
      Serial.println("received 'Left Arm Up',command flag '5'");
      delay(100);
      break;

    case 6:  //Custom command "Left Arm Down"
      for (uint16_t pulseLength = SERVO_MIN; pulseLength < SERVO_MAX; pulseLength++) {
        pwm.setPWM(0, 0, pulseLength);
        delay(5);
      }
      Serial.println("received 'Left Arm Down',command flag '6'");
      break;

    case 7:  //Custom command "Right Arm Up"
      for (uint16_t pulseLength = SERVO_MIN; pulseLength < SERVO_MAX; pulseLength++) {
        pwm.setPWM(2, 0, pulseLength);  // Channel 0, 0 for off, pulselen for on
        delay(5);                       //speed for action
      }
      Serial.println("received 'Right Arm Up',command flag '7'");
      delay(100);
      break;

    case 8:  //Custom command "Right Arm Down"
      for (uint16_t pulseLength = SERVO_MAX; pulseLength > SERVO_MIN; pulseLength--) {
        pwm.setPWM(2, 0, pulseLength);
        delay(5);
      }
      Serial.println("received 'Right Arm Down',command flag '8'");
      delay(100);
      break;

    case 9:  //Custom command "Left Gripper Open" fixx this not working command
      for (uint16_t pulseLength = SERVO_MIN; pulseLength < SERVO_MAX; pulseLength++) {
        pwm.setPWM(1, 0, pulseLength);  // Channel ?, 0 for off, pulselen for on
        delay(1);                       //speed for action
      }
      Serial.println("received 'Left Gripper Open',command flag '9'");
      delay(100);
      break;

    case 10:  //Custom command "Left Gripper Close"
      for (uint16_t pulseLength = SERVO_MAX; pulseLength > SERVO_MIN; pulseLength--) {
        pwm.setPWM(1, 0, pulseLength);
        delay(1);
      }
      Serial.println("received 'Left Gripper Close',command flag '10'");
      delay(100);
      break;

    case 22:  //Fixed command "Go Forward"
      pwm.setPWM(SERVOPINLT, 0, SERVO_REVERSE);
      pwm.setPWM(SERVOPINRT, 0, SERVO_FORWARD);
      delay(1000);
      Serial.println("received 'Go Forward',command flag '22'");
      pwm.setPWM(SERVOPINLT, 0, 1500);
      pwm.setPWM(SERVOPINRT, 0, 1500);
      delay(100);
      break;

    case 23:  ////Fixed command "Retreat"
      pwm.setPWM(SERVOPINLT, 0, SERVO_FORWARD);
      pwm.setPWM(SERVOPINRT, 0, SERVO_REVERSE);
      delay(1000);
      Serial.println("received 'Retreat',command flag '23'");
      pwm.setPWM(SERVOPINLT, 0, 1500);
      pwm.setPWM(SERVOPINRT, 0, 1500);
      delay(100);
      break;

    case 25:  //Fixed command "Turn Left 90degs"
      pwm.setPWM(SERVOPINLT, 0, SERVO_FORWARD);
      pwm.setPWM(SERVOPINRT, 0, SERVO_REVERSE);
      delay(2000);
      Serial.println("received 'Turn Left 90degs',command flag '25'");
      pwm.setPWM(SERVOPINLT, 1500, 1500);
      pwm.setPWM(SERVOPINRT, 1500, 1500);
      delay(100);
      break;

    case 26:  //Fixed command "Turn Left 45degs"
      pwm.setPWM(SERVOPINLT, 0, SERVO_FORWARD);
      pwm.setPWM(SERVOPINRT, 0, SERVO_REVERSE);
      delay(1000);
      Serial.println("received 'Turn Left 45degs',command flag '26'");
      pwm.setPWM(SERVOPINLT, 1500, 1500);
      pwm.setPWM(SERVOPINRT, 1500, 1500);
      delay(100);
      break;

    case 27:  //Fixed command "Turn Left 30degs"
      pwm.setPWM(SERVOPINLT, 0, SERVO_FORWARD);
      pwm.setPWM(SERVOPINRT, 0, SERVO_REVERSE);
      delay(1000);
      Serial.println("received 'Turn Left 30degs',command flag '27'");
      pwm.setPWM(SERVOPINLT, 1500, 1500);
      pwm.setPWM(SERVOPINRT, 1500, 1500);
      delay(100);
      break;

    case 29:  //Fixed command "Turn Right 45degs"
      pwm.setPWM(SERVOPINLT, 0, SERVO_FORWARD);
      pwm.setPWM(SERVOPINRT, 0, SERVO_REVERSE);
      delay(1000);
      Serial.println("received 'Turn Right 45degs',command flag '29'");
      pwm.setPWM(SERVOPINLT, 1500, 1500);
      pwm.setPWM(SERVOPINRT, 1500, 1500);
      delay(100);
      break;

    case 30:  //Fixed command "Turn Right 30degs"
      pwm.setPWM(SERVOPINLT, 0, SERVO_FORWARD);
      pwm.setPWM(SERVOPINRT, 0, SERVO_REVERSE);
      delay(1000);
      Serial.println("received 'Turn Right 30degs ',command flag '30'");
      pwm.setPWM(SERVOPINLT, 1500, 1500);
      pwm.setPWM(SERVOPINRT, 1500, 1500);
      delay(100);
      break;

    default:
      if (CMDID != 0) {
        Serial.print("CMDID = ");  //Printing command ID
        Serial.println(CMDID);
      }
      //if (case 25){ pwm.setPWM(3, 1500, 1600 );
      //delay(1000); rotate head in direction to command (testing not in use)

      delay(300);
  }
}

type or paste code here

You are making it difficult to provide help. What I was looking for was

The aim is to isolate driving the 360 degree "servos" from any other code in order to troubleshoot your problem. The reason that I asked for a schematic (a photo of a hand drawn circuit will do) is that any description often leaves too many questions unanswered, as in this case, such as do the servos and the Mega have a common GND connection ?

I am sorry if these requests seem pedantic, but my experience of the forum over many years has shown that the problem is often not where the OP thinks it is or that there are facts not revealed until more questions are asked

The fact of the matter is that if you can command a proper servo to a desired angle then it should be trivial to apply the same process to a 360 degree "servo" to make it rotate at a given speed in a given direction

Do you mean a hand drawen "sketch" ... i thought you wanted the sketch from IDE

I, and others, would like to see a schematic of your project. It could be in the form of a hand drawn circuit that you have photographed or a more formal schematic drawn using Kicad, EasyEDA or any other PCB design software

That is in addition to the simple Arduino sketch, ie your code

ok will send when i have something drawn up

Not sure how this will help. But I'am a man of my word. I will look at my current code to see where I messed up. thanks for your time and your reply

It doesn't really help as many connections are not clear, but it does raise at least one question

Exactly what type of 9V battery are you using ?
Is it by any chance a PP3 like this ?

Here's a 360 servo simulation using Servo.h degree writes...

Well it helps in seeing what you have, and you do not have 360˚ servos. These are rare things.

Sometimes you have what are miss called 360˚ servos and are just continuous movement servos. All you can do with these is control the speed and direction, you can not control the angle it points at. This is because this type of servo is simply a normal servo with its positional feedback circuit disconnected.

But not using a PCA9685, I see

True. I missed that.

yes

yes they are labeled continuous servos

all servos are connected thru the PCA9685. I know the picture is messy, sorry about that.

Such a battery is not appropriate to power an Arduino board for more than a few minutes. They are designed to provide a small current for a long times, as in a smoke detector. Can you measure the voltage of the battery whilst it is under load in your project ?