how to change this code with arduino uno internal eeprom

const int a=3;
const int b=4;
const int c=5;
const int d=6;
const int e=7;

const int buttonPin = 8;
int no = 0;
int buttonState = 0;
int lastButtonState = 0;

void setup() {

pinMode(buttonPin, INPUT);
pinMode(a, OUTPUT);
pinMode(b, OUTPUT);
pinMode(c, OUTPUT);
pinMode(d, OUTPUT);
pinMode(e, OUTPUT);

}
void loop(){

buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState)
{
if (buttonState == HIGH) {
no++;
}

else {
}
}
lastButtonState = buttonState;
////////////////////////
switch(no)
{
case 0:
digitalWrite(a, HIGH);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, LOW);

break;

case 1:
digitalWrite(a, LOW);
digitalWrite(b, HIGH);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, LOW);

break;

case 2:
digitalWrite(a, LOW);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, LOW);
digitalWrite(e, LOW);

break;
case 3:
digitalWrite(a, LOW);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, LOW);

break;
case 4:
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, HIGH);

break;

default: no=0; break; } }

please help to update code with eeprom

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. If your browser doesn't show the posting toolbar then you can just manually add the code tags:
[code]``[color=blue]// your code is here[/color]``[/code]
Using code tags and other important information is explained in the How to use this forum post. Please read it.

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read. If you're using the Arduino Web Editor you will not have access to this useful tool but it's still unacceptable to post poorly formatted code. I recommend you to use the standard IDE instead.

Please remove unnecessary blank lines from your code before posting to the forum. One or two to separate code into logical sections is fine but large spaces for no reason or random blank lines just make for more scrolling when we're trying to read your code. Do not post double spaced code.

RAJNIK:
please help to update code with eeprom

There's absolutely no way we could help you based on such little information.

please help to update code with eeprom

My ouiji board is on the fritz. YOU will have to tell us what you want to store in EEPROM, when you want to store in, and why you want to store it.

The only thing in your code that I can see might be appropriate is no, but I can't see why you would need to persist that over power outages.

i want to store led state . & by using this program i will make a special gate control system

Are we to infer that a, b, c, d and e are LEDs and that you want to store their values when you change their state and in the event of a power failure or reset resume with the previously save states ?

First declare all of your variables as byte instead of int as none of then will ever be larger than 255

Do you know how to read from the EEPROM and write to it ?
If not then look at the examples.

Read the values from EEPROM into your a .. e variables (horrible names) in setup(). Then when you change any of them in the program write them to EEPROM. Only do that when they change or use EEPROM.update() to avoid writing to EEPROM too many times.

i want to store led state

Why? The states are defined by the value of no. Store one byte, and set the state of the LEDs based on that one byte, just as you do today.

After arduino reset again program start from first

#include <EEPROM.h>
#define cella 0
int a=3;
int b=4;
int c=5;
int d=6;
int e=7;

int buttonPin = 8;    
int no = 0;
int buttonState = 0;  
int lastButtonState = 0;
 
void setup() {
buttonState = EEPROM.read( cella ); 
pinMode(buttonPin, INPUT); 
pinMode(a, OUTPUT);
pinMode(b, OUTPUT);
pinMode(c, OUTPUT);
pinMode(d, OUTPUT);
pinMode(e, OUTPUT);

}
void loop(){

  buttonState = digitalRead(buttonPin);
   if (buttonState != lastButtonState)
   {   
    if (buttonState == HIGH) {
     no++;
       
   EEPROM.write( cella,no );
    }
 
    else {
    }
  }
lastButtonState = buttonState;
////////////////////////
switch(no)
  {
    case 0:
    digitalWrite(a, HIGH);
    digitalWrite(b, LOW);
    digitalWrite(c, LOW);
    digitalWrite(d, LOW);
    digitalWrite(e, LOW);
    
    break;
 
        case 1:
    digitalWrite(a, LOW);
    digitalWrite(b, HIGH);
    digitalWrite(c, LOW);
    digitalWrite(d, LOW);
    digitalWrite(e, LOW);
    
    break;
 
        case 2:
    digitalWrite(a, LOW);
    digitalWrite(b, HIGH);
    digitalWrite(c, HIGH);
    digitalWrite(d, LOW);
    digitalWrite(e, LOW);
   
    break;
        case 3:
    digitalWrite(a, LOW);
    digitalWrite(b, HIGH);
    digitalWrite(c, HIGH);
    digitalWrite(d, HIGH);
    digitalWrite(e, LOW);
   
    break;
        case 4:
    digitalWrite(a, LOW);
    digitalWrite(b, LOW);
    digitalWrite(c, LOW);
    digitalWrite(d, LOW);
    digitalWrite(e, HIGH);
    
    break;
 
 default: no=0; break; } }
 buttonState = EEPROM.read( cella );

