Send integer get 256. Arduino I2C comunication

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 )

#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 )

#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.

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

See reply #8 on that page.

tmd3 it's a sample code

I will check Gammon

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

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

Without seeing your code any comment is pure conjecture.

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

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.

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.

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?

// 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.

I used this code from gammon library

I2C everything

MASTER ARDUINO UNO

// 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

// 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.

Maybe "ERW" is "Enhanced Release for Windows:" [MOD] Arduino Enhanced Release 1.0.5 for Windows (installer, drivers, etc) +SRC - Libraries - Arduino Forum.
I get the feeling that the OP isn't really trying.

"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.

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.

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

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.

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?

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"

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