how to use eeprom on arduino nano

can you help me… im a beginner…
how to use eeprom on adruino nano,

here my code:

#include <EEPROM.h>
int sw1 = 2; // switch is connected to pin 2
int ld1 = 8;
int ledng = 13;

int reset = 3;
int sw2 = 4;
int ld2 = 9;

int ledngState;
int vala;
int vala2;
int buttonState2;
int Mode2 = 0;
int val; // variable for reading the pin status
int val2; // variable for reading the delayed status
int buttonState; // variable to hold the button state
int Mode = 0; // What mode is the light in?

void setup() {
pinMode(sw1, INPUT_PULLUP ); // Set the switch pin as input
pinMode(reset, INPUT_PULLUP );
pinMode(ld1, OUTPUT);
pinMode(ledng, OUTPUT);

pinMode(sw2, INPUT_PULLUP );
pinMode(ld2, OUTPUT);

buttonState = digitalRead(sw1); // read the initial state
buttonState2 = digitalRead(sw2);
digitalWrite(ledng, ledngState);
checkledngState();

}

void loop(){
val = digitalRead(sw1); // read input value and store it in val
delay(10); // 10 milliseconds is a good amount of time
val2 = digitalRead(sw1); // read the input again to check for bounces

vala = digitalRead(sw2);
delay(10);
vala2 = digitalRead(sw2);

////////////////// SWITCH 1 ////////////////////////////////////////////////////////////

if (val == val2) { // make sure we got 2 consistant readings!
if (val != buttonState) { // the button state has changed!
if (val == HIGH) { // check if the button is pressed
if (Mode == 0) {
Mode = 1;
} else {
if (Mode == 1 ) {
Mode = 2;
} else {
if (Mode == 2 ) {
Mode = 0;
}
}
}
}
}
buttonState = val; // save the new state in our variable
}

// Now do whatever the lightMode indicates
if (Mode == 0) { // all-off
digitalWrite(ld2, LOW);

Mode = 0;
}
if (Mode == 1) { // sw1
digitalWrite(ld1, LOW);

Mode2 = 0;
digitalWrite(ld2, HIGH);

}
if (Mode == 2) { // sw2 NG
digitalWrite(ledng, HIGH);
EEPROM.update(0, ledngState);
}

//////////////////// SWITCH 2 /////////////////////////////////////////////////////////
if (vala == vala2) { // make sure we got 2 consistant readings!
if (vala != buttonState2) { // the button state has changed!
if (vala == HIGH) { // check if the button is pressed
if (Mode2 == 0) {
Mode2 = 1;
} else {
if (Mode2 == 1 ) {
Mode2 = 2;
} else {
if (Mode2 == 2 ) {
Mode2 = 0;
}
}
}
}
}
buttonState2 = vala;
}

// Now do whatever the lightMode indicates
if (Mode2 == 0 && digitalRead(ld2)==0) {
digitalWrite(ld1, HIGH);

Mode2 = 0;
}
else if (digitalRead(ld2)==1){
digitalWrite(ld1, LOW);
}
if (Mode2 == 1) { // sw1
digitalWrite(ld2, LOW);

Mode = 0;
digitalWrite(ld1, HIGH);

}
if (Mode2 == 2) { // sw2 NG
digitalWrite(ledng, HIGH);
EEPROM.update(0, ledngState);
}
///////////// RESET ///////////////////////////////
if (digitalRead(reset) == 0) {
digitalWrite(ledng,LOW);
digitalWrite(ld1,LOW);

Mode = 0;
digitalWrite(ld2,LOW);

Mode2 = 0;
}
/////////////// NO GOOD ////////////////////////////
if (digitalRead(ledng)==1) {
digitalWrite(ld1, HIGH);
digitalWrite(ld2, HIGH);
delay(500);
digitalWrite(ld1, LOW);
digitalWrite(ld2, LOW);
delay(500);
}
}
void checkledngState() {
ledngState = EEPROM.read(0);
if(ledngState == 0) {

digitalWrite(ledng, HIGH);
}
if(ledngState == 1) {

digitalWrite(ledng, LOW);
}
}

Please use code tags ( </> button ) when posting code.

