Go Down

Topic: How to get the most out of my EEPROM? (Read 3410 times) previous topic - next topic

miro2424

Hi guys,

I have a problem how to get the most out of my Arduino EEPROM which has only 512 bytes. I need to store as many as I can 3 digit random numbers from 1 to 504. ex. 242,421,411,2,67,24,92...
Using a 2 bytes integer for each number gives me only a maximum of 256 random numbers. Do any of you have a better idea?

Thanks,

pluggy

Use 8 bytes (one per value) and then use a ninth byte to store the extra bit for each of the preceding 8 bytes. 9 bits will give you any integer value between 0 and 511.  You'll get 455 values in 512 bytes this way.

Or upgrade your chip to a 328 and get 1024 bytes of eeprom.........

Coding Badly

The compiler seems to have very good support for bit-fields.  If you're willing to work in sets of eight values, the compiler should handle all the bit fiddling for you with this...

Code: [Select]
typedef struct
{
 unsigned v1:9;
 unsigned v2:9;
 unsigned v3:9;
 unsigned v4:9;
 unsigned v5:9;
 unsigned v6:9;
 unsigned v7:9;
 unsigned v8:9;
}
tRandomSet;


miro2424

I'm only familiar with Arduino language reference. I don't understand the code that you posted (Coding Badly) nor working with sets of 8 values, could you explain little bit more please?

AWOL

The structure CodingBadly posted allows you store eight 9 bit numbers in 9 bytes.
Code: [Select]
tRandomSet a; // declare a variable of type "tRandomSet"
...
a.v1 = random (1, 505);
a.v2 = random (1, 505);


And so on.
Then all you have to do is write the structure to EEPROM.

tkbyd

Are you storing these data because you are recording data coming into the Arduino, i.e. a data logging application?

If so, there are ways to connect external storage devices...

See http://wulfden.org/TheShoppe/DataLogger/index.shtml
for external EEPROM via I2C

Alternatively, you can connect a device into which you could plug an ordinary thumbdrive, and put the data on that.

See http://www.unlogic.co.uk/2009/03/usb-storage-and-arduino/


PaulS

One thing that nobody has asked is why you want to store random numbers. The whole purpose of random numbers is that they are random. If you store them, and re-use that set of numbers, the results are no longer random.

So, why do you want to store random numbers?

miro2424

Actually those numbers are not random. This data will be coming into the Arduino and will need to be stored even after the Arduino is reset. I don't want to add additional memory devices to the board. The main question is how to store different numbers from 1 to 504 and as many as I can in the EEPROM.

mowcius

Quote
If you store them, and re-use that set of numbers, the results are no longer random.

If you get a computer to generate them then they are not random  :o
http://en.wikipedia.org/wiki/Random_number_generation#Computational_methods

Mowcius

pluggy

I'd be tempted to divide 'em by 2 and store them in a single byte, then multiply them by two when you re-use them.  Slight loss of resolution, simple code, more storage.......

tkbyd

Given your design constraints, you really can't (easily) compress your data much further. If sometimes your data will consist of the same number again and again, you could use run-length encoding... but I doubt it is going to be worth your trouble.

The "use 9 bytes of storage for each 8 data" answer, given above, really is the best you can do, given what you explained as your needs.

If the data is critical, and you can't ensure that power will never be interrupted, you are faced with another problem:

Suppose you've collected 6 data, and written the first 8 bits of each to your EEPROM, saving the 9th bit of each for writing in a moment... and then the power fails.

When the power returns, assuming the device fires up happily, and collects, say, 16 more data, those new 16 data will write nicely to the next 18 bytes in the EEPROM.... assuming, by the way, that you've solved the problem of "where do I continue writing after a power failure" problem.

But then when you go to read the data, all will be well until you get to the block of 6. There will be no way to SEE that it is a block of 6. Those 6 data will be read, and then the 1st of what is actually the next block of 8 (NB8) will be treated as the 7th of the earlier block, the 2nd in the actual NB8 will be taken as the 8th of the earlier block, and then the 9th bits for each of the earlier 8 will be generated from what is actually the 3rd byte of the NB8.  Mayhem!

One answer that makes your data safer, overall, at the risk of losing "most recent" data (up to 8 readings) on every power fail....

Do not write anything to the EEPROM until you have collected 8 data. Then write them, as 9 bytes, as previously described. And write a 10th byte which is the complement of the 9th byte. Then, when reading the data, use that 10th byte as a mini checksum. The power COULD still fail in the middle of writing the block of 10 bytes... but the chances of that would be much lower than the chances of the problems under a progressive write scheme. And you would have a 254/255 chance of SEEING that it happened, and of revovering the data "downstream" from the glitch in the data. If 254/255 isn't good enough odds, then do another checksum byte, made up, say, of the LSB of each of the first 8 bits. It makes your data storage a little less dense, but increased the chances of dealing with a power-failed problem to 65535/65536.

Adding external storage REALLY isn't so terrible, you know, and those skills would open up so manuy possibilities!

Another "answer" (using external storage) is the 1-Wire devices. Again, the skills of interfacing to 1-Wire would be of considerable general use. Also, they would offer the convenience of easy removal of storage medium (like a USB-connected thumbdrive is easily removed), while at the same time being almost as inexpensive as the wulfden MemoryStamp answer.

Go Up