Better way to load variables in EEPROM?

I have some variables in the EEPROM. Durring run time my program loads them and uses them. Reading the EEPROM and then using it in a variable during run time does work fine.

My question is this: Can it be done more efficentlly? Can I load the variables directly into the program flash memory space at startup?

This is not a deal breaker: my program DOES RUN I just want to make it smaller and possibly faster.

Thank you!

Duplicate post
https://forum.arduino.cc/index.php?topic=513790.0

Reading the EEPROM and then using it in a variable during run time does work fine.

Do you read them in setup() ?

An example of how it helps to post the code.

aarg:

Not a duplicate post. The other post your reference involves writing to program memory not EEPROM memory. EEPROM and flash - program memories are kept in seperate areas and for the most part flash - program memory is not [easily ] accessable. EEPROM is easily and directly accessable.

aarg: You can not WRITE to the flash-program memory easily and if you could you still need to know memory address, opcodes, etc. EEPROM is very easy to read / write to. I just want to see if I can make it work better (less program bloat, faster execution, etc).

I just want to see if I can make it work better (less program bloat, faster execution, etc).

So post it here and you will get help

SamBrownADK:
aarg:

Not a duplicate post. The other post your reference involves writing to program memory not EEPROM memory. EEPROM and flash - program memories are kept in seperate areas and for the most part flash - program memory is not [easily ] accessable. EEPROM is easily and directly accessable.

You're tap dancing.

Can I load the variables directly into the program flash memory space at startup?

value = EEPROM.read(4);

...
...
...
...
...

if (j > value)
{
do_something ;
}

This works, but is slow and inefficent. So is there a better way? Can value be put in a constant for example? That probably would reduce executable size and increase speed.

if (j > 4)
   {
   do_something ;
   }

The compiler is more than smart enough to inline the value 4 as a constant in program memory. But for readability and maintenance this is better:

const byte value = 4;
...
if (j > value)
   {
   do_something ;
   }

Again, the compiler will recognize and inline the constant.

This works, but is slow and inefficent

That depends on where and how often you do it. We still don't know. We don't even know what sort of variable value is. It should be a byte, but only you know.

Garbage in, garbage out. To get good answers, you have to ask a good question.

SamBrownADK:
This works, but is slow and inefficent.

On what quantitative analysis are you basing that statement?

Can value be put in a constant for example? That probably would reduce executable size and increase speed.

'const' variables must be assigned a value at compile time. Not very useful if you want to assign them from EEPROM at run time.

You could put all your variables into a 'struct' and populate the whole thing at once with one call to
EEPROM.get()

gfvalvo:
Not very useful if you want to assign them from EEPROM at run time.

I asked about that in this or the other thread the OP started, and never got a clear answer.

'const' variables must be assigned a value at compile time. Not very useful if you want to assign them from EEPROM at run time.

Thats kind of what i was hoping to do but I assume it's not really going to work. My method (#8) above is stable and does work. I was just hoping to make it work better. I will live with it for now.

Thanks all for the help.

Thanks all for the help.

You might have got more help if you had posted your code and answered the questions posed.

SamBrownADK:
'const' variables must be assigned a value at compile time. Not very useful if you want to assign them from EEPROM at run time.

no, that isn't quite right, though it is true for global const variables.

SamBrownADK:
Thats kind of what i was hoping to do but I assume it's not really going to work. My method (#8) above is stable and does work. I was just hoping to make it work better. I will live with it for now.

your method (#8) doesn't demonstrate (to me) the reason you are using EEPROM. I assume you are saving to EEPROM at run-time somewhere, but you didn't show that.

BulldogLowell:

It was a conceptual question. I have no problem accessing the EEPROM and extracting a int at a specific location, that int can then be used in a if ( ) statement of what ever branched logic. It works and it has no problem.

I was just hoping to have it put to a constant so as to use less space and thus make the program faster.

I have read constants are faster, more efficient and take up less space. but since I have not been able to take a value stored in an EEPROM byte and put it to a constant I don't know if this is true or not. So... I have been trying to find a way to do it (take a value from an EEPROM address, a 8 bit number stored at that address 0 -255).

At this point I suspect it is not a viable option. So the program works... and that's that.

SamBrownADK:
I was just hoping to have it put to a constant so as to use less space and thus make the program faster.

Since the data in EEPROM must be retrieved (as you did) you only pay for that once if you store it into a global variable. Accessing that global (as you have done in your comparison) is quite fast. Since you are storing in a mutable object, then likewise you pay for the write time if and when you store it back to EEPROM.

Wha't happening (or even not happening) that makes your program seem to need speeding up the execution?

All these questions (and no code posted) makes us naturally curious... :wink:

I have no problem accessing the EEPROM and extracting a int at a specific location

Interestingly that is not what your code in reply #8 does.