What are you trying to do, and how does what the code does differ from that? It is much easier for us to spot problems if you give us an idea of what we’re looking for. And since were helping for free, you should try to make it easier on us :wink:

write / read / update work on bytes, not with other variable types. Use EEPROM.put to write to eeprom and EEPROM.get to retrieve from eeprom if you use integers, floats and other stuff.

Reported to be moved as this is not an "installation and troubleshooting" problem.

my project is making a 2 separator with led indicator and hold when ''NO GOOD''..either i turn it off still hold at ''NO GOOD''..only key switch will reset.....

i used my code on my arduino uno and it works...while on arduino nano doesnt work when i turn it off it reset..the ''NO GOOD'' doesnt hold...tnx..

Check your wiring then check it again. Check that any connections to a breadboard are actually connected and that the breadboard 5V and GND lines do not have gaps in them half way along.

As has already been pointed out EEPROM.write() writes a byte and EEPROM.read() reads a byte but ledngState, for instance, is declared as an int (2 bytes)

how to write as a code using eeprom can you give me some example..
using EEPROM...THANKS

i used that code to arduino uno and it works....
but when i use it to arduino nano doesnt work when i turn off..

geofmen:
how to write as a code using eeprom can you give me some example…
using EEPROM…THANKS

#include <EEPROM.h>

void setup()
{
  Serial.begin(115200);
  byte byteData = 0;
  int intData = 0;
  byte eepromPosition = 0;
  Serial.println("Writing bytes");
  for (int eepromCount = 0; eepromCount < 8; eepromCount++)
  {
    EEPROM.write(eepromCount, byteData);
    byteData += 10;
  }
  Serial.println("Reading bytes from saved bytes");
  for (int eepromCount = 0; eepromCount < 8; eepromCount++)
  {
    eepromPosition = eepromCount;
    Serial.print("position : ");
    Serial.print(eepromPosition);
    Serial.print(" : ");
    byteData = EEPROM.read(eepromCount);
    Serial.println(byteData);
  }
  Serial.println("\nWriting ints to 2 bytes each");
  for (int eepromCount = 0; eepromCount < 16 ; eepromCount += 2)
  {
    eepromPosition = eepromCount * 2;
    EEPROM.put(eepromPosition, intData);
    intData += 1000;
  }
  Serial.println("Reading ints");
  for (int eepromCount = 0; eepromCount < 16; eepromCount += 2)
  {
    eepromPosition = eepromCount * 2;
    Serial.print("position : ");
    Serial.print(eepromPosition);
    Serial.print(" : ");
    EEPROM.get(eepromPosition, intData);
    Serial.println(intData);
  }
  Serial.println("\nReading bytes from saved ints");
  for (int eepromCount = 0; eepromCount < 16; eepromCount++)
  {
    eepromPosition = eepromCount;
    Serial.print("position : ");
    Serial.print(eepromPosition);
    Serial.print(" : ");
    EEPROM.get(eepromPosition, intData);
    Serial.println(intData);
  }
}

void loop()
{
}

thanks…
another question…
why arduino nano can’t read EEPROM?

geofmen:
thanks....
another question..
why arduino nano can't read EEPROM?

It can.

Next question .

geofmen:
thanks....
another question..
why arduino nano can't read EEPROM?

I ran that code on a Nano before I uploaded it. No problems.

i can upload but the eeprom can't read..when i turn off the nano dont hold..

how??

Post the code that you're using to determine that the "nano dont hold".

