problemi per scrivere numeri a 5 cifre su eeprom esterna

Salve a tutti come da titolo devo scrivere numeri su eeprom esterna di ordini di 10^4.

Sul reference di arduino ci sono due funzioni:

write_byte e write_page.

La write byte scrive un byte per volta e riceve come parametro byte
solo che con un byte posso scrivere fino a 255 + 0.

La write page riceve invece (byte*)

e la uso in questo modo:

int x[5];
itoa(numero,x,10);
write_page(address,(byte*)x);

solo che in questo modo mi servono 5 byte per rappresentare un numero, quando me ne basterebbero 2 o 3.

Come posso fare per scrivere numeri grandi, magari con segno??

 /* 
  *  Use the I2C bus with EEPROM 24LC64 
  *  Sketch:    eeprom.pde
  *  
  *  Author: hkhijhe
  *  Date: 01/10/2010
  * 
  *   
  */

  #include <Wire.h> //I2C library



  void i2c_eeprom_write_byte( int deviceaddress, unsigned int eeaddress, byte data ) {
    int rdata = data;
    Wire.beginTransmission(deviceaddress);
    Wire.send((int)(eeaddress >> 8)); // MSB
    Wire.send((int)(eeaddress & 0xFF)); // LSB
    Wire.send(rdata);
    Wire.endTransmission();
  }

  // WARNING: address is a page address, 6-bit end will wrap around
  // also, data can be maximum of about 30 bytes, because the Wire library has a buffer of 32 bytes
  void i2c_eeprom_write_page( int deviceaddress, unsigned int eeaddresspage, byte* data, byte length ) {
    Wire.beginTransmission(deviceaddress);
    Wire.send((int)(eeaddresspage >> 8)); // MSB
    Wire.send((int)(eeaddresspage & 0xFF)); // LSB
    byte c;
    for ( c = 0; c < length; c++)
      Wire.send(data[c]);
    Wire.endTransmission();
  }

  byte i2c_eeprom_read_byte( int deviceaddress, unsigned int eeaddress ) {
    byte rdata = 0xFF;
    Wire.beginTransmission(deviceaddress);
    Wire.send((int)(eeaddress >> 8)); // MSB
    Wire.send((int)(eeaddress & 0xFF)); // LSB
    Wire.endTransmission();
    Wire.requestFrom(deviceaddress,1);
    if (Wire.available()) rdata = Wire.receive();
    return rdata;
  }

  // maybe let's not read more than 30 or 32 bytes at a time!
  void i2c_eeprom_read_buffer( int deviceaddress, unsigned int eeaddress, byte *buffer, int length ) {
    Wire.beginTransmission(deviceaddress);
    Wire.send((int)(eeaddress >> 8)); // MSB
    Wire.send((int)(eeaddress & 0xFF)); // LSB
    Wire.endTransmission();
    Wire.requestFrom(deviceaddress,length);
    int c = 0;
    for ( c = 0; c < length; c++ )
      if (Wire.available()) buffer[c] = Wire.receive();
  }




  void setup() 
  {
    char somedata[] = "this is data from the eeprom"; // data to write
    Wire.begin(); // initialise the connection
    Serial.begin(9600);
    i2c_eeprom_write_page(0x50, 0, (byte *)somedata, sizeof(somedata)); // write to EEPROM 

    delay(10); //add a small delay

    Serial.println("Memory written");
  }

  void loop() 
  {
    int addr=0; //first address
    byte b = i2c_eeprom_read_byte(0x50, 0); // access the first address from the memory

    while (b!=0) 
    {
      Serial.print((char)b); //print content to serial port
      addr++; //increase address
      b = i2c_eeprom_read_byte(0x50, addr); //access an address from the memory
    }
    Serial.println(" ");
    delay(2000);

  }

Li dividi in 2 byte (int) o 4 Byte (long)
Per gli int puoi usare
http://arduino.cc/en/Reference/LowByte
http://arduino.cc/en/Reference/HighByte

Ciao Uwe

Basta un unsigned int: 10^4 sono 10.000 combinazioni, un unsigned int tiene da 0 a 65.535 quindi è più che suff. E lo scomoni con le funzioni suggerite da Uwe, lowByte e highByte ottenendo 2 byte che scrivi uno alla volta.

leo72:
Basta un unsigned int: 10^4 sono 10.000 combinazioni, un unsigned int tiene da 0 a 65.535 quindi è più che suff. E lo scomoni con le funzioni suggerite da Uwe, lowByte e highByte ottenendo 2 byte che scrivi uno alla volta.

Anche 99000 è un numero dell'ordine di 10^4 (9,9 * 10^4) quindi non è detto sia sufficiente un unsigned int, dipende dal reale valore massimo che deve raggiungere.

Se scrive 10^4 io interpreto 10.000 come ultimo numero utile. Se voleva usare 99.000 diceva un numero 10^5.
Poi sono sfumature dialettali, una strada gliel'abbiamo suggerita: sta all'utente lavorare su questa traccia.