Sending large values using Wire.send()

I'm trying to send values > 255 using Wire.send() is there a way to do it? Or do I need to break the numbers up into individual units and send the one at a time? If so then is there an easy way to put a large number into an array to make the sending process easier?

You can do stuff like:

int aValue = 1234; // bigger than 225

byte* pInt = (byte*) &aValue;

Now, "pInt" is a pointer to the contents of "aValue", so Wire.send (pInt, sizeof (aValue)); will transmit however many bytes are in aValue. http://arduino.cc/en/Reference/WireSend

In your example it only seems to print out 210. Which is the same thing I get if I just tell it to print out aValue 1234/256=4.8203125 .8203125*256=210

1234 =

00000100 11010010

The last byte is =

11010010

Which is equal to 210

Oh, so does that mean the Serial.print I'm using is only printing out the last byte of aValue? is there a way I can tell it to print out all of the bytes at once. I just want to be able to reliably send an integer from one arduino to another using the Wire.send()

I've messed around with this a little more, but I haven't been making that much progress. here is the code:

int user=1234;
byte* pInt=(byte*) &user;
  Wire.beginTransmission(2);
    Wire.send(pInt,sizeof(user));
  Wire.endTransmission();

And the receiver code is:

void test(int count){
while(Wire.available()){
 int x=Wire.receive();
  Serial.println(x);
}
}

The serial output is 57 48 57 48

What are these numbers representing? how can i get the receiver to get 1234 as well?

What does this print:

int user=1234;
byte* pInt=(byte*) &user;

Serial.println(pInt[0],DEC);
Serial.println(pInt[1],DEC);

That code prints out:

210 4

That code prints out:

210 4

Which is correct.

Maybe Wiring does something to the data?

I'm not using Wiring in this basic example all of this is happening in the main loop, I'm not calling any wiring functions on this.

void loop(){
  int user=1234;
   byte* pInt=(byte*) &user;
   Serial.println(pInt[0],DEC);
   Serial.println(pInt[1],DEC);
   /*
  Wire.beginTransmission(2);
    Wire.send(pInt,sizeof(user));
  Wire.endTransmission();
  */
  delay(5000);
}

I'm not using Wiring in this basic example all of this is happening in the main loop, I'm not calling any wiring functions on this.

My point exactly. The data you recieve with this sketch, is correct. The data you recieve using Wire is wrong. Although operating on the same data in a seemingly identical way.

I do not know what causes the odd numbers. :(

I get the same response when I send the data using Wire.send() what I want to know is how I can format that back to 1234 on the receiver? Is that possible or is that were the issue lies?

byte first = 210;
byte second = 4;
int result = second<<8 | first;
Serial.println(result);

Prints 1234, I would think.

I’ve gotten it to work properly however my code is a little limited and I would like to make it a little more universal so it’s not as limited with what the data it can read. Here is the code

void test(int sizeIn){
  Serial.println(sizeIn);
  int buffer[sizeIn];
while(Wire.available()){
 buffer[inputCount]=Wire.receive();
 inputCount++;
  if(inputCount==sizeIn){
   int result = buffer[1]<<8 | buffer[0];
   Serial.println(result);
   inputCount=0;
}}}

The issues I’m running into is that if the number is too large then the array will be larger and I don’t know how to format it properly so that it will arrange the numbers properly when read, maybe if I use the Select case with the sizeIn parameter and then create a few possibilities of how to print the data out. Is there a better way to do this?

I can give you a hint for how to accept more bytes.
Going above 4 will be scary since the arduino does not support datatyes above 4 bytes.

But:

int result = buffer[1]<<81 | buffer[0]<<80;

See the pattern? :slight_smile:

Going above 4 will be scary since the arduino does not support datatyes above 4 bytes.

I'm guessing that the number 2828601 is above 4 bytes? because I can't get it to send properly it will send all but 4 bits of data this is what I get: 2828601=1010110010100100111001 it will get: 101011 101001 111001

I don't get why its those bits in between. for the code all i'm having it do is print out the buffer array each cell at a time. any ideas, is this even possible to send?

There is "long long" introduced in C99 that is required to be at least 64 bits wide.

Serial.println(sizeof(long long)); seems to confirm it's availability in arduino.

Is "long long" type really avalible in the Arduino system? It's not listed in the Arduino reference data types.

Lefty

Certainly seems like it.

void setup()
{
    Serial.begin(9600);
    delay(100);
}

void loop()
{
    long long x;
    Serial.println(sizeof x);
}

I cant figure this out. I have to be able to send a value as large as 9999999 but the formating keeps changing and I don’t know what to do from here. This is my sending code:

#include <Wire.h>
#define buzz 11
#define master 1



void setup(){
  Serial.begin(9600);
  Wire.begin(1);
  Wire.onReceive(inp);
 // sendData();
}


void loop(){
 long long user=4638671;
   byte* pInt=(byte*) &user;
  
   Wire.beginTransmission(2);
   Wire.send(pInt,sizeof(user));
   Wire.endTransmission();
 /*
  Wire.beginTransmission(2);
    Wire.send(pInt,sizeof(user));
  Wire.endTransmission();
  */
  delay(5000);
}

void inp(int count){
  int x = Wire.receive();
  Serial.println(x);
  
}

And this is my receiver code:

#include <Wire.h>
#define address 2
#define green 13

int inputCount=0;
long long result=0;
//int x=0;
void setup(){
  Serial.begin(9600);
  Serial.println("ready");
  Wire.begin(2);
  Wire.onReceive(test);
  pinMode(green,OUTPUT);
}
  
void loop(){
}

void test(int sizeIn){
  Serial.println(sizeIn);
  int buffer[sizeIn];
while(Wire.available()){
 buffer[inputCount]=Wire.receive();
 inputCount++;
  //Serial.println(buffer[inputCount]);
}
  switch(inputCount){
    case 1:
    result = buffer[0];
    break;
    case 2:
    result = buffer[1]<<8*1|buffer[0]<<8*0;
    break;
    case 3:
    result =buffer[2]<<8*2|buffer[1]<<8*1|buffer[0]<<8*0;
    break;
    case 8:
    result= buffer[2]<<16 | buffer[1]<<8 | buffer[0];
    Serial.println(result,BIN);
    //Serial.println(result);
    Serial.println(buffer[0],BIN);
    Serial.println(buffer[1],BIN);
    Serial.println(buffer[2],BIN);
    Serial.println(buffer[3],BIN);
    break;
  }
   //Serial.println(result);
   inputCount=0;
   result=0;

}

I succeeded in getting it to send numbers of the size #### but this is getting more and more complicated and i’m getting more and more confused. any ideas on how to make this simpler? can it be simpler? or does it need to be more complicated?