Wire.h write data

hi!

Just start to play with I2C communication and stuck with data transfer. Can slave send data like this?
SLAVE

void requestEvent() {
  for (int i_1 = 0; i_1 < 16; i_1 ++) {
    a[i_1] = readMux_1(i_1);
  }
  Wire.write(a[]);
}

Can Wire.write send all array?
If YES how MASTER should read this array?

it needs to know the number of bytes in the array

Wire.write (a, 16);

look are Wire.h to see what functions are available

I find this: Arduino - WireWrite
It is same as you say Wire.write(data, length)
In my case Wire.write(a, 16)
But I get error: no matching function for call to 'TwoWire::write(int [16], int)'
Using array is my weak spot...

the argument to write is a uint8_t (i.e char, byte). your arrays are ints (maybe they can be bytes) so you need to provide a pointer to a byte. sizeof() returns the size of a variable in bytes.

Wire.write ((uint8_t*)a, sizeof(a));

ok, then I declare a as byte: byte a[16];
I get no more error using this line: Wire.write(a, 16);
So right now I told to send byte a and all its data?

In MASTER code I use code like this:

Wire.requestFrom(1, 16);
  while (Wire.available()) {
    byte c = Wire.read();
    Serial.println(c);
  }

and I get 16 different meanings, but it is not the same as SLAVE send me

ammygo:
If YES how MASTER should read this array?

1. Connect UNO (Master) and NANO (Slave) as per Fig-1 using I2C Bus.


Figure-1:

2. Upload the following sketch in NANO (yours one slightly modified).

#include<Wire.h>
#define slaveAddress 0x08  //0001000 (7-bit)
byte readMux_1[2] = {0x12, 0x34};
byte arrayA[2];

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

void loop()
{

}

void requestEvent()
{
  for (int i_1 = 0; i_1 < 2; i_1 ++)
  {
    arrayA[i_1] = readMux_1[i_1];
  }
  Wire.write(arrayA, sizeof(arrayA));
}

3. Upload the following sketch in UNO.

#include <Wire.h>
#define slaveAddress 0x08
byte arrayA[2];

void setup() 
{
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
}

void loop() 
{
  Wire.requestFrom(slaveAddress, 2);
  arrayA[0] = Wire.read();
  arrayA[1] = Wire.read();

  Serial.println(arrayA[0], HEX);   //
  Serial.println(arrayA[1], HEX);  //
  Serial.println("=======================");

  delay(2500);
}

4. Check that 12 and 34 appear on Serial Monitor of Master at 2.5 sec interval. The data is being sent by Slave as per request of Master.
smk1.png

5. You may read the attached file for more knowledge on I2C Bus Operation and Programming.

smk1.png

Ch-6(I2C)doc.doc (1.43 MB)

and I get 16 different meanings, but it is not the same as SLAVE send me

what do you mean by "meanings"?

you might try specifying that you want the value displayed in HEX (or DEC). it may display the byte as an ASCII character

    Serial.println(c, HEX);

now everything is fine, I get all correct data.

SLAVE:

void requestEvent() {
  for (int i_1 = 0; i_1 < 16; i_1 ++) {
    a[i_1] = readMux_1(i_1);
  }
  Wire.write(a, 16);
}

MASTER:

