I had the function convertData tried in differet ways.
For example:
void convertData(int r){};
void convertData(int[] r){}; // when I tried to send an arrays
void convert ((uint8_t *) r)
void convertData((uint8_t *)&str1)
Nothing works for me.
How do i declare the Variable in the receiveFunction? For me, it works better to send an Arrays with data. like so:
void send(){
int[] i = {int val1, int val2}; // as i understood 4 bytes so i write:
Wire.beginTransmission(ledChannel);
Wire.write(i, 4);
Wire.endTransmission();
}
Paul's correct; the argument to whatever function you use is set in stone and must be an int. Perhaps something like:
struct { // 2 16-bit integers, 4 bytes total MAKE GLOBAL
int val1 = 5;
int val2 = 10;
} str1;
// Setup(), loop(), etc...
void convertData(int bytesToRead){
int i = 0;
union myUnion {
int A;
int B;
char buffer[4];
} mine;
while(Wire.available())
{
mine.buffer[i++] = Wire.read();
}
str1.val1 = mine.A; // You may have to play around with these cuz of Endian problem
str1.val2 = mine.B;
}
I cannot test this, so it may be completely wrong...
I realize this is an old post, but I just stumbled on it, and it's taking care of exactly what I need.
Here's a really stupid question:
If your array only has 2 components (sendInts[0] and sendInts[1]), why does aCount have a value of 4? I'm creating an array of 200 to be transmitted (a bunch of DMX addresses). Each components would be an int between 0 and 256 - what would my aCount be in this case?
If your array only has 2 components (sendInts[0] and sendInts[1]), why does aCount have a value of 4?
aCount is received bytes, and there are two per integer.
I'm creating an array of 200 to be transmitted (a bunch of DMX addresses). Each components would be an int between 0 and 256 - what would my aCount be in this case?
A byte has value of 0 to 255 inclusive, so I think you are talking about a byte array. Memory is precious on an Arduino, so don't use an int when a byte will do.
You will encounter another problem in that the Wire library for i2c communications is designed for short communications and there is a 32 byte buffer limitation. You will have to break your array into pieces.