I2C sending an array

Hi guys I am trying to get my master writer to send an array of 8 integers to the slave reader.

I am using the following

Wire.beginTransmission(4);
Wire.write(angFeed[8]);
Wire.endTransmission();

I cannot get the slave reader to read this value however. I tried to modify the example sketch for slave reader but it did not help.

I need the slave reader to receive these values and print them, can anyone explain how this is done please?

Thanks.

The Wire lib derives from the Print library, you can use:

Wire.write( angFeed, 8 );

This implies angFeed is an array of bytes/char if not (is larger like int/long), you'll have to multiply the number of elements to send by the size of one element.

pYro_65:
The Wire lib derives from the Print library, you can use:

Wire.write( angFeed, 8 );

This implies angFeed is an array of bytes/char if not (is larger like int/long), you'll have to multiply the number of elements to send by the size of one element.

http://www.arduino.cc/en/Reference/WireWrite

When I used that instead of the one I wrote I get an error while compiling.

RobertAgius:
When I used that instead of the one I wrote I get an error while compiling.

I didn't.

I managed to get it to work by sending the values individually.

Wire.write(angFeed[0]);
Wire.write(angFeed[1]);
Wire.write(angFeed[2]);

etc.

I did the same thing on the receiver end

incomingByte[0] = Wire.read();
incomingByte[1] = Wire.read();
incomingByte[2] = Wire.read();

I'm not sure why it worked this way but it did. Thanks for your help.

Try this sketch:

#include <Wire.h>

void setup() {
  char angFeed[8] ="A test!";
  Wire.write( angFeed, 8 );
}

void loop(){}

Does it compile?

RobertAgius:
When I used that instead of the one I wrote I get an error while compiling.

But the error message is a secret, right?

Sorry for the delay. This was the error I received.

All I did was change

 Wire.write(angFeed[8]);

to

 Wire.write(angFeed, 8);

in my master sender program

Arduino: 1.6.3 (Windows 8.1), Board: "Arduino Mega or Mega 2560, ATmega2560 (Mega 2560)"

Using library Wire in folder: C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\Wire 

Using library Servo in folder: C:\Program Files (x86)\Arduino\libraries\Servo 



C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avr-g++ -c -g -Os -w -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10603 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR -IC:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino -IC:\Program Files (x86)\Arduino\hardware\arduino\avr\variants\mega -IC:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\Wire -IC:\Program Files (x86)\Arduino\libraries\Servo\src C:\Users\Robert\AppData\Local\Temp\build161324557329061475.tmp\Master_Writer.cpp -o C:\Users\Robert\AppData\Local\Temp\build161324557329061475.tmp\Master_Writer.cpp.o 

Master_Writer.ino: In function 'void loop()':

Master_Writer.ino:195:24: error: no matching function for call to 'TwoWire::write(int [8], int)'

Master_Writer.ino:195:24: note: candidates are:

In file included from Master_Writer.ino:8:0:

C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\Wire/Wire.h:61:20: note: virtual size_t TwoWire::write(uint8_t)

   virtual size_t write(uint8_t);

                  ^

C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\Wire/Wire.h:61:20: note:   candidate expects 1 argument, 2 provided

C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\Wire/Wire.h:62:20: note: virtual size_t TwoWire::write(const uint8_t*, size_t)

   virtual size_t write(const uint8_t *, size_t);

                  ^

C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\Wire/Wire.h:62:20: note:   no known conversion for argument 1 from 'int [8]' to 'const uint8_t* {aka const unsigned char*}'

C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\Wire/Wire.h:70:19: note: size_t TwoWire::write(long unsigned int)

   inline size_t write(unsigned long n) { return write((uint8_t)n); }

                 ^

C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\Wire/Wire.h:70:19: note:   candidate expects 1 argument, 2 provided

C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\Wire/Wire.h:71:19: note: size_t TwoWire::write(long int)

   inline size_t write(long n) { return write((uint8_t)n); }

                 ^

