The reason for two arrays seems to be to keep the two PWM boards separate. How about adding a function to direct the data to the correct board? Then the two arrays can be consolidated into one:
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
#include <CMRI.h>
#define MIN_PULSE_WIDTH 95
#define MAX_PULSE_WIDTH 530
#define FREQUENCY 50
Adafruit_PWMServoDriver pwm0 = Adafruit_PWMServoDriver(0x40);
Adafruit_PWMServoDriver pwm1 = Adafruit_PWMServoDriver(0x41);
CMRI cmri(0, 96, 32); //CPNODE with address 0, 96 inputs, 32 outputs
struct turnoutData
{
byte Tbit; //0 or 1 from external program
int targetPos; //based on Tbit, set to either pos1 or pos2
int pos1; //servo position 1
int pos2; //servo position 2
int offset; //horn offset from 90 degree position added to targetPos before sending to servo
byte relayPin;
byte relayState; //set to either relayState1 or relayState2 based on Tbit value
byte relayState1; //servo's corresponding relays position 1 state
byte relayState2; //servo's corresponding relays position 2 state
int currentPos; //used to use slow motion to move servo from position to position
};
struct turnoutData turnArray[] =
{
// First PWM board
{ 0, 312, 249, 375, -40, 35, 0, 1, 0, 312}, //312 is 90 degrees, 249 is 70 degrees, 375 is 110 degrees
{ 0, 312, 249, 375, 20, 67, 0, 1, 0, 312},
{ 0, 312, 375, 249, -9, 30, 1, 0, 1, 312},
{ 0, 312, 249, 375, 12, 68, 0, 0, 1, 312},
{ 0, 312, 249, 375, 0, 32, 0, 1, 0, 312},
{ 0, 312, 249, 375, 14, 33, 0, 1, 0, 312},
{ 0, 312, 312, 312, 0, 70, 0, 0, 0, 312}, //Future use
{ 0, 312, 312, 312, 0, 70, 0, 0, 0, 312}, //Future use
{ 0, 312, 312, 312, 0, 70, 0, 0, 0, 312}, //Future use
{ 0, 312, 375, 249, 2, 69, 0, 1, 0, 312},
{ 0, 312, 375, 249, 6, 64, 1, 0, 1, 312},
{ 0, 312, 375, 249, 9, 66, 1, 0, 1, 312},
{ 0, 312, 375, 249, -31, 65, 0, 1, 0, 312},
{ 0, 312, 375, 249, 18, 63, 1, 0, 1, 312},
{ 0, 312, 375, 249, -9, 62, 0, 1, 0, 312},
{ 0, 312, 375, 249, 0, 31, 0, 1, 0, 312},
// Second PWM board
{ 0, 312, 249, 375, -19, 23, 1, 0, 1, 312},
{ 0, 312, 249, 375, 6, 25, 0, 1, 0, 312},
{ 0, 312, 249, 375, -23, 29, 1, 0, 1, 312},
{ 0, 312, 249, 375, -16, 22, 0, 1, 0, 312},
{ 0, 312, 249, 375, -6, 28, 0, 1, 0, 312},
{ 0, 312, 249, 375, 0, 24, 1, 0, 1, 312},
{ 0, 312, 375, 249, 0, 27, 0, 1, 0, 312},
{ 0, 312, 249, 375, -1, 26, 1, 0, 1, 312},
{ 0, 312, 375, 249, -9, 34, 0, 1, 0, 312},
{ 0, 312, 312, 312, 0, 70, 0, 0, 0, 312}, //Future use
{ 0, 312, 312, 312, 0, 70, 0, 0, 0, 312}, //Future use
{ 0, 312, 312, 312, 0, 70, 0, 0, 0, 312}, //Future use
{ 0, 312, 312, 312, 0, 70, 0, 0, 0, 312}, //Future use
{ 0, 312, 312, 312, 0, 70, 0, 0, 0, 312}, //Future use
{ 0, 312, 312, 312, 0, 70, 0, 0, 0, 312}, //Future use
{ 0, 312, 312, 312, 0, 70, 0, 0, 0, 312}, //Future use
};
void SetPWM(int channel)
{
if (channel < 16)
pwm0.setPWM(channel, 0, turnArray[channel].currentPos + turnArray[channel].offset);
else
pwm1.setPWM(channel - 16, 0, turnArray[channel].currentPos + turnArray[channel].offset);
}
void processTurnouts()
{
for (int i = 0; i < 32; i++)
{
if (turnArray[i].Tbit == 1)
(turnArray[i].targetPos = turnArray[i].pos1);
else
(turnArray[i].targetPos = turnArray[i].pos2);
if (turnArray[i].targetPos != turnArray[i].currentPos)
{
if (turnArray[i].targetPos > turnArray[i].currentPos)
{
turnArray[i].currentPos += 2;
if (turnArray[i].currentPos >= turnArray[i].targetPos)
{
turnArray[i].currentPos = turnArray[i].targetPos;
}
SetPWM(i);
}
else
{
turnArray[i].currentPos -= 2;
if (turnArray[i].currentPos <= turnArray[i].targetPos)
{
turnArray[i].currentPos = turnArray[i].targetPos;
}
SetPWM(i);
}
}
if (turnArray[i].currentPos == turnArray[i].pos1)
turnArray[i].relayState = turnArray[i].relayState1;
else
turnArray[i].relayState = turnArray[i].relayState2;
}
}
void setup()
{
Serial.begin(57600);
Serial.println("JMRI CMRI Arduino Mega Interface");
Wire.setClock(400000);
pwm0.begin();
pwm0.setPWMFreq(FREQUENCY);
pwm1.begin();
pwm1.setPWMFreq(FREQUENCY);
for (byte i = 0; i < 32; i++)
{
pinMode(turnArray[i].relayPin, OUTPUT);
digitalWrite(turnArray[i].relayPin, turnArray[i].relayState);
}
Serial.println("Init completed.");
}
void loop()
{
cmri.process();
for (byte i = 0; i < 32; i++)
{
turnArray[i].Tbit = cmri.get_bit(i); //store bit from external computer
}
processTurnouts();
for (byte i = 0; i < 32; i++)
{
cmri.set_bit(i, turnArray[i].relayState ); //send relayStates back to computer
digitalWrite(turnArray[i].relayPin, turnArray[i].relayState);
}
}