How do you get EPROM read and write to work?

I am trying to save the led state (ON or OFF) when the power is removed and reapplied. Cant seem to get the EEprom read write working. PLEASE HELP!

This code takes the Arduino IR remote and controls individual LED’s on/off and PWM on the all pins.
It is an RC Boat Navigation light control system but could be used for turning on and and dimming just about anything with any remote.

nav lights.txt (22.2 KB)

Post the code that you have tried.
Have you looked at the examples in the IDE ?

Yes I Have set up the code the best i could understand from the IDE.

  • Where are you loading the saved values? The section at line number 84 is commented out.
  • Do you ever declare the value of address[0-9]? If you don't define them, the values of them are semi-random. I'd change the section at line number 58 to:
int address0 = 0;
int address1 = 1;
int address2 = 2;
int address3 = 3;
int address4 = 4;
int address5 = 5;
int address6 = 6;
int address7 = 7;
int address8 = 8;
int address9 = 9;

So i tried that and all it does is load those numbers into the address locations, it does not store the current state of the remote in those locations.

I am trying to save the led state (ON or OFF) when the power is removed and reapplied.

So what hardware do you have that gives you enough warning to allow you to save the current states of the lights when he power is removed?

EEPROM memory has a limit on the number of write cycles it can use and a poor code could trash it in a few seconds.

That code you posted looks way too verbose to follow. You could reduce it to about 30 lines if you understood arrays and loops.

lights and Grumpy_Mike have identified the major problems, but I also found this:

long previousMillis = 0;        // stores last time LED was updated
long interval = 1000;           // interval at which to blink (milliseconds)

unsigned long

How are you testing if the numbers are saved? You don't have anything reading them.

Grumpy_Mike: your right I have limited knowledge of loops and arrays and defiantly could do this better. I set it up so it was the easiest for me to follow, which i know is not the best. I am completely open to any suggestions though. This took me about 8 hours to write.

Jiggy-Ninja: I left that in due to every time i removed it I got errors in compiling.

Lights0123: At the end of the code there is a serial.println that displays the if it was written, it has yet to be written, i get all zeros.

nav lights.txt (22.2 KB)

//void saveConfig(){
//  int LedPin0state = EEPROM.write(LedPin0state, address0);
//  int LedPin1state = EEPROM.write(LedPin1state,address1);
//  int LedPin2state = EEPROM.write(LedPin2state,address2);
//  int LedPin3state = EEPROM.write(LedPin3state,address3);
//  int LedPin4state = EEPROM.write(LedPin4state,address4);
//  int LedPin5state = EEPROM.write(LedPin5state,address5);
//  int LedPin6state = EEPROM.write(LedPin6state,address6);
//  int LedPin7state = EEPROM.write(LedPin7state,address7);
//  int LedPin8state = EEPROM.write(LedPin8state,address8);
//  int LedPin9state = EEPROM.write(LedPin9state,address9);
//}

The syntax your sketch is backwards, and the assignement of int LedPinXstate to the EEPROM.write is wrong as well.

  EEPROM.write(addr, val);

OK so I understand that it is not correct, i just not sure what is. Still cant get it to write anything to eeprom. the code is different now just has the same name

nav lights.txt (22.1 KB)

cattledog:

//void saveConfig(){

//  int LedPin0state = EEPROM.write(LedPin0state, address0);
//  int LedPin1state = EEPROM.write(LedPin1state,address1);
//  int LedPin2state = EEPROM.write(LedPin2state,address2);
//  int LedPin3state = EEPROM.write(LedPin3state,address3);
//  int LedPin4state = EEPROM.write(LedPin4state,address4);
//  int LedPin5state = EEPROM.write(LedPin5state,address5);
//  int LedPin6state = EEPROM.write(LedPin6state,address6);
//  int LedPin7state = EEPROM.write(LedPin7state,address7);
//  int LedPin8state = EEPROM.write(LedPin8state,address8);
//  int LedPin9state = EEPROM.write(LedPin9state,address9);
//}




The syntax your sketch is backwards, and the assignement of int LedPinXstate to the EEPROM.write is wrong as well.



EEPROM.write(addr, val);

That part is commented out and is not used.

Halocline:
Lights0123: At the end of the code there is a serial.println that displays the if it was written, it has yet to be written, i get all zeros.

You're just printing the addresses: they're 0, and each one needs to be separate. Change lines 58-67 to:

int address0 = 0;
int address1 = 1;
int address2 = 2;
int address3 = 3;
int address4 = 4;
int address5 = 5;
int address6 = 6;
int address7 = 7;
int address8 = 8;
int address9 = 9;

Also, change the EEPROM.put back to EEPROM.write, or even better, just remove it and rely on hitting the forwards button.

Did I miss something.
Where do you initialize these addresses?
int address0 ;
int address1 ;
int address2 ;
int address3 ;
int address4 ;
int address5 ;
int address6 ;
int address7 ;
int address8 ;
int address9 ;

Remember there are 100,000 write cycles for a EEPROM location.
This can be easily reached if your sketch is not written properly.

.

OK I did what you suggested, the code is attached but still nothing saves. Also Based on the 100k limit I made the forward button a save button, to reduce this, that I was hoping would save the values to the eprom, nope.

Not sure what you mean bye the values aren’t declared as I thought that what the INT is?

ir2b.ino (21.5 KB)

int address0 = 0;
int address1 = 0;
int address2 = 0;
int address3 = 0;
int address4 = 0;
int address5 = 0;
int address6 = 0;
int address7 = 0;
int address8 = 0;
int address9 = 0;

These are all set to zero.
You are missing the point of an address.
They should be unique. 0,1,2,3 etc. see lights0123 last post.

.

To add to what LarryD said, int is the type. The values are 0.

--Michael

Think of them like house addresses: they should be unique. If Bob's address is 0 Example St., and Alice's address is 0 Example St., they're both going to be in the same house - it's not going to work, as there would be 2 houses overlapping. Instead, Bob's house should be 0 Example St., and Alice's house should be 1 Example St. The same thing applies to memory addresses - you can't save multiple things to the same location.

OK now that is making way more sense, regarding the address’s. I have made the change, and almost completely understand why. Nice guys,

So with them each having the corresponding value 0-9, how does it not think that they are equal to those values and assign them automatically 0-9 upon power up?

nav lights5.txt (22.2 KB)

So with them each having the corresponding value 0-9, how does it not think that they are equal to those values and assign them automatically 0-9 upon power up?

You are confusing the address and the information stored at the address.