Broadcasting to all Slaves

I have four LED I2C boards daisy chained. They are custom I2C boards created by the engineer at my work. They use the PCA9532 LED dimmer. Each board has 12 LED's and I have been given an Arduino Mega 2560 to use as the master.

I have written a GUI to control these LED's manually and also with preset colours and this is all working fine.

The problem I have now is connecting to each slave at the same time. Most of what I have learned regarding I2C has been from this link from Nick..

My four slaves addresses are..
0x60
0x61
0x62
0x63

Connecting to each individually works fine and also I can connect to them all using a for loop.
The code below connects and turns on all slaves. However, the LED's seem to flicker when they are on, especially at the last slave the code gets to, slave 4. I think this is a timing thing and because the code is not getting to all slaves at once. When I test the code on each slave individually there is no problem.
For example:

void transmitToSlaves(byte PWM0_REGISTER, int PWM0_DATA, byte LS1_REGISTER, int rgGlobal, byte LS2_REGISTER, int bGlobal)
{
  for(int i = 0; i < 4; i++)
  {
    //PWM0
    Wire.beginTransmission(slaveArray[i]);
    Wire.write(PWM0_REGISTER);
    Wire.write(PWM0_DATA);
    Wire.endTransmission();

    //PWM1
    Wire.beginTransmission(slaveArray[i]);
    Wire.write(PWM1_REGISTER);
    Wire.write(PWM1_DATA);
    Wire.endTransmission();

    //Red & Green & LS1
    Wire.beginTransmission(slaveArray[i]);
    Wire.write(LS1_REGISTER);
    Wire.write(rgGlobal);
    Wire.endTransmission();

    //Blue
    Wire.beginTransmission(slaveArray[i]);
    Wire.write(LS2_REGISTER);    
    Wire.write(bGlobal);
    Wire.endTransmission();

    //LS0 selector
    Wire.beginTransmission(slaveArray[i]);
    Wire.write(0x06);
    Wire.write(0x55);
    Wire.endTransmission();

    //LS3
    Wire.beginTransmission(slaveArray[i]);
    Wire.write(0x09);
    Wire.write(0x15);
    Wire.endTransmission();
  }
}

I am trying to broadcast as mentioned in the link. As broadcasting is turned off by default and I am using the PCA9532 I am unsure how or even if its possible to turn this on using this.

PCA9532 Documentation:
http://www.nxp.com/documents/data_sheet/PCA9532.pdf

So basically, I want to connect to all slaves at the same time but I don't know how to enable broadcasting on a PCA9532 using a Mega 2560 as the master.

A quick look at the datasheet and I cannot see where the chip has the ability to receive broadcast instructions.

Riva:
A quick look at the datasheet and I cannot see where the chip has the ability to receive broadcast instructions.

Yeah I don't think it is possible to do unfortunately.

I think for most things this wouldn't be a problem receiving the data one at a time but due to it being LED's it seems to interfere and cause the flickering of the lights.

Thanks for looking.

I would prefer to see all your code, for example did you speed up I2C?

http://snippets-r-us.com/

Do you need all those being/end transactions? It might work if you just send everything in one batch.

I am trying to broadcast as mentioned in the link. As broadcasting is turned off by default and I am using the PCA9532 I am unsure how or even if its possible to turn this on using this.

Did you try broadcasting? If so please post your code.

Sorry, here is my code.
I tried standard speed and also different speeds, the flickering is still there.

#include <Wire.h>

#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
int index;

int SLAVE_ADDRESS_1 = 0x63;
int slaveArray[] = {
  99,98,97,96};

//global variables to hold both RED and GREEN values
//while masking.
int rgGlobal = 85; // 01010101
int bGlobal = 85;

int PWM0_DATA;
int PWM1_DATA;
int RED_DATA;
int GREEN_DATA;
int BLUE_DATA;

byte PWM0_REGISTER = 0x03;
byte PWM1_REGISTER = 0x05;
byte LS0_REGISTER = 0x06;
byte LS1_REGISTER = 0x07;
byte LS2_REGISTER = 0x08;
byte LS3_REGISTER = 0x09;