You should be reading the value of no not buttonState

still it does not work :cry: :cry: :cry:

#include <EEPROM.h>
#define cella 0
int a=3;
int b=4;
int c=5;
int d=6;
int e=7;

int buttonPin = 8;    
int no = 0;
int buttonState = 0;  
int lastButtonState = 0;
 
void setup() {
no = EEPROM.read( cella ); 
pinMode(buttonPin, INPUT); 
pinMode(a, OUTPUT);
pinMode(b, OUTPUT);
pinMode(c, OUTPUT);
pinMode(d, OUTPUT);
pinMode(e, OUTPUT);
EEPROM.write( cella,a);
EEPROM.write( cella,b);
EEPROM.write( cella,c);
EEPROM.write( cella,d);
EEPROM.write( cella,e);

}
void loop(){

  buttonState = digitalRead(buttonPin);
   if (buttonState != lastButtonState)
   {   
    if (buttonState == HIGH) {
     no++;
     EEPROM.write(cella,no);  

    }
 
    else {
    }
  }
lastButtonState = buttonState;
////////////////////////
switch(no)
  {
    case 0:
    digitalWrite(a, HIGH);
    digitalWrite(b, LOW);
    digitalWrite(c, LOW);
    digitalWrite(d, LOW);
    digitalWrite(e, LOW);
    
    break;
 
        case 1:
    digitalWrite(a, LOW);
    digitalWrite(b, HIGH);
    digitalWrite(c, LOW);
    digitalWrite(d, LOW);
    digitalWrite(e, LOW);
    
    break;
 
        case 2:
    digitalWrite(a, LOW);
    digitalWrite(b, HIGH);
    digitalWrite(c, HIGH);
    digitalWrite(d, LOW);
    digitalWrite(e, LOW);
   
    break;
        case 3:
    digitalWrite(a, LOW);
    digitalWrite(b, HIGH);
    digitalWrite(c, HIGH);
    digitalWrite(d, HIGH);
    digitalWrite(e, LOW);
   
    break;
        case 4:
    digitalWrite(a, LOW);
    digitalWrite(b, LOW);
    digitalWrite(c, LOW);
    digitalWrite(d, LOW);
    digitalWrite(e, HIGH);
    
    break;
 
 default: no=0; break; } }

Why is no an int when its value will never be more than 255 ? Make it a byte

Note also that EEPROM.Read() returns a byte not an int so making no a byte makes even more sense.

Have you tried printing the value of no after reading it from the EEPROM ?

I'm having an issue saving and reading to the EEPROM.
The code does not show any errors but not save data to eeprom

#include <EEPROM.h>
#define cella  0
byte a=3;
byte b=4;
byte c=5;
byte d=6;
byte e=7;

byte buttonPin = 8;    
byte no = 0;
byte buttonState = 0;  
byte lastButtonState = 0;
 
void setup() {
 
pinMode(buttonPin, INPUT); 
pinMode(a, OUTPUT);
pinMode(b, OUTPUT);
pinMode(c, OUTPUT);
pinMode(d, OUTPUT);
pinMode(e, OUTPUT);

 no = EEPROM.read( cella );


}
void loop(){
   
  buttonState = digitalRead(buttonPin);
   if (buttonState != lastButtonState)
   {   
    if (buttonState == HIGH) {
     no++;
     
    }
 
    else {
    }
  }

lastButtonState = buttonState;
  
////////////////////////

switch(no)

  {
    case 0:
    digitalWrite(a, HIGH);
    digitalWrite(b, LOW);
    digitalWrite(c, LOW);
    digitalWrite(d, LOW);
    digitalWrite(e, LOW); 
    
    break;
 
        case 1:
    digitalWrite(a, HIGH);
    digitalWrite(b, HIGH);
    digitalWrite(c, LOW);
    digitalWrite(d, LOW);
    digitalWrite(e, LOW);
  
    break;
 
        case 2:
    digitalWrite(a, HIGH);
    digitalWrite(b, HIGH);
    digitalWrite(c, HIGH);
    digitalWrite(d, LOW);
    digitalWrite(e, LOW);
  
   
   
    break;
        case 3:
    digitalWrite(a, HIGH);
    digitalWrite(b, HIGH);
    digitalWrite(c, HIGH);
    digitalWrite(d, HIGH);
    digitalWrite(e, LOW);
    
    break;
        case 4:
    digitalWrite(a, HIGH);
    digitalWrite(b, HIGH);
    digitalWrite(c, HIGH);
    digitalWrite(d, HIGH);
    digitalWrite(e, HIGH);
   
    break;
 
    default: no=0; 

    EEPROM.update(cella, no);

 break;
 

  }

 }

Use the auto-format in the Arduino editor to organise that code a bit better.

The only place you write to the EEPROM is immediately after setting no=0; so it won't ever write any other value, will it?

