Go Down

Topic: Saving arrays in EEPROM using ARDUINOMEGA (Read 7143 times) previous topic - next topic

Daniel Formosa

Dear all, i need to save a 3 digit # and a 4 digit # in an array[3 digit #]  = (4 digit #) using the EEPROM since I am recieving this data using serial GUI.

Can anyone give any hints on how I can start since I have no idea how to bgin.
Tnx

PaulS

Quote
i need to save a 3 digit # and a 4 digit # in an array

The number of digits in the numbers is irrelevant, as long as the array type is correct (int, not byte).

Quote
array[3 digit #]  = (4 digit #)

Does this mean that you need an array whose size is somewhere between 111 to 999?

Quote
using the EEPROM

On which Arduino? You may or may not have enough room in EEPROM for a 1000 element int array.

Quote
I am recieving this data using serial GUI.

How? The Arduino doesn't have the native ability to display a GUI.

liudr

Also, are you using the ATMEGA's onboard EEPROM or external EEPROMs via I2C like 24LC512?

Daniel Formosa

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

liudr

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.

AWOL

Quote
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 = ?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Daniel Formosa

Ok...so this is the code(it doesn't have the GUI and stuff just an short program...)
Code: [Select]
#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

AWOL

Quote
So how can I save 323 then??


As two bytes, aka "int".
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Daniel Formosa

Sry this is the code..
Code: [Select]

#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()
{
   
}

Daniel Formosa

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

AWOL

#10
Mar 09, 2011, 09:12 pm Last Edit: Mar 09, 2011, 09:15 pm by AWOL Reason: 1
Quote
It is saved as int....
int reference = 323;

But the EEPROM is only byte wide.

sizeof (byte) == sizeof (int) / 2  (for the Arduino)
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

PaulS

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

Code: [Select]
EEPROM.write(0, highByte(reference));
EEPROM.write(1, lowByte(reference));


To read an int:

Code: [Select]
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

Daniel Formosa

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

That is a problem....

AWOL

#13
Mar 09, 2011, 09:26 pm Last Edit: Mar 09, 2011, 09:28 pm by AWOL Reason: 1
Quote
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
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Daniel Formosa

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

Go Up