I am trying to write the code for my arduino mega to control 31 servos with 2 pca9685 via I2C but am stumped as to how I connect my switchpins on the mega to the individual servos on the pca9685 boards.
Not everybody will know what a PCA9685 is, and since it's actually a chip not a board, you should provide a link to the actual board you are using.
Sorry about that it is a PCA9685 16 channel 12 bit PWM servo motor driver I2C module for Arduino. There are lots of pictures of them on E bay but I don't know how to show the link
A Mega has the I2C pins in a different place (pin 20 and 21).
So you can't use a PCA9685 shield with a Mega without modding it.
You can use a PCA9685 breakout board (with wires).
The servos and servo supply connect to the breakout board.
The Mega's 5volt, ground and the two I2C lines connect to the first servo board.
Second board (with changed I2C address) connects to the first servo board.
Post a picture of what you have, and how you have connected it.
Leo..
Edit: This page has a link to a tutorial.
This is what I have written so far
[/#include <Adafruit_PWMServoDriver.h>
Adafruit_PWMServoDriver pwm1 = Adafruit_PWMServoDriver(0x40);
Adafruit_PWMServoDriver pwm2 = Adafruit_PWMServoDriver(0x41);
#include <Servo.h>
#include <Wire.h>
const int NumServos = 31;
const int NumSwitches = 31;
Servo ServoArray[NumServos];
int ServoLowEndPoints[NumServos] = {75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75,75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75,75, 75, 75};
int ServoHighEndPoints[NumServos] = { 115, 115, 115, 115, 115, 115, 115, 115, 115,115, 115, 115, 115,115, 115, 115, 115, 115, 115, 115, 115, 115, 115,115, 115, 115, 115, 115, 115, 115, 115};
int SwitchPins[NumSwitches] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32};
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pwm1.begin();
pwm1.setPWMFreq(60);
pwm2.begin();
pwm2.setPWMFreq(60);
Serial.write (0);
for (int i = 0; i < NumSwitches; i++)
pinMode(SwitchPins[i], INPUT_PULLUP);
for (int i = 0; i < NumServos; i++)
ServoArray[i].attach(ServoPins[i]);
}
void loop() {
// put your main code here, to run repeatedly:
for (int i = 0; i < NumServos; i++)
{
if (digitalRead(SwitchPins[i]))
ServoArray[i].write(ServoLowEndPoints[i]);
else
ServoArray[i].write(ServoHighEndPoints[i]);
}
code]