eeprom storing numbers larger then 256?

so ive been playing around with the eeprom for a few days now and i have managed to achieve what i was try to,

but id like to know more about storing a number larger then 256 in the eeprom,

the company i work for have a programmer which we sell, currently at the moment the programmer is unlimited, (it programs doors) however the big boss would like the option to sell these programmers with x amount of uses on them, (a basic count down on the eeprom) ive managed to achieve this but the problem i have is i can currently only write a max of 256 to eeprom.

can anyone point me in the right directon of how this is done.

i love learning this stuff, ive checked the examples but cant find anthing,

Regards.

http://playground.arduino.cc/Code/EEPROMWriteAnything

Currently you can only write a max of 255 (0xFF), not 256 (0x100) ...

i did have a look at this, and thought it was maybe a bit over kill for writing 1000

is this the only way i can get it to write 1000 ?

And thanks Mark, i thought it was 256 not 255,

thanks :smiley:

Split the number up into pieces and store the pieces.

Then later, you can read the pieces and reassemble the number.

Look up "division" and "modulo" in the reference.

Look where sorry ?
ive been threw arduino software looking at examples.

i write this which has compiled but im not sure if it will work, i currently cant test as ive broken my board and waiting on a replacement.

would this below work ?

int eepromMemory = 1;  // set the location of the memory were going to use in the eeprom/
int counterValue; //  set the var
int subtractCounter = 1;
int testValue;
int i;
#include <EEPROM.h> // include the eerpom libary
int LED = 13;
void setup() {
  pinMode(LED, OUTPUT);
  // put your setup code here, to run once:
// set the serial monitor
Serial.begin(9600);

counterValue = EEPROM.read(eepromMemory);
Serial.println("Previous eeprom Value");
Serial.println(counterValue);
delay(500);
Serial.println("New eeprom Value");
if (counterValue, testValue > 0) {counterValue = counterValue - subtractCounter;  // check to see if counter above zero, if so deduct
} else {
//do nothing
} 

//counterValue = counterValue - subtractCounter;
EEPROM.write(eepromMemory, counterValue);
delay(500);
Serial.println(counterValue);

}

void loop() {
  // put your main code here, to run repeatedly:
// start test code
//if (EEPROM.read(counterValue == 0)
//for(int i=0; i<=255; i++);
//EEPROM.write(i, 0);
// end test code


if (counterValue > 0) {
  
   digitalWrite(LED, LOW);
// need to find a way to erase the eeprom to prevent it from looping.
} else {digitalWrite(LED, HIGH);} 
}

this is the line i have modified.

if (counterValue, testValue > 0) {counterValue = counterValue - subtractCounter;  // check to see if counter above zero, if so deduct

so basically im telling the script to check counterValue & testValue if both are above zero to remove the count from one of them ?

ive had alook at splitting up the number but i dont quite understand how it works,

could someone give me an example ? Regards

There are two macros, highByte() and lowByte(), that will extract the high and low bytes from an integer. Since you can store bytes, storing two of them means that you can store an int.

There is a macro, word(), that combines two bytes to make an int, so you can recreate the int that you split up to store.

Of course, the macros are doing nothing that you can't do with division and the modulo function or bit-shifting and masking.

But, they are documented on the Reference page.

hi paul,

ive had a look at the following pages but there are no examples or explanations on how its done ?

www.arduino.cc/en/Reference/HighByte

ive tried the following
highByte(1000)

which is will not allow me to compile like this.

Another way is to use a union for the target data type:

#include <EEPROM.h>

union {
  byte myArray[2];
  int val;
} myUnion;

void setup() {

  int i;
  int j;
  int offset = 0;
  int temp;

  Serial.begin(9600);
  myUnion.val = 1000;

  for (j = 0; j < 10; j++) {
    for (i = 0; i < sizeof(int); i++) {
      EEPROM.write(offset, myUnion.myArray[i]);         // Write to EEPROM
      offset++;
    }
    Serial.println(myUnion.val);
    myUnion.val++;
  }
  Serial.println("====== Reading Back ======");
  offset = 0;
  for (j = 0; j < 10; j++) {
    for (i = 0; i < sizeof(int); i++) {
      myUnion.myArray[i] = EEPROM.read(offset++);  // Read from EEPROM
    }
    Serial.println(myUnion.val);
  }

}

void loop() {
  // put your main code here, to run repeatedly:

}

ive had a look at the following pages but there are no examples or explanations on how its done ?

int val = 1024;
byte lo = lowByte(val);
byte hi = highByte(val);

int w = word(hi, lo);

so i assume when it comes to reading the value and writing it i just call the val; in my code

mathewbuer:
so i assume when it comes to reading the value and writing it i just call the val; in my code

When storing the data in EEPROM, you'd use the highByte() and lowByte() macros to take the int apart for storage.

When reading the data from EEPROM, you'd use the word() macro to reassemble the two bytes read to put the int back together.

thanks, ill give it a go. if i get stuck, after doing my research ill post back.

this is where im at so far, it compiles, can someone tell me if im onto the right track, as with eeprom as far as im aware there is no way to tell if im writing to the eeprom correctly.

#include <EEPROM.h>

int eepromMemory1 = 1;
int eepromMemory2 = 2;
int counterLimit = 1000;
byte HighByte1(eepromMemory1);
byte LowByte1(eepromMemory1);
byte HighByte2(eepromMemory2);
byte LowByte2(eepromMemory2);
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
EEPROM.write(eepromMemory1, HighByte1);
EEPROM.write(eepromMemory2, LowByte1);
}

