Cant get more than 8 servos working

Hello Everyone

I am trying use the pca 9685 to drive servos for turnouts on my model railroad using JMRI/CMRI nodes. I can get 8 servos working correctly but when i add more they start making random 5 or so degree movements and are unresponsive to any commands from JMRI. I have checked that all servos work with the ladyada servo sketch and all 16 servos cycled correctly so i dont think its hardware related. I have tried adding 1 servo at a time into the sketch but when i hit number 9 it stops functioning correctly. Here is the code I am trying to use any help figuring this out would be appreciated
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
#include <CMRI.h>
#include <Auto485.h>

#define CMRI_ADDR 1
#define DE_PIN 2
#define numServos 16 //The number of servos connected

Adafruit_PWMServoDriver pwm1 = Adafruit_PWMServoDriver(0X40); //setup the board address
Adafruit_PWMServoDriver pwm2 = Adafruit_PWMServoDriver(0X41); //setup the board address
//Adafruit_PWMServoDriver pwm3 = Adafruit_PWMServoDriver(0X42); //setup the board address

Auto485 bus(DE_PIN); // Arduino pin 2 -> MAX485 DE and RE pins
CMRI cmri(CMRI_ADDR, 24, 48, bus);

int Status[numServos]; //Create a table to hold the status of each turnout, signal, etc.
int Throw[numServos]; //Create a table to hold the throw value for each servo
int Close[numServos]; //Create a table to hold the close value for each servo

void setup() {
Serial.begin(9600);
bus.begin(9600);
pwm1.begin();
pwm1.setPWMFreq(60); // This is the maximum PWM frequency
pwm2.begin();
pwm2.setPWMFreq(60); // This is the maximum PWM frequency
// pwm3.begin();
// pwm3.setPWMFreq(60); // This is the maximum PWM frequency

//SET THE THROW AND CLOSE VALUES FOR EACH SERVO BASED ON THE CALIBRATION PROCESS

//Servo`s Board1 - point motor
Throw[0] = 600;
Close[0] = 2300;
Throw[1] = 600;
Close[1] = 2300;
Throw[2] = 600;
Close[2] = 2300;
Throw[3] = 600;
Close[3] = 2300;
Throw[4] = 600;
Close[4] = 2300;
Throw[5] = 600;
Close[5] = 2300;
Throw[6] = 600;
Close[6] = 2300;
Throw[7] = 600;
Close[7] = 2300;
// Throw[8] = 600;
// Close[8] = 2300;
// Throw[9] = 600;
// Close[9] = 2300;
// Throw[10] = 600;
// Close[10] = 2300;
// Throw[11] = 600;
//Close[11] = 2300;
// Throw[12] = 600;
// Close[12] = 2300;
// Throw[13] = 600;
// Close[13] = 2300;
// Throw[14] = 600;
// Close[14] = 2300;
// Throw[15] = 600;
// Close[15] = 2300;

// Board2 Servo connection 0 - point motor
// Throw[16] = 600;
//Close[16] = 2300;

// Board3 Servo connection 0 - point motor
//Throw[32] = 600;
//Close[32] = 2300;
}

void loop(){
cmri.process();

// Board 1
for (int i = 0; i < numServos; i++) {

    Status[i] = (cmri.get_bit(i));
    if (Status[i] == 1){
        pwm1.writeMicroseconds(i, Throw[i]);
    }
    else {
        pwm1.writeMicroseconds(i, Close[i]);
    }

    
    // Board 2

    Status[i+16] = (cmri.get_bit(i+16));
    if (Status[i+16] == 1){
        pwm2.writeMicroseconds(i, Throw[i+16]);
    }
    else {
        pwm2.writeMicroseconds(i, Close[i+16]);
    }
        // Board 3

   // Status[i+32] = (cmri.get_bit(i+32));
   // if (Status[i+32] == 1){
     //   pwm3.writeMicroseconds(i, Throw[i+32]);
    //}
    //else {
      //  pwm3.writeMicroseconds(i, Close[i+32]);
    }
}

// }

`

This and another line like it nearby appear to be referencing array elements that do not exist.

You size them to numServos or 16, i + 16 will run past that in you for loop.

HTH

a7

thanks i missed that one when modifing the sketch

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