Declare one or more data to send with the same function

I am wondering, if it is possible, or how it is possible, to declare one or more data in a function to send it over I2C at once. Normally a function takes one value and send it with Wire.endTransmission() to the slave side. That needs a little delay after endTransmission when sending more than one value. But if it is possible to write inside the function one value or also more than one value to send all together to the slave at once, would not need a delay!

For example, i send a value with the function send(1);, this will be sent with:

Wire.begin(9);
Wire.write(1);
Wire.endTransmission();

Is it now possible, to declare for example two values inside the function to send them at once like that, but also if only given one value with the same function?

send(1,34);
....

send(byte value1, byte value2){
Wire.begin(9);
Wire.write(value1);
Wire.write(value2);
Wire.endTransmission();
}

But also:

send(4);
....

send(byte value1, byte value2){
Wire.begin(9);
Wire.write(value1);
Wire.write(value2);
Wire.endTransmission();
}

Perhaps it is also possible to write the values inside the function on two lines inside the () instead of separating them with a comma?

have a look at Wire.BeginTransmission()

Wire - Arduino Reference

and if you want to send multiple values, they could be in a byte array and use write() with the size parameter

Hello,
I suggest to use array and pass argument, which tells how many elements should be transferred.
Like this:

unsigned char Ar[100];

void func1()
{
  unsigned char *PAr;
  
  PAr= Ar;
  *PAr= 1;
  PAr++;
  *PAr= 34;
  send(2, Ar);
  return;
}

void send(unsigned char N, unsigned char *PA)
{
  unsigned char i;
  /* This is probably in setup(): Wire.begin();  */
  Wire.beginTransmission(9);
  for(i= 0; i<N; i++)
  {
    Wire.write(*PA);
    PA++;
  }
  Wire.endTransmission();
  return;
}

If you like challenges, variable list may give one:

#include <stdio.h>
#include <stdarg.h>

void func1()
{
  send(2, 1, 34);
  send(1, 4);
  return;
}

void send(unsigned char NVal, ...)
{
  va_list ValList;
  unsigned char i;

/* initialize ValList for NVal number of arguments */
  va_start(ValList, NVal);

  /* This is probably in setup(): Wire.begin();  */
  Wire.beginTransmission(9);
  for (i = 0; i < NVal; i++) 
  {
    Wire.write( (unsigned char)va_arg(ValList, unsigned char) );
  }
  Wire.endTransmission();
	
/* clean memory reserved for ValList*/
  va_end(ValList);
  return;
}

I have not tested these examples! Please, check before use.

Best regards,
Gienek.

Just do Wire.write(PA, N);

How to send a letter and number combined as one data packet or when multiple of such data packets? I thought of something with struct, but that is perhaps not the right way for that.
As I see it with the list method, it takes one number after the other and write them in the i2c buffer to send. This method looks not really difficult, but does it take a lot of RAM because of the two libs?

At the moment, I have stored the letter and number with sprintf() to an array and after that I have sent it. If it is possible to declare the letter and number combination all with commas separated into a function and write these together and after that the endTransmission command to sent them away. I really do not see how these examples will work for that way!?

Have you considered that the I2C functions are blocking and that I2C is very slow compared to how fast the processor runs? By doing this you are stopping the processor doing anything else. My advice is to send as few bytes as possible each time and use the time taken to send them for something else.

The reason why I am trying to do that is, because after each data I will send, I have had to ad a delay of 12ms to send and receive all data. Other wise some data is lost because the multiple times write and endTransmission needs some time to process it.
12ms is not that much, but I think it could be faster without it, is not it?
Or is it really as fast as to send more than these two bytes at a time with a 12ms delay? I have not tried to send a few bytes (more than 6 to 10 bytes) within one endTransmission. If it does not make any differences or is slower that way, I will rest with my 12ms delay after the endTransmission to wait for all data sent and received completely. On the serial monitor it is really fast showing all written and received data packets. Also 12ms are not easy to imagine how fast that really is!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.