using eeprom

Why does this code not retain the EEPROM data at locations 0 & 1?
I write to it, read it back and it seems to be there,
write to 1200 other locations,
then read back 0 & 1 and its gone.

What the heck?

  case 0x53:
    // capital S for saving to EEPROM
    SaveMode = SaveMode + 1;
    if (SaveMode == 3){ // wait for SSS
      //dataEntry = Memory;
      SaveMode = 0;
      // save length in EEPROM, write displayArray to EEPROM
      Serial.print ("saving to EEPROM, displayEnd = ");
      EEPROM.write(0, (lowByte (displayEnd)));
      EEPROM.write(1, (highByte (displayEnd)));
      Serial.print (EEPROM.read(1));
      Serial.print (" ");
      Serial.println (EEPROM.read(0));
      for (x = 2; x<(arraySize); x=x+1){
        EEPROM.write(x, displayArray[x]);
        Serial.print(x);
        Serial.print (" ");
        Serial.println (EEPROM.read(x));
      }

      Serial.print ("EEPROM address 0 = ");
      Serial.println (EEPROM.read(0));
      Serial.print ("EEPROM address 1 = ");
      Serial.println (EEPROM.read(1));

      highEEPROM = EEPROM.read(1);
      lowEEPROM = EEPROM.read(0);
      EEPROMend = (highEEPROM<<8) + lowEEPROM;
      Serial.print ("EEPROMend = ");
      Serial.println (EEPROMend);

    }
    break;

Serial monitor:

:
:
Displaying from 140 to 152
Displaying from 140 to 152
saving to EEPROM, displayEnd = 0 152
2 0
3 0
4 0
5 0
6 0
7 0
8 254
9 1
10 6
11 1
12 254
:
:
1196 0
1197 0
1198 0
1199 0
EEPROM address 0 = 0
EEPROM address 1 = 0
EEPROMend = 0
Displaying from 141 to 152
Displaying from 142 to 152
:
:

Processor?

arraySize definition?

Try arraySize = 1023 instead of 1200, and see what happens.

Also, what's with x=x+1 etc.? The language is called C++, not C=C+1. :grin:

Also, what's with x=x+1 etc.? The language is called C++, not C=C+1.

You've been reading too many of PaulS's posts. Does the resulting code run any differently? If not, what does it matter?

CrossRoads:
Why does this code not retain the EEPROM data at locations 0 & 1?
I write to it, read it back and it seems to be there,
write to 1200 other locations,
then read back 0 & 1 and its gone.

What the heck?

I had a similar problem a few months ago. Turned out some previous sketch had worn out the first six bytes of EEPROM.

dxw00d:
You've been reading too many of PaulS's posts. Does the resulting code run any differently? If not, what does it matter?

Well, observing stylistic conventions in a language makes it easier for other people to read your code. Using x++ means there's also less typing, and less scope for sources of error in general.

The other thing is that you get more precision in terms of expression; you can choose to use either x++ or ++x an the increment operator, and the difference can often be exploited to write code that is concise, powerful, and even elegant.

C programming has its artistic element, after all.

In general, there are many ways to write code that will result in equivalent compiler output, but source code is not just for machines to read, it is (usually) also to be read by humans. If you think they are all equivalent, I invite you to look at some creative examples of deliberately obfuscated code. :wink:

Capital S is best written 'S', then you don't need the comment to remind you.

source code is not just for machines to read, it is (usually) also to be read by humans.

Exactly, so all things being equal, in terms of resulting code, you write the way you are comfortable with. Those of us that code professionally have coding standards to adhere to, for that reason. People that code for a hobby, and have no others regularly collaborating on the code, can write how they like.

The other thing is that you get more precision in terms of expression; you can choose to use either x++ or ++x an the increment operator, and the difference can often be exploited to write code that is concise, powerful, and even elegant.

In the line of code being discussed, this is entirely irrelevant.

dxw00d:
People that code for a hobby, and have no others regularly collaborating on the code, can write how they like.

Well, of course, a hobbyist is free to write how they like. But perhaps they might aspire to gain a higher level of proficiency in the art? Just a thought.

The other thing is that you get more precision in terms of expression; you can choose to use either x++ or ++x an the increment operator, and the difference can often be exploited to write code that is concise, powerful, and even elegant.

dxw00d:
In the line of code being discussed, this is entirely irrelevant.

But the points I make are quite a general ones, rather than about a single line of code. After all, you asked the general question "Does the resulting code run any differently? If not, what does it matter?" So I answered that question. Perhaps the points being made are a bit abstract for you -- you strike me as a fairly, er, blunt fellow.

And of course this:

dxw00d:
collaborating

is basically an own goal, in overtime, anyway. Bob is posting code for the sole purpose of collaboration with his fellow coders. Obviously.

In any case, I'm sure he can tell the difference between a snide dig and a good humored pointer on programming style.

Unlike some. Thanks for playing. :roll_eyes:

I'm sure he can tell the difference between a snide dig and a good humored pointer on programming style.

As can I, but when PaulS picks it up all the time, in that subtle way of his, the humour tends to fade.

It's really not worth a big argument. It's called programming style, because that's all it is.

Crap - I refreshed instead of hitting post, all my typing got dumped.

Anyway,

Here's the odd part - I had this working, so I went thru and commented out the various Serial.print's that had been added for debugging, as they impact how big the array can be. And it stopped working! So I put them back in thinking maybe a timing thing - no help.

The processor is just a '328, actually using a Duemilanove card.
I have

const unsigned int arraySize = 1200;
byte displayArray[(arraySize-1)];

defined in the pre-setup code area so I can change the size in one place easily.