NOT WORK :cry: :cry:

#include <EEPROM.h>
#define cella  0
byte a = 3;
byte b = 4;
byte c = 5;
byte d = 6;
byte e = 7;

byte buttonPin = 8;
byte no = 0;
byte buttonState = 0;
byte lastButtonState = 0;

void setup() {

  pinMode(buttonPin, INPUT);
  pinMode(a, OUTPUT);
  pinMode(b, OUTPUT);
  pinMode(c, OUTPUT);
  pinMode(d, OUTPUT);
  pinMode(e, OUTPUT);
  no = digitalRead(cella);

}
void loop() {


  if (buttonState != lastButtonState)
  {
    if (buttonState == HIGH) {
      no++;

    }


  }

  lastButtonState = buttonState;

  ////////////////////////

  switch (no)

  {
    case 0:
      digitalWrite(a, HIGH);
      digitalWrite(b, LOW);
      digitalWrite(c, LOW);
      digitalWrite(d, LOW);
      digitalWrite(e, LOW);
      EEPROM.update(cella, no);
      break;

    case 1:
      digitalWrite(a, HIGH);
      digitalWrite(b, HIGH);
      digitalWrite(c, LOW);
      digitalWrite(d, LOW);
      digitalWrite(e, LOW);
      EEPROM.update(cella, no);
      break;

    case 2:
      digitalWrite(a, HIGH);
      digitalWrite(b, HIGH);
      digitalWrite(c, HIGH);
      digitalWrite(d, LOW);
      digitalWrite(e, LOW);
      EEPROM.update(cella, no);

      break;
    case 3:
      digitalWrite(a, HIGH);
      digitalWrite(b, HIGH);
      digitalWrite(c, HIGH);
      digitalWrite(d, HIGH);
      digitalWrite(e, LOW);
      EEPROM.update(cella, no);
      break;

    case 4:
      digitalWrite(a, HIGH);
      digitalWrite(b, HIGH);
      digitalWrite(c, HIGH);
      digitalWrite(d, HIGH);
      digitalWrite(e, HIGH);
      EEPROM.update(cella, no);
      break;
    default: no = 0;
      break;
  }
}

Luckily you used update() and not write() otherwise the EEPROM could be toast.

update() the value in the EEPROM when you change no, not each time through loop()

Where do you read the value from EEPROM ?

OK, a little better. But now you don't update the EEPROM when no is set to zero. Why not just put one update after the switch()?

You never read the button either. It looks like your digitalRead() and your EEPROM.read() got mixed up.

now this code working fine :slight_smile: :slight_smile: :slight_smile:

thank you MorganS, UKHeliBob & PaulS

#include <EEPROM.h>
#define cella  0
byte a=3;
byte b=4;
byte c=5;
byte d=6;
byte e=7;

byte buttonPin = 8;    

byte buttonState = 0;  
byte lastButtonState = 0;
 byte no = 0;
void setup() {
 
pinMode(buttonPin, INPUT); 
pinMode(a, OUTPUT);
pinMode(b, OUTPUT);
pinMode(c, OUTPUT);
pinMode(d, OUTPUT);
pinMode(e, OUTPUT);
 no = EEPROM.read(cella);

}
void loop(){
 
   
  buttonState = digitalRead(buttonPin);
   if (buttonState != lastButtonState)
   {   
    if (buttonState == HIGH) {
     no++;
     EEPROM.write(cella, no);
    }
 
    else {
    }
  }

lastButtonState = buttonState;
  
////////////////////////

switch(no)

  {
    case 0:
    digitalWrite(a, HIGH);
    digitalWrite(b, LOW);
    digitalWrite(c, LOW);
    digitalWrite(d, LOW);
    digitalWrite(e, LOW); 
    
    break;
 
        case 1:
    digitalWrite(a, HIGH);
    digitalWrite(b, HIGH);
    digitalWrite(c, LOW);
    digitalWrite(d, LOW);
    digitalWrite(e, LOW);
  
    break;
 
        case 2:
    digitalWrite(a, HIGH);
    digitalWrite(b, HIGH);
    digitalWrite(c, HIGH);
    digitalWrite(d, LOW);
    digitalWrite(e, LOW);
  
   
   
    break;
        case 3:
    digitalWrite(a, HIGH);
    digitalWrite(b, HIGH);
    digitalWrite(c, HIGH);
    digitalWrite(d, HIGH);
    digitalWrite(e, LOW);
    
    break;
        case 4:
    digitalWrite(a, HIGH);
    digitalWrite(b, HIGH);
    digitalWrite(c, HIGH);
    digitalWrite(d, HIGH);
    digitalWrite(e, HIGH);
  
   
    break;

     
  default: no=0; 

 break;
 

  }

 }

That code never writes 0 to the EEPROM. Is that what you want?