Using HCPCA9685 library & calling using a variable

I am reworking an earlier project which used the servo.h library to drive servos directly from the arduino. With help from this forum you enabled me to insert a variable into the call to the specific servo by using an array.

I am now running several more servos and using PCA9685 driver boards of which I have two. I have created two instances of the driver, but am again in the position where I need to insert a variable into the the call. However, this won't work using the array method (at least not as far as I've been able to attempt!).

How can I insert a variable into the line
HCPCA9685_1.Servo(3, angle);
where the instance of the driver required could be either HCPCA9695_1 or HCPCA9685_2 depending on the button pressed at the start of the action?

Here is my code to that point


/* Include the HCPCA9685 library */
#include <HCPCA9685.h>

/* I2C slave address for the device/module. For the HCMODU0097 the default I2C address
   is 0x40 */
#define  I2CAdd_1 0x40
#define  I2CAdd_2 0x41

/* Create an instance of the library */
HCPCA9685 HCPCA9685_1(I2CAdd_1);
HCPCA9685 HCPCA9685_2(I2CAdd_2);

  /* Set up arrays for signal servo positions */
  /* Note: Using HCPC9685 driver means positions are between 10 - 450 rather than angles in degrees
     Assuming halfway point (230) is equivalent to 90 degrees */
int butSig[24] = {0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10}; // For each button define the signal controlled
int sigOffAry[11] = {257,158,305,330,257,158,305,330,257,158,305}; // The 'Off' position for each signal
int sigSetAry[11] = {12,12,24,24,12,12,24,24,12,12,24}; // The additional 'Settle' position when pulling 'Off'
int sigBncAry[11] = {24,12,36,48,24,12,36,48,24,12,36}; // The degree of 'Bounce' when returning to 'On'
int bdelAry[11] = {10,10,2,2,10,10,2,2,10,10,2}; // The starting speed delay for each signal 
int sigStat[11] = {1,1,1,1,1,1,1,1,1,1,1}; // Status register for each signal. On=1, Off=0

  /* Set up variable to be used during signal setting */
int sigNo; // The signal number
int offAng; // The 'Off' position of the selected signal
int setAng; // The Settle when pulling off (Off position +/- Settle addition)
int setAdn; // The additional Settle position fetched from the array
int bncAng; // The degree of bounce fetched from the array
int bounce; // Bounce position = 230 +/- the bncAng
int bdel; // Delay used during move of signal arm
int bdelCount; // Counts used for speeding up fall of signal arm
int angle = 0; // Current position of Servo
byte buttonNo = 0; // The decimal value of the button pressed
int svDrvNo = 0;

void setup() 
{
  /* Initialise the input pins for 5-bit digital input */
  DDRB = DDRB | B11100000;
  /* Initialise the library and set it to 'servo mode' */ 
  HCPCA9685_1.Init(SERVO_MODE);
  HCPCA9685_2.Init(SERVO_MODE);
  
  /* Wake the device up */
  HCPCA9685_1.Sleep(false);
  HCPCA9685_2.Sleep(false);
}

void loop() 
{
  byte buttonNo = PINB & 0b00011111;
  while (buttonNo != 0) {
    sigNo = (butSig[buttonNo]); // Fetch relevant Signal/Servo number for the button pressed
    offAng = (sigOffAry[sigNo]); // Fetch the 'Off' angle for that signal
    bdel = (bdelAry[sigNo]); // Fetch the starting speed delay for that signal
    if sigNo <= 4 {
      svDrvNo = 1;
    } else {
      svDrvNo = 2;
    }
  if (buttonNo % 2) {
    // Result is odd - Action reqd is to pull signal Off
    if (sigStat[(sigNo)] == 1) { // If status is not 1, then signal is already Off so no action needed
        Serial.print("Action is to pull off:");
        Serial.println(sigNo);
        setAdn = (sigSetAry[sigNo]); // Fetch additional settle angle from array
        if (offAng > 230) {
          // off Action is clockwise
          setAng = ((offAng) + (setAdn)); 
          for (angle = 230; angle <= (setAng); angle += 1) {
          HCPCA9685_1.Servo(3, angle);
          // HCPCA9685_2.Servo(3, angle);
          delay(bdel);
          }
          for (angle = (setAng); angle >= (offAng); angle -= 1) {
          sig[(sigNo)].write(angle);
          delay(bdel);  
          }
          sigStat[(sigNo)] = 0; // sigstat variable needs to be changed for each action

Define the objects this way:

HCPCA9685 controller[2] = {HCPCA9685(I2CAdd_1), HCPCA9685(I2CAdd_2)};

later you can use the controller array as usual.

Thank you Pylon - that's exactly what I was looking for!

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