how to write the case to EEPROM..??

I wrote a code which blinks a LED and a push button connected to Pin 2 of digital by pressing which the Pattern of the led can be changed..

Now i want to disconnect the arduino and re power it but stay in the same pattern it was last flashing on...??

How do I write it to the EEPROM and read it every time the arduino is repowered...??

the code is here...

int buttonPin = 2;
int buttonState = 0;
int i = 0;
int ledPin = 13;



void setup(){
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
  
}

void loop(){
  buttonState = digitalRead(buttonPin);
  

 if (buttonState == HIGH) {
     digitalWrite(ledPin, HIGH);
     i ++;
     if ( i > 3){
       i = 0;
     }
 }

  else {
    digitalWrite(ledPin, LOW);
    
  }
  
  switch (i){
    case 0:
  digitalWrite(13, HIGH);  
  delay(30);              
  digitalWrite(13, LOW);  
  delay(175);             
  digitalWrite(13, HIGH);  
  delay(30);             
  digitalWrite(13, LOW);  
  delay(1000);             
     break;

    case 1:
  digitalWrite(13, HIGH); 
  delay(100);            
  digitalWrite(13, LOW);   
  delay(175);             
  digitalWrite(13, HIGH);  
  delay(100);              
  digitalWrite(13, LOW);   
  delay(1000);             
     break;

    case 2:
  digitalWrite(13, HIGH); 
  delay(30);             
  digitalWrite(13, LOW); 
  delay(1000);             
     break;

    case 3:
  digitalWrite(13, HIGH); 
  delay(100);             
  digitalWrite(13, LOW);  
  delay(1000);             
     break;
  }
  Serial.print(i);
    Serial.print("\n");

  
}

How do I write it to the EEPROM and read it every time the arduino is repowered...??

It is the value in i that you want to save. I'd recommend a better name for that variable, since it has a lot of significance to the program.

It's range of values (0 to 3) is such that it will easily fit in a byte, which is easier to write to/read from EEPROM.

How to use EEPROM:
http://arduino.cc/en/Reference/EEPROM

I tried something like this..
But its not working...

int buttonPin = 2;
int buttonState = 0;
int b = 0;
int ledPin = 13;
int value;
int i;
#include <EEPROM.h>

void setup(){
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);


  
  for (i = 0; i <= 1; i++){
  EEPROM.write(i, b);
}}

void loop(){
  buttonState = digitalRead(buttonPin);
  
  
b = EEPROM.read(i);
  

 if (buttonState == HIGH) {
     b ++;
     if ( b > 3){
       b = 0;
     }
 }

  
  switch (b){
    case 0:
  digitalWrite(13, HIGH);  
  delay(30);              
  digitalWrite(13, LOW);  
  delay(175);             
  digitalWrite(13, HIGH);  
  delay(30);             
  digitalWrite(13, LOW);  
  delay(1000);             
     break;
    case 1:
  digitalWrite(13, HIGH); 
  delay(100);            
  digitalWrite(13, LOW);   
  delay(175);             
  digitalWrite(13, HIGH);  
  delay(100);              
  digitalWrite(13, LOW);   
  delay(1000);             
     break;
    case 2:
  digitalWrite(13, HIGH); 
  delay(30);             
  digitalWrite(13, LOW); 
  delay(1000);             
     break;
    case 3:
  digitalWrite(13, HIGH); 
  delay(100);             
  digitalWrite(13, LOW);  
  delay(1000);             
     break;
  }
  Serial.print(b);
    Serial.print("\n");

  
}
  for (i = 0; i <= 1; i++){
  EEPROM.write(i, b);
}

Why are you writing 0 to EEPROM in setup()? Why is the call to EEPROM.write() in a loop? You want to write ONE value, whenever that value changes in loop(), and read ONE value in setup(), to retrieve the last value written.

Thanks PaulS..

That was a great explanation.. You explained me the problem instead of writing the code...

I solved it
:slight_smile:

Just wrote
for (i = 0; i <= 2; i++){
EEPROM.write(i, b);
}

in the loop, wrote 2 instead 1 in i<= 1

and pasted the read eeprom in setup
that is..

b = EEPROM.read(i);

Pauls I have another small problem...

to shift for one blinking pattern to another by pressing the button.. That is to go from Case 0 to case 1 OR from case 1 to case 2 OR from case 2 to case 3..
I have to keep the button pressed and wait for the led to blink again then only it shifts to the next case i think i have to wait for the loop to start again..

Is is because of the delays I am using..

I want the button to react as soon as i click and leave it quick ... and even it should not change from case 1 to case 2 and then case 3 if I keep it pressed..It should only shift once and wait for me to leave it and press it again...

How to solve this part..??

The way you are writing the data, and the way you are reading it now are both wrong. Let's look at the read first:

b = EEPROM.read(i);

You are reading the value from the address stored in i (which is 0, initially), and storing it in b (which is supposed to be the address).

The one letter names are not very good. If you used something like this:

byte address = 0;
byte codeType = 0;

It would be much clearer which variable to use where in the EEPROM.read() call.

codeType = EEPROM.read(address);

For the write, you only want to write ONE value, which means that you do not need a loop.

EEPROM.write(address, codeType);

With the loop you have now, the value that ends up in EEPROM will always be 2, and having run the loop, you've changed the value used in the switch statement, so that it is out of sync with the number of switch presses.

I followed u what u said about how to read and write EEPROM...

But the switch problem I am saying u I was having before i even thought of writing anything in the EEPROM...

how to solve that part...??

You know that there are two ways to blink an LED, right? With delay() and without delay(). Look at the blink without delay example to see how to blink an LED without using the delay() function. Adapt that to your situation.