Saving arrays in EEPROM using ARDUINOMEGA

Yes i know the arduino doesn't have the GUI, but I managed to program it serailly using C# using forms.
That has been sorted out and PAULS, it is tnx to you that I managed it.

I am using the Arduino Mega, having an inbuilt 4kb EEPROM.

Ok so the digits are irrelivant....

What I need is this:
Imagine I recieved an integer serially, say for example 323 (this has been converted from character to an array an to an integer.......) So we have an integer saved in the FLASH memory, i.e. 323.
An then I recieve another # say for example 1234(the same procedure from a char to int.....).So we have another integer saved in the FLASH.
i.e. eg int reference_no = 323;
int gas = 1234;
Therefore I need to save these integers in an array in the EEPROM.

This is what I had in mind:

EEPROM:
ADDRESS VALUE
0 323
1 1234
2
.
.
.
4095

The problem I currently have is when writing for example 323 in the EEPROM in address 0, and when I read it, it isn't 323 saved but 67..???
Is this since the number is greater than 255????

How can I save 323 then??
TNX

Just to straight out the terms, FLASH you mentioned in NOT EEPROM.
You need to show the code. I suspect you only read one byte instead of two. I don't know if you wrote two bytes or not with your code. You know, 323 needs two bytes. If you only write the low byte, you get 323 chopped to fit within 255=67

It's probably that.

The problem I currently have is when writing for example 323 in the EEPROM in address 0, and when I read it, it isn't 323 saved but 67..???

323 - 256 = ?

Ok...so this is the code(it doesn't have the GUI and stuff just an short program...)

#include <EEPROM.h>

void setup()
{
  Serial.begin(9600);
  int reference = 323;
  int gas = 1234;
  int serial;
  
  EEPROM.write(0,323);
  
  serial =EEPROM.read(0);
  
  Serial.print(serial);
  
  
}
void loop()
{
  int reference = 323;
  int gas = 1234;
  int serial;
  
  EEPROM.write(0,323);
  
  serial =EEPROM.read(0);
  
  Serial.print(serial);
  
}

So how can I save 323 then??

tnx

So how can I save 323 then??

As two bytes, aka "int".

Sry this is the code..

#include <EEPROM.h>

void setup()
{
  Serial.begin(9600);
  int reference = 323;
  int gas = 1234;
  int serial;
  
  EEPROM.write(0,323);
  
  serial =EEPROM.read(0);
  
  Serial.print(serial);
  
  
}
void loop()
{
    
}

It is saved as int....
int reference = 323;
???

It is saved as int....
int reference = 323;

But the EEPROM is only byte wide.

sizeof (byte) == sizeof (int) / 2 (for the Arduino)

EEPROM.write() and EEPROM.read() operate on bytes. To save an int:

EEPROM.write(0, highByte(reference));
EEPROM.write(1, lowByte(reference));

To read an int:

int val = (EEPROM.read(0) << 8) + EEPROM.read(1);

You might want to look at the EEPROM_readAnything() and EEPROM_writeAnything() functions:
http://www.arduino.cc/playground/Code/EEPROMWriteAnything

Ok didn't know this....
So I am using 2 addresses inside the EEPROM now?

That is a problem....

So I am using 2 addresses inside the EEPROM now?

So you've got at least 510 more to play with

That's not a problem - read and understand reply #11

No it is a problem...
The integer of the reference # varies from 322 to 500 (so in this case it the above procedure needs to be done, if I am correctly understanding)
While the gas # varies from 500 to 1200. Which means I am taking more than one and more addresses

But I need to save these #s in an array which is proportional to the reference # and gas

meaning array[reference] = gas

So how can I know which address it is if each refernce # and gas amount is different entered by the user??

I don't know If I described it correctly....

Make it simple - always store and recover "int"s.
Read reply #11.
Really, there is no problem.

I don't know about the application and description of the needs, but simple way to get high and low byte would be like this.

If your integer is 1234, just do 1234/256=4
4 is the value of your highbyte

Then next you take (4256) out from your original int; 1234 - (4256) =210
210 is your lower byte.

Then, when you need to know the address your about to write these values.

If the pointer in the array is, lets say 51, so you have value 1234 in YourArray[51], you write it to eeprom;
EEPROM.write( (251) , lowByte );
EEPROM.write( ( (2
51) + 1) , highByte );

I'm a bit tired, so there's probably errors in my text, please, don't hesitate to say that was bull...it.

Cheers,
Kari

EDIT. I have never used eeprom library, that maybe wrong syntax...

Yes i read it.
So do I put the writeanyting and readanyting as functions in my program?
Can I save them in a library of some sort?

tnx

Can I save them in a library of some sort?

You could put them in a simple header file - they are simple templates.
As long as you know how big your objects are, you can store and read back any size, up to the maximum size of the EEPROM on your processsor.
I'd probably recommend describing a single struct to store all the values you need to save, so a single call will save/restore all your values, though I'd probably rewrite "writeAnything" to only write a byte if it had changed (read it first to compare)

The "readAnything", is not working prop...

This is my code:

#include <EEPROM.h>
template <class T> int EEPROM_writeAnything(int ee, const T& value)
{
    const byte* p = (const byte*)(const void*)&value;
    int i;
    for (i = 0; i < sizeof(value); i++)
	  EEPROM.write(ee++, *p++);
    return i;
}

template <class T> int EEPROM_readAnything(int ee, T& value)
{
    byte* p = (byte*)(void*)&value;
    int i;
    for (i = 0; i < sizeof(value); i++)
	  *p++ = EEPROM.read(ee++);
    return i;
}
void setup()
{
  Serial.begin(9600);
  int reference = 323;
  int gas = 1234;
  int serial;
  int serial1;
  
  
    EEPROM_writeAnything(0, reference);
    serial = EEPROM_readAnything(0,reference);
    
   
   Serial.print(serial);
    
    
}
void loop()
{
    
}

2 is displayed now.

Does it work properly???

Yes it works perfectly.
"reference" is two bytes long, so 2 is printed

Did you mean to print the value of "reference" you read back from the EEPROM?

(Is your question mark key a bit sticky?)

Hehe no it ain't im just stupid lol.
Ok so it prints 2 since its 2 bytes....(and NO . mark key aint sticky :P)

Yes how can I print the value stored in the EEPROM?
tnx

Yes how can I print the value stored in the EEPROM?

Serial.print(reference);

because that's the last value you read back