# Send integer get 256. Arduino I2C comunication

**URL:** <https://forum.arduino.cc/t/send-integer-get-256-arduino-i2c-comunication/258120>\
**Category:** Programming\
**Created:** [September 17, 2014, 2:32am UTC](https://forum.arduino.cc/t/send-integer-get-256-arduino-i2c-comunication/258120 "2014-09-17T02:32:46Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [September 17, 2014, 2:32am UTC](https://forum.arduino.cc/t/send-integer-get-256-arduino-i2c-comunication/258120/1 "2014-09-17T02:32:46Z")

</div>

As i said, i'm sending via SCL SDA a value from an arduino to another.

The data is integer, is sent as integer, is received as integer, but it seems to be bytes.  
What i have to do to make a conversion?

MASTER  
( sample code )

```cpp
#include <Wire.h>
int val=1234; 

void setup(){
  Wire.begin(); // Start I2C Bus as Master
Serial.begin(9600);
}

void loop() {

  Wire.beginTransmission(9); // transmit to device #9
  Wire.write(val);              // sends x 
  Wire.endTransmission();    // stop transmitting

}

```

SLAVE  
( sample code )

```cpp
#include <Wire.h>
int x; ( i cant edit int ) ; 
void setup()
{  

   Wire.begin(9);                // Start I2C Bus as a Slave (Device Number 9)
  Wire.onReceive(receiveEvent); // register event

}

void loop()
{

"Print on a LCD x" , 
"x is 0 to 256" 

}

void receiveEvent(int howMany) {
  x = Wire.read();    // receive byte as an integer
}

```

I need the pure data in the other arduino.

---

<div class="post-metadata">

**Author:** ![tmd3](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tmd3/32/1099451_2.png) [@tmd3](https://forum.arduino.cc/u/tmd3)\
**Post date:** [September 17, 2014, 3:39am UTC](https://forum.arduino.cc/t/send-integer-get-256-arduino-i2c-comunication/258120/2 "2014-09-17T03:39:59Z")

</div>

"SLAVE" code doesn't compile. Is this really the code you're using?

---

<div class="post-metadata">

**Author:** ![nickgammon](https://avatars.discourse-cdn.com/v4/letter/n/45deac/32.png) [@nickgammon](https://forum.arduino.cc/u/nickgammon)\
**Post date:** [September 17, 2014, 6:01am UTC](https://forum.arduino.cc/t/send-integer-get-256-arduino-i2c-comunication/258120/3 "2014-09-17T06:01:34Z")

</div>

> **[Gammon Forum : Electronics : Microprocessors : I2C - Two-Wire Peripheral...](http://www.gammon.com.au/i2c)**
>
> Gammon Forum : Electronics : Microprocessors : I2C - Two-Wire Peripheral Interface - for Arduino

See reply #8 on that page.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [September 17, 2014, 8:47am UTC](https://forum.arduino.cc/t/send-integer-get-256-arduino-i2c-comunication/258120/4 "2014-09-17T08:47:55Z")

</div>

tmd3 it's a sample code

I will check Gammon

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [September 17, 2014, 9:18am UTC](https://forum.arduino.cc/t/send-integer-get-256-arduino-i2c-comunication/258120/5 "2014-09-17T09:18:18Z")

</div>

Gammon i dont know why but the library makes me crash the ERW version of the IDE.  
I will try and let you know.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [September 17, 2014, 9:56am UTC](https://forum.arduino.cc/t/send-integer-get-256-arduino-i2c-comunication/258120/6 "2014-09-17T09:56:06Z")

</div>

It arrives at 64x and restart all over. I need to send a \> 10.000 integer

---

<div class="post-metadata">

**Author:** ![nickgammon](https://avatars.discourse-cdn.com/v4/letter/n/45deac/32.png) [@nickgammon](https://forum.arduino.cc/u/nickgammon)\
**Post date:** [September 17, 2014, 11:06am UTC](https://forum.arduino.cc/t/send-integer-get-256-arduino-i2c-comunication/258120/7 "2014-09-17T11:06:30Z")

</div>

Without seeing your code any comment is pure conjecture.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [September 17, 2014, 11:19am UTC](https://forum.arduino.cc/t/send-integer-get-256-arduino-i2c-comunication/258120/8 "2014-09-17T11:19:41Z")

</div>

It's not mine, it's the code of the example.

---

<div class="post-metadata">

**Author:** ![tmd3](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tmd3/32/1099451_2.png) [@tmd3](https://forum.arduino.cc/u/tmd3)\
**Post date:** [September 17, 2014, 2:48pm UTC](https://forum.arduino.cc/t/send-integer-get-256-arduino-i2c-comunication/258120/9 "2014-09-17T14:48:57Z")

</div>

The original post suggests that you're running some sort of code, and that it's not giving you the results you expect. If that's so, please post the code that you're using.

Subsequent posts suggest that you're asking a theoretical question about some example code you found somewhere. If that's so, please tell us that, and post a link to the source of that example code.

---

<div class="post-metadata">

**Author:** ![Grumpy\_Mike](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/grumpy_mike/32/213521_2.png) [@Grumpy\_Mike](https://forum.arduino.cc/u/Grumpy_Mike)\
**Post date:** [September 17, 2014, 3:16pm UTC](https://forum.arduino.cc/t/send-integer-get-256-arduino-i2c-comunication/258120/10 "2014-09-17T15:16:55Z")

</div>

> Piton:  
> It arrives at 64x and restart all over. I need to send a \> 10.000 integer

Might have escaped your notice but 10.000 is not an integer it is a floating point number.

Yes I2C only transmits bytes, you might try and send an int but all that gets sent is a byte. So you need to split it up at the send end, send it a byte at a time and put it back together at the receive end.

We need to see the real code you are having trouble with. The "sample code" you posted is not sample code at all, it is useless non working code.

Unless you cooperate with requests for information no one can help you.

---

<div class="post-metadata">

**Author:** ![nickgammon](https://avatars.discourse-cdn.com/v4/letter/n/45deac/32.png) [@nickgammon](https://forum.arduino.cc/u/nickgammon)\
**Post date:** [September 17, 2014, 8:54pm UTC](https://forum.arduino.cc/t/send-integer-get-256-arduino-i2c-comunication/258120/11 "2014-09-17T20:54:28Z")

</div>

> Piton:  
> Gammon i dont know why but the library makes me crash the ERW version of the IDE.  
> I will try and let you know.

These lines of code crashed the compiler?

```arduino
// Written by Nick Gammon
// May 2012

#include <Arduino.h>
#include <Wire.h>

template <typename T> unsigned int I2C_writeAnything (const T& value)
  {
    const byte * p = (const byte*) &value;
    unsigned int i;
    for (i = 0; i < sizeof value; i++)
          Wire.write(*p++);
    return i;
  }  // end of I2C_writeAnything

template <typename T> unsigned int I2C_readAnything(T& value)
  {
    byte * p = (byte*) &value;
    unsigned int i;
    for (i = 0; i < sizeof value; i++)
          *p++ = Wire.read();
    return i;
  }  // end of I2C_readAnything

```

Better get another compiler then (what's ERW?) - it works fine on the Arduino IDE.

> It arrives at 64x and restart all over. I need to send a \> 10.000 integer

It crashes but also wraps at 64? How does that work?

If you want to send more than 65535 you need to use a long, not an int.

Post your code, enough of this waffle.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [September 18, 2014, 1:33am UTC](https://forum.arduino.cc/t/send-integer-get-256-arduino-i2c-comunication/258120/12 "2014-09-18T01:33:05Z")

</div>

I used this code from gammon library

I2C everything

> **[Gammon Forum : Electronics : Microprocessors : I2C - Two-Wire Peripheral...](http://www.gammon.com.au/forum/?id=10896&reply=8#reply8)**
>
> Gammon Forum : Electronics : Microprocessors : I2C - Two-Wire Peripheral Interface - for Arduino

MASTER ARDUINO UNO

```plaintext
// Written by Nick Gammon
// May 2012

#include <Wire.h>
#include <I2C_Anything.h>

const byte SLAVE_ADDRESS = 42;

void setup() 
{
  Wire.begin ();
}  // end of setup

void loop() 
{

 long foo = 42;
 
 for (float fnum = 1; fnum <= 10; fnum += 0.015)
    {  
    Wire.beginTransmission (SLAVE_ADDRESS);
    I2C_writeAnything (fnum);
    I2C_writeAnything (foo++);
    Wire.endTransmission ();
      
    delay (200);
    }  // end of for

```

SALVE ARDUINO NANO

```plaintext
// Written by Nick Gammon
// May 2012

#include <Wire.h>
#include <I2C_Anything.h>

const byte MY_ADDRESS = 42;

void setup() 
{
  Wire.begin (MY_ADDRESS);
  Serial.begin (115200);
  Wire.onReceive (receiveEvent);
}  // end of setup

volatile boolean haveData = false;
volatile float fnum;
volatile long foo;

void loop() 
{
  if (haveData)
    {
    Serial.print ("Received fnum = ");
    Serial.println (fnum);  
    Serial.print ("Received foo = ");
    Serial.println (foo);  
    haveData = false;  
    }  // end if haveData

}  // end of loop

// called by interrupt service routine when incoming data arrives
void receiveEvent (int howMany)
 {
 if (howMany >= (sizeof fnum) + (sizeof foo))
  {
  I2C_readAnything (fnum);   
  I2C_readAnything (foo);   
  haveData = true;     
  }  // end if have enough data
 }  // end of receiveEvent

```

foo value 0 to 640 +-

I need 10.000 and more.

When i copypaste I2C Everything on ERW IDE the IDE crashes, works with normal IDE.

---

<div class="post-metadata">

**Author:** ![tmd3](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tmd3/32/1099451_2.png) [@tmd3](https://forum.arduino.cc/u/tmd3)\
**Post date:** [September 18, 2014, 4:06am UTC](https://forum.arduino.cc/t/send-integer-get-256-arduino-i2c-comunication/258120/13 "2014-09-18T04:06:27Z")

</div>

Maybe "ERW" is "Enhanced Release for Windows:" [[MOD] Arduino Enhanced Release 1.0.5 for Windows (installer, drivers, etc) +SRC - Libraries - Arduino Forum](http://forum.arduino.cc/index.php?topic=118440.0).  
I get the feeling that the OP isn't really trying.

---

<div class="post-metadata">

**Author:** ![nickgammon](https://avatars.discourse-cdn.com/v4/letter/n/45deac/32.png) [@nickgammon](https://forum.arduino.cc/u/nickgammon)\
**Post date:** [September 18, 2014, 4:46am UTC](https://forum.arduino.cc/t/send-integer-get-256-arduino-i2c-comunication/258120/14 "2014-09-18T04:46:35Z")

</div>

"Crashed", eh? Any error messages you care to copy and paste? Screenshots? More information?

> Maybe "ERW" is "Enhanced Release for Windows

Can't be **that** enhanced if it can't handle templates.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [September 18, 2014, 11:13am UTC](https://forum.arduino.cc/t/send-integer-get-256-arduino-i2c-comunication/258120/15 "2014-09-18T11:13:56Z")

</div>

I would really love the library to work as intended instead of focusing on the ERW problem. I think is your interset too, to debug something you wrote wich is not working. I just cant get the error, i'm not that skilled programmer.

---

<div class="post-metadata">

**Author:** ![nickgammon](https://avatars.discourse-cdn.com/v4/letter/n/45deac/32.png) [@nickgammon](https://forum.arduino.cc/u/nickgammon)\
**Post date:** [September 18, 2014, 11:18am UTC](https://forum.arduino.cc/t/send-integer-get-256-arduino-i2c-comunication/258120/16 "2014-09-18T11:18:19Z")

</div>

I'd love to see your **error messages**. Looks like we will both be disappointed.

---

<div class="post-metadata">

**Author:** ![nickgammon](https://avatars.discourse-cdn.com/v4/letter/n/45deac/32.png) [@nickgammon](https://forum.arduino.cc/u/nickgammon)\
**Post date:** [September 18, 2014, 11:19am UTC](https://forum.arduino.cc/t/send-integer-get-256-arduino-i2c-comunication/258120/17 "2014-09-18T11:19:11Z")

</div>

> Piton:  
> I would really love the library to work as intended instead of focusing on the ERW problem.

The library is working as intended. ERW would **appear to be the problem**.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [September 18, 2014, 1:21pm UTC](https://forum.arduino.cc/t/send-integer-get-256-arduino-i2c-comunication/258120/18 "2014-09-18T13:21:34Z")

</div>

Omg, unbelievable Let's say the ERW problem was my fault ok? HEIII now is working, FINALYY ! Stop talking about it please. YOU ARE OFFTOPIC.

NOW!

Is intended not to send more than 640 long ? Could you please FOCUS ON THIS?

Or could you just tell us you dont know what is happening instead on focusing on the ERW , that is in topic as Global Warming, Chemtrails and Diseases?

---

<div class="post-metadata">

**Author:** ![Grumpy\_Mike](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/grumpy_mike/32/213521_2.png) [@Grumpy\_Mike](https://forum.arduino.cc/u/Grumpy_Mike)\
**Post date:** [September 18, 2014, 1:39pm UTC](https://forum.arduino.cc/t/send-integer-get-256-arduino-i2c-comunication/258120/19 "2014-09-18T13:39:07Z")

</div>

> Piton:  
> Omg, unbelievable Let's say the ERW problem was my fault ok? HEIII now is working, FINALYY ! Stop talking about it please. YOU ARE OFFTOPIC.
> 
> NOW!
> 
> Is intended not to send more than 640 long ? Could you please FOCUS ON THIS?
> 
> Or could you just tell us you dont know what is happening instead on focusing on the ERW , that is in topic as Global Warming, Chemtrails and Diseases?

Wow, is that how you ask for help in other things?

You go to the doctor and say, please cure me.  
Doctor says "what is wrong"?  
You shout "I DON'T KNOW YOU ARE THE DOCTOR"  
Doctor says "where does it hurt"  
You shout "THAT IS NOT IMPORTANT WHY WILL YOU NOT CURE ME"?

What do you think the Doctor says next?

I say the same first word but change the second one from "you" to "off"

---

<div class="post-metadata">

**Author:** ![tmd3](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tmd3/32/1099451_2.png) [@tmd3](https://forum.arduino.cc/u/tmd3)\
**Post date:** [September 18, 2014, 2:04pm UTC](https://forum.arduino.cc/t/send-integer-get-256-arduino-i2c-comunication/258120/20 "2014-09-18T14:04:53Z")

</div>

I feel a strange inclination to abandon this thread.

Must not descend into name-calling; I'll quote the Bard. Interested persons can check the reference. Regarding some of the histrionic, silly things that have been said here:

> Piton:  
> Omg, unbelievable ... YOU ARE OFFTOPIC. ... NOW! ... FOCUS ON THIS? ... tell us you dont know what is happening ...

"... full of sound and fury, signifying nothing." _Macbeth_, Act 5, Scene 5

[Next page](https://forum.arduino.cc/t/send-integer-get-256-arduino-i2c-comunication/258120.md?page=2)