void loop() {
  // put your main code here, to run repeatedly:

}

so from what ive worked out (weather it be right or wrong) is this code should write the number 1000 to the eeprom in 2 segments ?

Avr Micro were already programmed before the existence of Wiring / Arduino. IDE.

Have a look here:
http://www.nongnu.org/avr-libc/user-manual/group__avr__eeprom.html

You will found functions for Write /Read byte, word, float, bloc in Eeprom

Nota Bene : Wiring / Arduino functions are just an encapsulation of byte functions in a class to avoid using a pointer.

Edit : it seems that there is new version in 2015.
The previous version was :

uint8_t EEPROMClass::read(int address)
{
	return eeprom_read_byte((unsigned char *) address);
}

void EEPROMClass::write(int address, uint8_t value)
{
	eeprom_write_byte((unsigned char *) address, value);
}

You can recognize avr-lic functions.

can someone tell me if im onto the right track

No, you are not.

byte HighByte1(eepromMemory1);
byte LowByte1(eepromMemory1);
byte HighByte2(eepromMemory2);
byte LowByte2(eepromMemory2);

These are function prototypes.

EEPROM.write(eepromMemory1, HighByte1);
EEPROM.write(eepromMemory2, LowByte1);

Here, you are trying to write the address of a function into EEPROM.

Isn't it counterLimit that you want to read from EEPROM, recombine, decrement, and write back to EEPROM?

correct PaulS but i decided to start a new project for this untill i understand how it all works,

at the moment i have something which works, (upto 255)

im now trying to master value above 1000 and then ill intergrate it into my current project.,

im now trying to master value above 1000 and then ill intergrate it into my current project.,

int counter = 1000;

void setup()
{
   // Read the stored values
   byte hi = EEPROM.read(1);
   byte lo = EEPROM.read(2);

   // Reassemble the integer
   counter = word(hi, lo);

   // Decrement to 0 but not lower
   if(counter > 0)
     counter--;

   // Store the new value
   EEPROM.write(1, highByte(counter));
   EEPROM.write(2, lowByte(counter));
}

void loop()
{
   if(counter > 0)
   {
      // do it up to 1000 times
   }
}

mathewbuer:
this is where im at so far, it compiles, can someone tell me if im onto the right track, as with eeprom as far as im aware there is no way to tell if im writing to the eeprom correctly.

If you are using the Arduino IDE version 1.6.2 or greater, then your task becomes far easier. The method EEPROM.put() can write values larger than 255 (data types greater than 1 byte). You can use EEPROM.get() to read the data.

Here is an example of reading and writing an int!

#include <EEPROM.h>

void setup(){
  int data = 1000;
  int address = 0; //data will be stored in cells 0 & 1
  
  Serial.begin(9600);
  Serial.print( "data: " );
  Serial.println( data );
  
  // Write data to EEPROM
  EEPROM.put( address, data );
  
  int result = 0; //So we can test the read.
  
  // Read the data.
  EEPROM.get( address, result );

  Serial.print( "result: " );
  Serial.println( result );
}

void loop(){}

This will work for numbers between 0 & 65535.

#include <EEPROM.h>

void setup(){
  Serial.begin(115200);
  uint8_t address = 0;
  uint16_t x = 12345;
  Serial.println(x);
  EEPROM16_Write(address, x);
  x = 0;
  Serial.println(x);
  x = EEPROM16_Read(address);
  Serial.println(x);
}

void loop(){
}

void EEPROM16_Write(uint8_t a, uint16_t b){
  EEPROM.write(a, lowByte(b));
  EEPROM.write(a + 1, highByte(b));
}

uint16_t EEPROM16_Read(uint8_t a){
  return word(EEPROM.read(a + 1), EEPROM.read(a));
}