Problem using PCA9685 with L298 motor driver(SOLVED)

I'm trying to run servo motors with the PCA9685 and the 12V DC motors from Arduino Mega2560 pins to run the H Bridge L298 drivers.
My project needs 19 PWM pins and the mega2560 only has 15.

Anyone know how to modify the Adafruit library to make it work?

My error: cannot convert 'Adafruit_PWMServoDriver to 'int' for argument '2'

On the line for the " analogWrite(enable1, pwm); " for the 12V motor enable

Thanks for the replies everyone. I think I'll junk the PCA, and just link an Uno to get the extra PWM pins I need. A lot simpler fix.

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

Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

#define MIN_PULSE_WIDTH 650
#define MAX_PULSE_WIDTH 2350
#define DEFAULT_PULSE_WIDTH 1500
#define FREQUENCY 50

const int rc_ch_9= 11;
const int SWC_Min = 0;
const int SWC_Max = 1550;
const int in1 = 39; // to in1 on driver
const int in2 =38; // to in2 on driver
const int enable1 = 6; // ~pwm to enable1 on driver
const int analogPin_1 = A2;
int threshold_Min;
int threshold_Max;
int range;
int val;

uint8_t servonum = 0;

void setup(){
pinMode(rc_ch_9, INPUT);
Serial.begin(9600);
Serial.println("16 channel Servo test!");
pwm.begin();
pwm.setPWMFreq(FREQUENCY);
}

int pulseWidth(int angle){
int pulse_wide, analog_value;
pulse_wide = map(angle, 0, 180, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);
analog_value = int(float(pulse_wide) / 1000000 * FREQUENCY * 4096);
Serial.println(analog_value);
return analog_value;
}

void loop() {
val=0;
int SWC= pulseIn(rc_ch_9, HIGH, 25000);

int analogValue_1 = analogRead(analogPin_1);
//Serial.print(analogValue_1);
delay(10);

range = map( SWC, SWC_Min, SWC_Max, 0, 3); //map the pulse low and high for switch

switch (range){

case 0:
pwm.setPWM(0, 0, pulseWidth(200));
delay(10);
break;

case 1:
pwm.setPWM(0, 0, pulseWidth(-10));
delay(10);
break;

case 2:
pwm.setPWM(0, 0, pulseWidth(200));

break;
}
delay(10);
if (SWC < 1550 && analogValue_1 > threshold_Min){ // SWITCH OFF
threshold_Min=200;
threshold_Max=700;
pwm = map(SWC, 1550, 1000, 200, 255); //map speed
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
analogWrite(enable1, pwm);
Serial.print(pwm);
delay(1);
}

if (SWC < 1550 && analogValue_1 <= threshold_Min){
threshold_Min=200;
threshold_Max=700;threshold_Min=200;
threshold_Max=700;
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
analogWrite(enable1, 0);

delay(10);
}

if (SWC > 1550 && analogValue_1 < threshold_Max){ //SWITCH ON
threshold_Min=200;
threshold_Max=700;
pwm = map(SWC, 1550, 2000, 200, 255); //map speed
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
analogWrite(enable1, pwm);
Serial.print(pwm);
delay(10);
}
if( SWC > 1550 && analogValue_1 >= threshold_Max){
threshold_Min=200;
threshold_Max=700;

digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
analogWrite(enable1, 0);
//Serial.println( " DRIVE ON " );
delay(10);
}

Serial.print(analogValue_1);
delay(10);
}

/code]

Would it be thinkable to use another servo library? That ought to be a lot faster, easier and more predictable.

Please fix your code display. Reformat your code (CTRL-T), Copy for the forum and Paste it, or at least select it all and click </>.

You can not use a PWMServoDriver object (pwm) for the duty cycle in analogWrite(). Dunno what you do wrong, perhaps pwm should be a local int variable here?

Doesn't the library have multiple constructors or begin() methods, for various driver boards?

ray1331:
My project needs 19 PWM pins and the mega2560 only has 15.

The logical though is to use software PWM for the remaining channels.

ray1331:
I'm trying to run servo motors with the PCA9685 and the 12V DC motors from Arduino Mega2560 pins to run the H Bridge L298 drivers.

Can't mix that on a single PCA9685.
Servos need a 1-2ms pulse, with a frequency of 50Hz.
DC motors may need 40-100% PWM at a higher frequency.

How many servos and how many DC motors are we talking about?
Leo..

ray1331:
My error: cannot convert 'Adafruit_PWMServoDriver to 'int' for argument '2'

On the line for the " analogWrite(enable1, pwm); " for the 12V motor enable

I'm sure that error is not from the code you posted.

In that code, pwm has never been given a type, so the compiler will fall over that first.

Related, you seem to address pwm as some kind of object on one line, then use it to store a value (from the map function) on another. That just can't be right, either. Well, with proper type declarations you can make it work of course - that's C++ for you, allowing you to shoot yourself in the foot in innumerable ways.