Wire.requestFrom(1, 16); 
  while (Wire.available()) {
    byte c = Wire.read();
    Serial.println(c);

Thanks for help gcjr and GolamMostafa

ammygo:
now everything is fine, I get all correct data.

Please, post complete sketches for both Master and Slave.

Wire.requestFrom(1, 16);
  while (Wire.available()) {
    byte c = Wire.read();
    Serial.println(c);

You are receiving 16 bytes data from Slave; but, you saving saving only the last byte as is known from the above snippet. That.s why I am asking for the full sketch to see what you are doing with the received data.

void requestEvent() {
  for (int i_1 = 0; i_1 < 16; i_1 ++) {
    a[i_1] = readMux_1(i_1);
  }
  Wire.write(a, 16);
}

In the above snippet, you are saving data in an array returned by a function. We would like to see the function definition.

it’s only in testing mode but it works
SLAVE

#include <Wire.h>

//Mux control pins
int s0_1 = 8;
int s1_1 = 9;
int s2_1 = 10;
int s3_1 = 11;

int s0_2 = 8;
int s1_2 = 9;
int s2_2 = 10;
int s3_2 = 11;

//Mux in "SIG" pin
int SIG_pin_1 = 0;
int SIG_pin_2 = 1;

byte a[16];

void setup() {
  Wire.begin(1);                /* join i2c bus with address 8 */
  //Wire.onReceive(receiveEvent); /* register receive event */
  Wire.onRequest(requestEvent); /* register request event */
  Serial.begin(9600);           /* start serial comm. */
  Serial.println("-= Slave =-");

  pinMode(s0_1, OUTPUT);
  pinMode(s1_1, OUTPUT);
  pinMode(s2_1, OUTPUT);
  pinMode(s3_1, OUTPUT);

  pinMode(s0_2, OUTPUT);
  pinMode(s1_2, OUTPUT);
  pinMode(s2_2, OUTPUT);
  pinMode(s3_2, OUTPUT);

  digitalWrite(s0_1, LOW);
  digitalWrite(s1_1, LOW);
  digitalWrite(s2_1, LOW);
  digitalWrite(s3_1, LOW);

  digitalWrite(s0_2, LOW);
  digitalWrite(s1_2, LOW);
  digitalWrite(s2_2, LOW);
  digitalWrite(s3_2, LOW);
}


void loop() {

  //Loop through and read all 16 values
  //Reports back Value at channel 6 is: 346
  //for (int i_1 = 0; i_1 < 16; i_1 ++) {
  //a[i_1] = readMux_1(i_1);
  //delay(10);
  //}

  //for (int i_2 = 0; i_2 < 16; i_2 ++) {
  //Serial.println(readMux_2(i_2));
  //delay(10);
  //}

}


int readMux_1(int channel_1) {
  int controlPin_1[] = {s0_1, s1_1, s2_1, s3_1};

  int muxChannel_1[16][4] = {
    {0, 0, 0, 0}, //channel 0
    {1, 0, 0, 0}, //channel 1
    {0, 1, 0, 0}, //channel 2
    {1, 1, 0, 0}, //channel 3
    {0, 0, 1, 0}, //channel 4
    {1, 0, 1, 0}, //channel 5
    {0, 1, 1, 0}, //channel 6
    {1, 1, 1, 0}, //channel 7
    {0, 0, 0, 1}, //channel 8
    {1, 0, 0, 1}, //channel 9
    {0, 1, 0, 1}, //channel 10
    {1, 1, 0, 1}, //channel 11
    {0, 0, 1, 1}, //channel 12
    {1, 0, 1, 1}, //channel 13
    {0, 1, 1, 1}, //channel 14
    {1, 1, 1, 1} //channel 15
  };

  //loop through the 4 sig
  for (int i_1 = 0; i_1 < 2; i_1 ++) {
    digitalWrite(controlPin_1[i_1], muxChannel_1[channel_1][i_1]);
  }

  //read the value at the SIG pin
  int val_1 = analogRead(SIG_pin_1);

  //return the value
  return val_1;
}


int readMux_2(int channel_2) {
  int controlPin_2[] = {s0_2, s1_2, s2_2, s3_2};

  int muxChannel_2[16][4] = {
    {0, 0, 0, 0}, //channel 0
    {1, 0, 0, 0}, //channel 1
    {0, 1, 0, 0}, //channel 2
    {1, 1, 0, 0}, //channel 3
    {0, 0, 1, 0}, //channel 4
    {1, 0, 1, 0}, //channel 5
    {0, 1, 1, 0}, //channel 6
    {1, 1, 1, 0}, //channel 7
    {0, 0, 0, 1}, //channel 8
    {1, 0, 0, 1}, //channel 9
    {0, 1, 0, 1}, //channel 10
    {1, 1, 0, 1}, //channel 11
    {0, 0, 1, 1}, //channel 12
    {1, 0, 1, 1}, //channel 13
    {0, 1, 1, 1}, //channel 14
    {1, 1, 1, 1}  //channel 15
  };

  //loop through the 4 sig
  for (int i_2 = 0; i_2 < 2; i_2 ++) {
    digitalWrite(controlPin_2[i_2], muxChannel_2[channel_2][i_2]);
  }

  //read the value at the SIG pin
  int val_2 = analogRead(SIG_pin_2);

  //return the value
  return val_2;
}

void requestEvent() {
  for (int i_1 = 0; i_1 < 16; i_1 ++) {
    a[i_1] = readMux_1(i_1);
    Serial.print("a[");
    Serial.print(i_1);
    Serial.print("] = ");
    Serial.println(a[i_1]);
    delay(10000);
  }
  Wire.write(a, 16);  /*send string on request */
  Serial.println("---------------------");
}

MASTER

#include <Wire.h>

void setup() {
  Serial.begin(9600); /* begin serial comm. */
  Wire.begin(); /* join i2c bus as master */
  Serial.println("-= Master =-");
}

void loop() {
  Wire.beginTransmission(1); /* begin with device address 8 */
  //Wire.write("Hello Slave");  /* sends hello string */
  //Wire.endTransmission();    /* stop transmitting */

  Wire.requestFrom(1, 16); /* request & read data of size 9 from slave */
  while (Wire.available()) {
    byte c = Wire.read();/* read data received from slave */
    Serial.println(c);
  }
  Serial.println("----------------------------");
  delay(1000);
}

Thank you for posting the sketches; however, there are many anomalies between your codes and the corresponding comments. When posting works for others to read, it is necessary that we try to spend sometime to make the write up friendly, which may not be possible due to some other constrains.