I've had the array at 1500 before I started the EEPROM storge, had knocked it down to 1200. Can try a little smaller.
Is odd to me that the two addresses are apparently being read back properly before addresses 2-1199 are written & read, and then 0-1 cannot be read.

x=x+1. I just like being able to quickly see what I have going on. You might say I don't completely buy in to the C++ abstractions & overloading & structures vs functions & whatever - I just treat it like fancy BASIC and x=x+1 suits my needs. If I could, I would still use "next x;' 8)

If I could, I would still use "next x;'

It's macro time!

There's only 4 macro's I like - CTRL-A, CTRL-C, CTRL-V - and then CTRL-Z :grin:

CrossRoads:
I've had the array at 1500 before I started the EEPROM storge, had knocked it down to 1200. Can try a little smaller.

The reason I suggested 1023 was to see if you were getting address wrap at 1024 (i.e., a 10-bit address for the eeprom, 1024 = 0, 1025 = 1, etc.) Are you using the eeprom on the actual 328 chip? Because that's only got 1024 bytes of eeprom!

Although I've never tried it, my guess is that writing to an eeprom address x > 1023 almost certainly would be writing to eeprom address x % 1024 (i.e., the address wraps back to zero). So what you are seeing at eeprom addresses x = 0 and x = 1 after the write are the contents of displayArray[1024] and displayArray[1025].

Make sense?

Crap - I refreshed instead of hitting post, all my typing got dumped.

Glad it's not just me. The amount of time I write and review a reply but then forget to hit post. Don't realize it till some time later when someone else replies to the post and I think 'I replied to that' where did the it go.

Only 1K of EEPROM - I totally forgot that! :fearful:
I know I was thinking I had 2K to play with for some reason. But its 2K of SRAM. Rats.

I might have been testing with arraySize at 500 when it was working (just to have it complete faster).
Thanks, will scale it back some more tonight & see if goes back to working.
This is where the 2nd (and 3rd, and 4th) set of eyes comes in handy :sweat_smile:

CrossRoads:
This is where the 2nd (and 3rd, and 4th) set of eyes comes in handy :sweat_smile:

OK, so now we've got that settled, let's revisit this x=x+1 thing...

]:smiley:

let's revisit this x=x+1 thing...

This whole section:

Compound Operators
++ (increment) 
-- (decrement) 
+= (compound addition) 
-= (compound subtraction) 
*= (compound multiplication) 
/= (compound division) 

&= (compound bitwise and) 
|= (compound bitwise or)

just ain't gonna get used!

dxw00d:
It's really not worth a big argument. It's called programming style, because that's all it is.

It's much more than just a fashion choice if that's what you're implying.

It's more like having a good swimming style or a good golfing style. A good style means you'll perform better, arrive sooner, be less tired.

CrossRoads:

let's revisit this x=x+1 thing...

This whole section:

just ain't gonna get used!

Do you like
green eggs and ham

I do not like them,
Sam-I-am.
I do not like
green eggs and ham.

Would you like them
Here or there?

I would not like them
here or there.
I would not like them
anywhere.
I do not like
green eggs and ham.
I do not like them,
Sam-I-am

Would you like them
in a house?
Would you like them
with a mouse?

I do not like them
in a house.
I do not like them
with a mouse.
I do not like them
here or there.
I do not like them
anywhere.
I do not like green eggs and ham.
I do not like them, Sam-I-am.

Would you eat them
in a box?
Would you eat them
with a fox?

Not in a box.
Not with a fox.
Not in a house.
Not with a mouse.
I would not eat them here or there.
I would not eat them anywhere.
I would not eat green eggs and ham.
I do not like them, Sam-I-am.

Would you? Could you?
in a car?
Eat them! Eat them!
Here they are.

I would not ,
could not,
in a car

You may like them.
You will see.
You may like them
in a tree?
d not in a tree.
I would not, could not in a tree.
Not in a car! You let me be.

I do not like them in a box.
I do not like them with a fox
I do not like them in a house
I do mot like them with a mouse
I do not like them here or there.
I do not like them anywhere.
I do not like green eggs and ham.
I do not like them, Sam-I-am.

A train! A train!
A train! A train!
Could you, would you
on a train?

Not on a train! Not in a tree!
Not in a car! Sam! Let me be!
I would not, could not, in a box.
I could not, would not, with a fox.
I will not eat them with a mouse
I will not eat them in a house.
I will not eat them here or there.
I will not eat them anywhere.
I do not like them, Sam-I-am.

Say!
In the dark?
Here in the dark!
Would you, could you, in the dark?

I would not, could not,
in the dark.

Would you, could you,
in the rain?

I would not, could not, in the rain.
Not in the dark. Not on a train,
Not in a car, Not in a tree.
I do not like them, Sam, you see.
Not in a house. Not in a box.
Not with a mouse. Not with a fox.
I will not eat them here or there.
I do not like them anywhere!

You do not like
green eggs and ham?

I do not
like them,
Sam-I-am.

Could you, would you,
with a goat?

I would not,
could not.
with a goat!

Would you, could you,
on a boat?

I could not, would not, on a boat.
I will not, will not, with a goat.
I will not eat them in the rain.
I will not eat them on a train.
Not in the dark! Not in a tree!
Not in a car! You let me be!
I do not like them in a box.
I do not like them with a fox.
I will not eat them in a house.
I do not like them with a mouse.
I do not like them here or there.
I do not like them ANYWHERE!

I do not like
green eggs
and ham!

I do not like them,
Sam-I-am.

You do not like them.
SO you say.
Try them! Try them!
And you may.
Try them and you may I say.