adrawo
1
My friend and i are trying to send a "unsigned long long" via "Wire.write();".
We wonder if there is a solution to this. Here's the code:
We have the arrays in another part of the code.
#define SLAVEADDR 8
void sendArray(){
unsigned long long data[4];
for(int x = 0; x < 5; x++){
for(int i = 0; i < 20; i++){
data[x] = data[x] + array[array2[i + (x * 20)]];
data[x] << 3;
}
}
Wire.beginTransmission(SLAVEADDR);
Wire.write(data[0]);
Wire.write(data[1]);
Wire.write(data[2]);
Wire.write(data[3]);
Wire.write(data[4]);
Wire.endTransmission();
}
If theres a better solution to this you are free to reply with that.
Thanks in beforehand! 
adrawo:
My friend and i are trying to send a "unsigned long long" via "Wire.write();".
We wonder if there is a solution to this. Here's the code:
#define SLAVEADDR 8
void sendArray(){
unsigned long long data[4];
for(int x = 0; x < 5; x++){
for(int i = 0; i < 20; i++){
data[x] = data[x] + matrix[checkmatrix[i + (x * 20)]];
data[x] << 3;
}
}
Wire.beginTransmission(SLAVEADDR);
Wire.write(data[0]);
Wire.write(data[1]);
Wire.write(data[2]);
Wire.write(data[3]);
Wire.write(data[4]);
Wire.endTransmission();
}
If theres a better solution to this you are free to reply with that.
Thanks in beforehand! :)
First of all, your array has 4 elements numbered 0 through 3. The first loop should be x=0; x<4, not x<5.
I have no idea what "checkmatrix" is since you didn't post it.
Lastly, why not use a union?
union {
uint64_t num;
uint8_t parts[8];
} u;
u.num = ginormous number;
for (x = 0; x < 8; x++) {
Wire.write (u.parts[x]);
}
Or do "x = 8; while x--" to reverse the order of the bytes being sent.
To write an array with Wire.write():
Syntax
Wire.write(value)
Wire.write(string)
Wire.write(data, length)
Parameters
value: a value to send as a single byte
string: a string to send as a series of bytes
data: an array of data to send as bytes
length: the number of bytes to transmit
I would use a union, too.
data[x] << 3;I don't know what that is, but I suspect it is missing a "="
Groove:
data[x] << 3;
I don't know what that is, but I suspect it is missing a "="
I agree... but what he was doing is all wrong anyway, so it's a moot point.
groundfungus:
To write an array with Wire.write():
I would use a union, too.
You mean do a Wire.write of the single object "u.parts" with a count of 8 bytes? Would that work?
I think it would... but it would have to be passed as "&u.parts"... I think. I HATE I2C and Wire... never use them... so I can't test it here.
adrawo
7
Thanks for the answers! I did not write this code, but thanks for the help!
You mean do a Wire.write of the single object "u.parts" with a count of 8 bytes? Would that work?
This code demonstrates writing a long long using an array with Wire.write(data, length). The code has been tested on 2 Unos.
Slave sender:
#include <Wire.h>
union longlong
{
long long uLL;
byte uBytes[8];
};
longlong aLL;
void setup()
{
aLL.uLL = 98123456789;
Wire.begin(20);
Wire.onRequest(requestEvent);
}
void loop()
{
delay(1000);
}
void requestEvent()
{
Wire.write(aLL.uBytes, 8);
}
Master reader:
union longlong
{
long long uLL;
byte uBytes[8];
};
longlong aLL;
#include <Wire.h>
void setup()
{
Wire.begin();
Serial.begin(115200);
}
void loop()
{
Wire.requestFrom(20, 8);
int index = 0;
delay(5);
while (Wire.available())
{
char c = Wire.read();
aLL.uBytes[index] = c;
index++;
}
if (index != 0)
{
Serial.println();
// some trickery to print long long (which is not supported by Arduino print())
char buffer[24];
sprintf(buffer, "%0ld", aLL.uLL / 1000000L);
Serial.print(buffer);
sprintf(buffer, "%0ld", aLL.uLL % 1000000L);
Serial.println(buffer);
}
delay(2000);
}