bool PWM0_RECEIVED = false;
bool PWM1_RECEIVED = false;
bool R_RECEIVED = false;

bool G_RECEIVED = false;
bool B_RECEIVED = false;

//Function Prototypes
void commandProcessing(char *p_inputBuffer, int inputBufferLength);
void commandToSlave(int pwm0, int pwm1, int R, int G, int B);
void transmitToSlaves(byte PWM0_REGISTER, int PWM0_DATA, byte LS1_REGISTER, int rgGlobal, byte LS2_REGISTER, int bGlobal);

void setup()
{
  Serial.begin(9600);
  Wire.begin();
}

void commandProcessing(char *cmd, int sData)
{ 
  char localCMD = *cmd;

  switch(localCMD)
  {
  case '0': //PWM0
    {
      PWM0_DATA = sData;
      PWM0_RECEIVED = true;
      break;
    } 
  case '1': //PWM1
    { 
      PWM1_DATA = sData;
      PWM1_RECEIVED = true;
      break;
    }
  case 'R':
    {
      RED_DATA = sData;
      R_RECEIVED = true;

      //rgGlobal - 01 01 01 01
      if(RED_DATA == 0)
      {
        // 100% ON
        // 01000101. Could make global variable.
        rgGlobal &= ~(1 << 4); 
      }
      if(RED_DATA == 1)
      {
        // OFF
        // 01010101
        rgGlobal |= (1 << 4);
        rgGlobal &= ~(1 << 5);
      }
      if(RED_DATA == 2)
      {
        // PWMO
        // 01100101
        rgGlobal &= ~(1 << 4);
        rgGlobal |= (1 << 5);
      }
      if(RED_DATA == 3)
      {
        // PWM1
        // 01110101
        rgGlobal |= (1 << 4);
        rgGlobal |= (1 << 5);
      }
      break;
    }
  case 'G':
    {  
      GREEN_DATA = sData;
      G_RECEIVED = true;

      if(GREEN_DATA == 0)
      {
        // 100% ON
        // 00010101. Could make global variable.
        rgGlobal &= ~(1 << 6); 
      }
      if(GREEN_DATA == 1)
      {
        // OFF
        // 01010101
        rgGlobal |= (1 << 6);
        rgGlobal &= ~(1 << 7);
      }
      if(GREEN_DATA == 2)
      {
        // PWMO
        // 10010101
        rgGlobal &= ~(1 << 6);
        rgGlobal |= (1 << 7);
      }
      if(GREEN_DATA == 3)
      {
        // PWM1
        // 11010101
        rgGlobal |= (1 << 6);
        rgGlobal |= (1 << 7);
      }     
      break;
    }
  case 'B':
    {
      BLUE_DATA = sData;
      B_RECEIVED = true;

      if(BLUE_DATA == 0)
      {
        // 100% ON
        // 01010100. Could make global variable.
        bGlobal &= ~(1 << 0); 
      }
      if(BLUE_DATA == 1)
      {
        // OFF
        // 01010101
        bGlobal |= (1 << 0);
        bGlobal &= ~(1 << 1);
      }
      if(BLUE_DATA == 2)
      {
        // PWMO
        // 01010110
        bGlobal &= ~(1 << 0);
        bGlobal |= (1 << 1);
      }
      if(BLUE_DATA == 3)
      {
        // PWM1
        // 01010111
        bGlobal |= (1 << 0);
        bGlobal |= (1 << 1);
      }  
      break;
    }
  default:
    {
      Serial.println("Invalid Command");
      Serial.println();
      break;
    }
  }
  if(PWM0_RECEIVED && PWM1_RECEIVED && R_RECEIVED && G_RECEIVED && B_RECEIVED)
  {
    transmitToSlaves(PWM0_REGISTER, PWM0_DATA, LS1_REGISTER, rgGlobal, LS2_REGISTER, bGlobal);
    
    PWM0_RECEIVED = false;
    PWM1_RECEIVED = false;
    R_RECEIVED = false;
    G_RECEIVED = false;
    B_RECEIVED = false;

    rgGlobal = 85;
    bGlobal = 85;
  }
}

