# HOW TO USE EE.PROM (READ AND WRITE) TO VALUES GREATER THAN 255

Hello, everyone!!

I have been developing a code to use in a Solar Tracker Project. I’m using a switch to avoid solar panel going through an certain angle value (maximum angle). So, I would like to store some specific values on the EE.PROM memory, like the values to mark where the switch is positioned. However, some of these values are greater than 255 (for example: 813).
How do I wrote this value on EEPROM and read this same value after? Is there a routine to do this?

The set values that should be store is:
const float anguloesquerdomaximoV = 210;
const float angulodireitomaximoV = 813;
const float anguloesquerdomaximoH = 210;
const float angulodireitomaximoH = 813;

They are used in the function Homeposition and calibracaorastreamento in the code attached.

sketch_mar03a.ino (32.9 KB)

813 = 0000‭0011 00101101‬

store 00101101 in one byte address and 0000‭0011 into the other byte address.

Maybe this helps

You need to make sure you keep track of the addresses and the length of the different datatypes.

This is how I do it

``````//EEPROM_LONG//
void EEPROMWritelong(int address, unsigned long value)
{
//Decomposition from a long to 4 bytes by using bitshift.
//One = Most significant -> Four = Least significant byte
byte four = (value & 0xFF);
byte three = ((value >> 8) & 0xFF);
byte two = ((value >> 16) & 0xFF);
byte one = ((value >> 24) & 0xFF);

//Write the 4 bytes into the eeprom memory.
EEPROM.commit();
}

//This function will return a 4 byte (32bit) long from the eeprom
{
//Read the 4 bytes from the eeprom memory.

//Return the recomposed long by using bitshift.
return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF) + ((two << 16) & 0xFFFFFF) + ((one << 24) & 0xFFFFFFFF);
}

//END//
/code]

Then the syntax to read or write

[code]
``````

Just remember to allow the necessary space in your address to accommodate the long, so I always start with 0, then 5, then 10 ect…

Thank you so much for everyone that answer my question.

Proietti, where the function VOID EEPROMWritelong (int address, unsigned long value) is put? Is it in the setup, on the loop or in a separated function?

Another question: how do I acess this read value (in the function unsigned long EEPROMReadlong (long address) to use it in another functions in my code, like calibracaorastreamento and Homeposition?

Is it possible to create another variable to concatenate all the four variables used in a single variable, instead of using return?

Is //EEPROM_LONG// a library?

Use put()

Aarg and Klaus_K, good evening!

I think I would be a problem to use put in my case, because I didn't want to update the value that I was supposed to record previously. When I read the EEPROM.put definition, this problem comes up.

What do you think?

luciocr:
I think I would be a problem to use put in my case, because I didn't want to update the value that I was supposed to record previously.

You need to explain this. If I was updating the end limit for a solar tracker, I wouldn't care at all what the old value was...

@luciocr, the void EEPROMWritelong() and the void EEPROMReadlong() is created as separate functions as I have posted. When you want to write or read a long you just you just write

``````//global variable//
unsigned long svalue;

void loop()
{
if (sensorhaschanged == 1)
{
EEPROMWritelong(0, sensorValue);
}
else
{
}
}
/code]

The plus side of this code is you can even write floats
``````

Is //EEPROM_LONG// a library?
/quote]

No it's not a library as fas I know. Try it it works.

Proietti:
@luciocr, the void EEPROMWritelong() and the void EEPROMReadlong() is created as separate functions as I have posted. When you want to write or read a long you just you just write

``````//global variable//
``````

unsigned long svalue;

void loop()
{
if (sensorhaschanged == 1)
{
EEPROMWritelong(0, sensorValue);
}
else
{
}
}
/code]

The plus side of this code is you can even write floats

But, is it possible to do something like this:

``````//global variable//
unsigned long svalue;
int comparative = 100;

void loop()
{
if (sensorhaschanged == 1)
{

if (svalue > comparative)
{

do something
}
}

/code]
``````

I suppose you could do that but would it not be simpler to do this

``````[/
//global variable//
unsigned long svalue;
int comparative = 100;

void loop()
{
if (sensorhaschanged == 1)
{

}

if (svalue > comparative)
{

do something
}
}code]

saves you writing writing 5 lines of code just for a reading? What I normally do is create a new tab in my sketch called EEPROMLONG and I just paste the EEPROMWritelong() & EEPROMReadlong() there so it is out of the way.
``````

Proietti:
I suppose you could do that but would it not be simpler to do this

``````[/
``````

//global variable//
unsigned long svalue;
int comparative = 100;

void loop()
{
if (sensorhaschanged == 1)
{

}

if (svalue > comparative)
{

do something
}
}code]

saves you writing writing 5 lines of code just for a reading? What I normally do is create a new tab in my sketch called EEPROMLONG and I just paste the EEPROMWritelong() & EEPROMReadlong() there so it is out of the way.

My friend, Proietti! So, in this case, how should new tab be used to calculate a difference between a single value not stored in EEPROM (in this case, potValue) and these values stored in EEPROM (in this case, the array leituraswitchextremo)?

What I would like to do is the following:

• Calculate the diference between the potentiometer reading (potValue) and the
• Before calculate this diference, I want to compare it with a certain value and if it happens, the code will do something.

The part of the code is below:

``````[/

// just the part used to declare variables that need to be stored in EEPROM

const float anguloesquerdomaximoV = 210;
const float angulodireitomaximoV = 813;
const float anguloesquerdomaximoH = 210;
const float angulodireitomaximoH = 813;
int leituraswitchextremo={{anguloesquerdomaximoV, angulodireitomaximoV},{anguloesquerdomaximoH, angulodireitomaximoH}}; - this array is used to store the values from both movement axis and for both sides of the same axis.

// function used to calibrate the switch with the set value stored in EEPROM if there are a diference between potentiometer reading and the set value stored (from leituraswitchextremo

void Homeposition (){

potValue = analogRead(PinoPotEixo[eixoatual]); //faz a leitura inicial do potenciometro
swLeitura [nEixo] = digitalRead(pinoswitch [eixoatual]); //faz a leitura do pino switch
int difleiturapot = abs(potValue - leituraswitchextremo[eixoatual][dir(eixoatual)]); //calcula a diferença entre a leitura atual do pino switch e o valor pré-determinado para cada eixo (vertical e horizontal) e para cada lado (esquerdo e direito)

if(difleiturapot>3){ ///caso a diferença entre as leituras for maior do que 3 bytes (valor calculado para o erro de 1º)

leituraswitchextremo[eixoatual][dir] = potValue; // substitui a leitura do pino switch atual pelo valor pré-estabelecido
}
}
``````

Sorry I am struggling to understand exactly what it is you want. Within you sketch in Arduino IDE top right hand corner you can create tabs. This is useful to separate and tidy your code, especially if it is a generic code which does not require any further changing. I use a lot of tabs to arrange my code so its easier to work with. One of them will be the EEPROM tab where I would put the void EEPROMReadlong(intaddress, unsigned long value) & the unsigned long EEPROMReadlong(long address). Then in my main code tab (where all the works is done) I use the syntax EEPROMWritelong(address, value to write); & EEPROMReadlong(address);. The 2 syntaxes stated above replace the standard EEPROM.write & EEPROM.read. So in exactly the same way you would use the standard EEPROM commands but using my long versions. Just be aware though that when using longs you have to allocate the appropriate memory space so I tend to use addresses 0, 5, 10, 15...ect.
By the way I had a quick look at your sketch and noticed that within you loop you have a delay of 60000! You do realise this will stop entire sketch for 1 minute...nothing can happen during this delay.