Arrays trough I2C

Hi,
I have two Arduino boards connected by I2C. I connected some switches to the master board and some LED’s to the slave board. The LED just have to follow it’s switch (on/off). When I start using arrays everything goes wrong. It seems I can’t get the array into the I2C. Can someone show me the right direction? I’m comfortable with arrays and had some successful tests with I2C.

This is all i have... :blush:

Master:

#include <Wire.h>
int SwitchArray[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};  

void setup()
{
  Wire.begin();
  int i;
   
  for (i=0;i<15;i++){
    pinMode(SwitchArray[i], INPUT); 
  }
}

slave:

#include <Wire.h>

int LEDArray[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};

void setup()
{
  Wire.begin(9);               
  Wire.onReceive(receiveEvent); 
  
  int i;
   
  for (i=0;i<15;i++){
    pinMode(LEDArray[i], OUTPUT);
    digitalWrite(LEDArray[i], LOW);
  }

Thanks,
Chris

Where's the code which sends the data over i2c to the slave?

There was no rest :slight_smile: nothing really came out. I have this now but I think there’s a problem with the HIGH and LOW on the Byte-array…
Master:

void loop()
{
  Wire.beginTransmission(9);
  
  int i;
  
  for (i=0;i<16;i++){
  Wire.write(i2cArray[i]);
  }
  
  Wire.endTransmission();
  
  for (i=0;i<16;i++){
    if (digitalRead(SwitchArray[i]) == HIGH){
      digitalWrite(i2cArray[i],HIGH);
      
    }
    else {
      digitalWrite(i2cArray[i],LOW);
      
    }   
  }
}

Slave:

void loop() {
  int i;
  
  for (i=0;i<16;i++){
    if (digitalRead(i2cArray[i]) == HIGH){
      digitalWrite(LEDArray[i],HIGH);
      
    }
    else {
      digitalWrite(LEDArray[i],LOW);
     
    }
  }
}

void receiveEvent(int OnOrOff) {
  int i;
  
  for (i=0;i<16;i++){
  i2cArray[i] = Wire.read();
  }
}

Thanks,
Chris

Bump

I can see only partial pieces of code but there are still things missing

  • i2cArray ? where is it declared?

furthermore in C array's start with 0, just as the pin numbers on Arduino

don't know what board you are using but pin1 is from the rs232 so ti migth cause some trouble

There’s probably a lot of code missing, but that’s why I ‘m consulting the forum.

Basically this is what I’m trying to achieve. I have allot of switches (more than 15 in the example) on one arduino board (Mega 2560) and as many LED’s on another board (also Mega 2560).

I wanted to setup an array to read in the switches, put that on i2c to send it to the other board and write that to the LED’s by using another array in the second board.

Imgur

I don’t think it’s that difficult, but somehow I just can’t find the right code combination… :~

Cheers,
Chris