void loop()
{
  char OutputReport[256];

  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
      index = 0;
      inData[index] = '\0';
      started = true;
      ended = false;
    }
    else if(inChar == EOP)
    {
      ended = true;
      break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }
  // if started and ended then have received all of the data.
  if(started && ended)
  {
    //output report is -1, this will not work with index < 1;
    for(int i = 1; i <= index; i++)
    {
      OutputReport[i-1] = inData[i];
    }    
    //atoi - String to Int conversion
    int n = atoi(OutputReport);
    //Send data for processing
    commandProcessing(&inData[0], n);
    // Reset the markers and clear the buffer.
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}

/* --------------------------------
 TRANSMITTING TO SLAVE DEVICES
 
 SETTING PWM VALUES - RED(LS1[5,4]) & GREEN(LS1[7,6])
 
 00 - ON 100%
 01 - OFF
 10 - SET BY PWM0
 11 - SET BY PWM1
 
 LED PINS ON OR OFF
 
 00 = output is set (LED off; default)
 01 = output is set LOW (LED on)
 10 = output blinks at PWM0 rate
 11 = output blinks at PWM1 rate
 ----------------------------------*/
void transmitToSlaves(byte PWM0_REGISTER, int PWM0_DATA, byte LS1_REGISTER, int rgGlobal, byte LS2_REGISTER, int bGlobal)
{
  for(int i = 0; i < 4; i++)
  {
    //PWM0
    Wire.beginTransmission(slaveArray[i]); //Connects to one slave at a time. 
    Wire.write(PWM0_REGISTER);             //Trying to see if I can enable broadcasting on slaves
    Wire.write(PWM0_DATA);                 //to send code to all them at once.
    Wire.endTransmission();

    //PWM1
    Wire.beginTransmission(slaveArray[i]);
    Wire.write(PWM1_REGISTER);
    Wire.write(PWM1_DATA);
    Wire.endTransmission();

    //Red & Green & LS1
    Wire.beginTransmission(slaveArray[i]);
    Wire.write(LS1_REGISTER);
    Wire.write(rgGlobal);
    Wire.endTransmission();

    //Blue
    Wire.beginTransmission(slaveArray[i]);
    Wire.write(LS2_REGISTER);    
    Wire.write(bGlobal);
    Wire.endTransmission();

    //LS0 selector
    Wire.beginTransmission(slaveArray[i]);
    Wire.write(0x06);
    Wire.write(0x55);
    Wire.endTransmission();

    //LS3
    Wire.beginTransmission(slaveArray[i]);
    Wire.write(0x09);
    Wire.write(0x15);
    Wire.endTransmission();
  }
}

You've ignored my suggestion to get rid of the multiple begin/end transactions. Does this work?

void transmitToSlaves(byte PWM0_REGISTER, int PWM0_DATA, byte LS1_REGISTER, int rgGlobal, byte LS2_REGISTER, int bGlobal)
{
  for(int i = 0; i < 4; i++)
  {
    //PWM0
    Wire.beginTransmission(slaveArray[i]); //Connects to one slave at a time. 
    Wire.write(PWM0_REGISTER);             //Trying to see if I can enable broadcasting on slaves
    Wire.write(PWM0_DATA);                 //to send code to all them at once.
    //PWM1
    Wire.write(PWM1_REGISTER);
    Wire.write(PWM1_DATA);

    //Red & Green & LS1
    Wire.write(LS1_REGISTER);
    Wire.write(rgGlobal);

    //Blue
    Wire.write(LS2_REGISTER);    
    Wire.write(bGlobal);

    //LS0 selector
    Wire.write(0x06);
    Wire.write(0x55);

    //LS3
    Wire.write(0x09);
    Wire.write(0x15);
    Wire.endTransmission();
  }
}

I don't see you attempting broadcasting in your code.