Maybe you've already exceed the erase/write cycle limit for your EEPROM

 #include <EEPROM.h>
    int sw1 = 2;              // switch is connected to pin 2
    int ld1 = 8;
    int ledng = 13;
   
    int reset = 3;
    int sw2 = 4;
    int ld2 = 9;
   

    int ledngState;
    int vala;  
    int vala2;  
    int buttonState2;
    int Mode2 = 0; 
    int val;                        // variable for reading the pin status
    int val2;                       // variable for reading the delayed status
    int buttonState;                // variable to hold the button state
    int Mode = 0;              // What mode is the light in?
    
    void setup() {
      pinMode(sw1, INPUT_PULLUP );    // Set the switch pin as input
      pinMode(reset, INPUT_PULLUP ); 
      pinMode(ld1, OUTPUT);
      pinMode(ledng, OUTPUT);
     
      pinMode(sw2, INPUT_PULLUP );
      pinMode(ld2, OUTPUT);
    
      buttonState = digitalRead(sw1);   // read the initial state
      buttonState2 = digitalRead(sw2);
      digitalWrite(ledng, ledngState);
      checkledngState();
     
    }

    void loop(){
      val = digitalRead(sw1);      // read input value and store it in val
      delay(10);                         // 10 milliseconds is a good amount of time
      val2 = digitalRead(sw1);     // read the input again to check for bounces
     
       vala = digitalRead(sw2); 
      delay(10);
       vala2 = digitalRead(sw2);
        
      
////////////////// SWITCH 1 ////////////////////////////////////////////////////////////
      
      if (val == val2) {                 // make sure we got 2 consistant readings!
        if (val != buttonState) {          // the button state has changed!
          if (val == HIGH) {                // check if the button is pressed
            if (Mode == 0) {           
              Mode = 1;               
            } else {
                if (Mode == 1 ) {       
                Mode = 2;           
            } else {
                if (Mode == 2 ) {      
                Mode = 0;           
            } 
           }
          }
         }
        }
        buttonState = val;                 // save the new state in our variable
      }

      // Now do whatever the lightMode indicates
      if (Mode == 0) { // all-off
        digitalWrite(ld2, LOW);
       
        Mode = 0;
      }
      if (Mode == 1) { // sw1
         digitalWrite(ld1, LOW);
        
        Mode2 = 0;
        digitalWrite(ld2, HIGH);
       
        
         
      }
      if (Mode == 2) { // sw2 NG
        digitalWrite(ledng, HIGH);
        EEPROM.update(0, ledngState);
      }
      
//////////////////// SWITCH 2 /////////////////////////////////////////////////////////     
      if (vala == vala2) {                 // make sure we got 2 consistant readings!
        if (vala != buttonState2) {          // the button state has changed!
          if (vala == HIGH) {                // check if the button is pressed
            if (Mode2 == 0) {           
              Mode2 = 1;               
            } else {
                if (Mode2 == 1 ) {       
                Mode2 = 2;           
            } else {
                if (Mode2 == 2 ) {      
                Mode2 = 0;  
                    } 
           }
          }
         }
        }
        buttonState2 = vala;                 // save the new state in our variable
      }

      // Now do whatever the lightMode indicates
      if (Mode2 == 0 && digitalRead(ld2)==0) { // all-off
        digitalWrite(ld1, HIGH);
        Mode2 = 0;
      }
else if (digitalRead(ld2)==1){
  digitalWrite(ld1, LOW);
  }
      if (Mode2 == 1) { // sw1
        digitalWrite(ld2, LOW);
        Mode = 0;
        digitalWrite(ld1, HIGH);
        
         
        
      }
      if (Mode2 == 2) { // sw2 NG
        digitalWrite(ledng, HIGH);
        EEPROM.update(0, ledngState);
      }
///////////// RESET ///////////////////////////////     
      if (digitalRead(reset) == 0) {
        digitalWrite(ledng,LOW);
         digitalWrite(ld1,LOW);
        
        Mode = 0;
        digitalWrite(ld2,LOW);
        
        Mode2 = 0;
        }
/////////////// NO GOOD ////////////////////////////       
        if (digitalRead(ledng)==1) {
          digitalWrite(ld1, HIGH);
          digitalWrite(ld2, HIGH);
          delay(500);
          digitalWrite(ld1, LOW);
          digitalWrite(ld2, LOW);
          delay(500);  
          }
    }
/////////////// HOLD ////////////////////////////////    
void checkledngState() {
   ledngState = EEPROM.read(0);
   if(ledngState == 0) {
    
    digitalWrite(ledng, HIGH);
   } 
   if(ledngState == 1) {
    
    digitalWrite(ledng, LOW);
   }
    }

Please always do an Auto Format (Tools > Auto Format in the Arduino IDE or Ctrl + B in the Arduino Web Editor) on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read.

There is way more code there than would be necessary to determine whether the EEPROM is holding values. You need to check this by writing a minimal code of only a handful of lines, with all the extraneous stuff left off.

What happens if you run the test code in reply #7 ?
Please post the output