[SOLVED] EEProm using bit shift example

I'm having trouble I started out using unsigned int for FR_INTERVAL and decided to change it to long. I made sure that it was declared as long but every time I try to go over 65535, I get randomness as if it were declared as unsigned int.

I'm quite certain I'm doing something dumb but I just can't see it.

I was using my own schema but abandon it only to find out the schema from the example on the arduino web site produced the same results.
see the 3 file attachment

eeprom.ino (20.8 KB)

eetst.ino (6.4 KB)

menu.ino (14.8 KB)

//This function will write a 4 byte (32bit) long to the eeprom at
//the specified address to adress + 3.
void EEPROMWritelong(int address, 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.write(address, four);
      EEPROM.write(address + 1, three);
      EEPROM.write(address + 2, two);
      EEPROM.write(address + 3, one);
}

//This function will return a 4 byte (32bit) long from the eeprom
//at the specified address to adress + 3.
long EEPROMReadlong(long address)
{
      //Read the 4 bytes from the eeprom memory.
      long four = EEPROM.read(address);
      long three = EEPROM.read(address + 1);
      long two = EEPROM.read(address + 2);
      long one = EEPROM.read(address + 3);

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

Why don't use EEPROM.put(address,long) and EEPROM.get(address,long)? (EEPROM library functions)

Simples test:

#include <EEPROM.h>

long myNumber1 = 123456;
#define EEPROM_myNumber1 0

long myNumber2 = -78987;
#define EEPROM_myNumber2 EEPROM_myNumber1+sizeof(myNumber1)

void setup() {
  Serial.begin(115200);
  Serial.println("Begin");
  EEPROM.put(EEPROM_myNumber1,myNumber1);
  EEPROM.put(EEPROM_myNumber2,myNumber2);
  myNumber1 = 0;
  myNumber2 = 0;
  EEPROM.get(EEPROM_myNumber1,myNumber1);
  EEPROM.get(EEPROM_myNumber2,myNumber2);
  Serial.println(myNumber1);
  Serial.println(myNumber2);
}

void loop() {
  // put your main code here, to run repeatedly:
}

I may use that in the future but right now I'm trying to figure out what and where this bug is? Does anybody else have a suggestion.

Try out!

In the place of this line:

FR_INTERVAL = EEPROMReadlong(26);

Use:

EEPROM.get(26,FR_INTERVAL);

And in the place of this:

EEPROMWritelong(26, FR_INTERVAL);

Use:

EEPROM.put(26,FR_INTERVAL);

See if it works.

Unfortunately no, it didn't work I can still only go up to 65535, if I do 65536 I get 0

does it need to be unsigned long int FR_INTERVAL; or unsigned long FR_INTERVAL; it shouldn't make a difference...this is one of those things that is sticking in my craw....LOL

Please, use the codes below to DEBUG the FR_INTERVAL.

Put them in the LoadSettings() and SaveSettings() right where you get and write to EEPROM.

EEPROM.get(26,FR_INTERVAL);
Serial.print("Getting FR_INTERVAL = ");
Serial.println(FR_INTERVAL);
Serial.print("Writing FR_INTERVAL = ");
Serial.println(FR_INTERVAL);
EEPROM.put(26,FR_INTERVAL);

What Serial Output are you getting?

I get 0 in both cases when I go above 65535

The only part of the code I could see you assigning a value to FR_INTERVAL (other than EEPROM.get()) is the:

void drawFreeSetup()
{
  unsigned int idx;

      /* ... CODE ... */

      idx=readLine();
      FR_INTERVAL = idx;
}

This is assigning a unsigned int to unsigned long. Max value o unsigned int = 65535, so the FR_INTERVAL will never be greater than 65535.

Change idx to unsigned long and test again.

Another thing:
The function readLine() returns an unsigned int, change it to unsigned long too (maybe you will have to modify the function logic to work with unsigned long).

Thanks....I knew it was something dumb I was doing

How do I mark this thread as solved

There isn't really the need, but you can change the thread title, by clicking Modify in the First Post right-bottom corner. Add [SOLVED] to it.

Thanks for the help and pointing out the error of my ways, I'll know next time when I see this problem to check back through the code to make sure all the vars that touch it get modified as well