C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\Wire/Wire.h:71:19: note:   candidate expects 1 argument, 2 provided

C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\Wire/Wire.h:72:19: note: size_t TwoWire::write(unsigned int)

   inline size_t write(unsigned int n) { return write((uint8_t)n); }

                 ^

C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\Wire/Wire.h:72:19: note:   candidate expects 1 argument, 2 provided

C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\Wire/Wire.h:73:19: note: size_t TwoWire::write(int)

   inline size_t write(int n) { return write((uint8_t)n); }

                 ^

C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\Wire/Wire.h:73:19: note:   candidate expects 1 argument, 2 provided

In file included from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Stream.h:26:0,

               from C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\Wire/Wire.h:26,

               from Master_Writer.ino:8:

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:54:12: note: size_t Print::write(const char*, size_t)

   size_t write(const char *buffer, size_t size) {

          ^

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:54:12: note:   no known conversion for argument 1 from 'int [8]' to 'const char*'

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:49:12: note: size_t Print::write(const char*)

   size_t write(const char *str) {

          ^

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:49:12: note:   candidate expects 1 argument, 2 provided

Error compiling.

Moderator edit: [code] ... [/code] tags added. (Nick Gammon)

OK, cast it to const uint8_t *.

I'm sorry but I do not know what that means. Can you elaborate please?

Wire.write((const uint8_t *)angFeed, 8);

septillion:
Wire.write((const uint8_t *)angFeed, 8);

Thank you very much, it compiled without errors.

I still need help on the slave end of the program however. I edited the example sketch for the slave reader to the following

#include <Wire.h>
 int angFeed[8]; 
 
void setup()
{
  Wire.begin(4);                // join i2c bus with address #4
  Wire.onReceive(receiveEvent);
  Serial.begin(9600);           // start serial for output
}

void loop()
{
  delay(100);
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
  while (1 < Wire.available()) // loop through all but the last
  {
  angFeed[8] = Wire.read();
  Serial.print(angFeed[0]);
  Serial.print(",");
  Serial.print(angFeed[1]);
  Serial.print(",");
  Serial.print(angFeed[2]);
  Serial.print(",");
  Serial.print(angFeed[3]);
  Serial.print(",");
  Serial.print(angFeed[4]);
  Serial.print(",");
  Serial.print(angFeed[5]);
  Serial.print(",");
  Serial.print(angFeed[6]);
  Serial.print(",");
  Serial.println(angFeed[7]);  
}
  
}

It compiles just fine but prints out only zeros. I'm suspecting that angFeed[8] = Wire.read(); isn't correct.

You cannot simple write the hole array at once. Also, Wire.read() just gives you the next available byte, to all received bytes. So just loop over them:

#include <Wire.h>
 int angFeed[8];
 
void setup()
{
  Wire.begin(4);                // join i2c bus with address #4
  Wire.onReceive(receiveEvent);
  Serial.begin(9600);           // start serial for output
}

void loop()
{
  delay(100);
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany){
  for(howMany; howMany > 0; howMany--){
    angFeed[howMany - 1] = Wire.read();
    Serial.print(angFeed[howMany - 1]);
    if(howMany != 1){
      Serial.print(",");
    }
    else{
      Serial.println();
    }
  }
}

septillion:
You cannot simple write the hole array at once. Also, Wire.read() just gives you the next available byte, to all received bytes. So just loop over them:

#include <Wire.h>

int angFeed[8];

void setup()
{
  Wire.begin(4);                // join i2c bus with address #4
  Wire.onReceive(receiveEvent);
  Serial.begin(9600);          // start serial for output
}

void loop()
{
  delay(100);
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany){
  for(howMany; howMany > 0; howMany--){
    angFeed[howMany - 1] = Wire.read();
    Serial.print(angFeed[howMany - 1]);
    if(howMany != 1){
      Serial.print(",");
    }
    else{
      Serial.println();
    }
  }
}

This worked like a charm! Thanks you're a life saver! :smiley: