how do i read/write output pin states to eeprom?

how do you go about writing output pins (led pin) states to eeprom in order to load them back up
exactly how you saved them?

i have 8 pins (pins 6-13) setup for led outputs. The arduino reads serial data in order to toggle individual led outputs on/off
using simple if statements. eg

 if (dataByte == 0){
        if (digitalRead (13) == LOW)
        digitalWrite (13, HIGH);  //turn on led
      else
        digitalWrite (13, LOW);  //turn off led

this is more or less repeated for each output led 6-13.

Whats puzzling me is how to take the 8 output led states (byte) of data and have a switch to write to address/location 0, and then if i want to load that setting, press a load setting switch to
light up the leds that were stored to that address?

somethings giving me the idea of getting the 8 output led states and putting it in some sort of array and storing that array to address 0 and then loading it up when needed yet i dont understand how to do that.

anyone help me out with trying to get my head around the code to do this
thanks

The code would look something like this:

// Save the state of the lights
unsigned char lights = 0;
for (i=6; i<14; i++) {
   lights |= digitalRead(i) << (i-6);
}
eepromWrite(location, lights);

// restore the lights
lights = eepromRead(location);
for (i=6; i<14; i++) {
   digitalWrite(i, lights & (1<< (i-6));
}

thanks for the quick reply. could you elaborate what the code is doing?

i get the for loop is counting through the output pins but not entirely sure
whats happening within the for loop.

cheers

The save code is taking the state of each output (0 or 1), shifting it to a specific bit position, and storing it in lights.

The restore code does the opposite: sets the output pin to the stat of the bit in a specific bit position.

The bitRead() and bitWrite() "functions" do the same thing, but make it a bit clearer exactly what is happening. Since they are implemented as macros, there's not the overhead of a function call, so performance is not (seriously, if at all) impacted.

PaulS:
The bitRead() and bitWrite() "functions" do the same thing, but make it a bit clearer exactly what is happening. Since they are implemented as macros, there's not the overhead of a function call, so performance is not (seriously, if at all) impacted.

I prefer to write in C most of the time but, OK...

http://arduino.cc/en/Reference/BitRead
http://arduino.cc/en/Reference/BitWrite

// Save the state of the lights using bitWrite()
unsigned char lights = 0;
for (i=6; i<14; i++) {
   bitWrite(lights, i-6, digitalRead(i));
}
eepromWrite(location, lights);

// restore the lights using bitRead() 
lights = eepromRead(location);
for (i=6; i<14; i++) {
   digitalWrite(i, bitRead(lights, i-6));
}

Of course anyone not familiar with those two 'functions' has to guess what the arguments mean and trust that the author has put them in the correct order. This is particularly confusing in the case of bitWrite() because if it were really a function the first argument would have to be passed by address (&light) in order for the 'function' to modify the value. Since it isn't passed by address one would naturally assume that the value is not modified because that is how C functions work.

This is particularly confusing in the case of bitWrite() because if it were really a function the first argument would have to be passed by address (&light) in order for the 'function' to modify the value. Since it isn't passed by address one would naturally assume that the value is not modified because that is how C functions work.

If the function is defined as taking a reference, that is not true. If the function takes a reference, there is no need to then use the address of operator.

cheers for the replies guys.

i will get round to trying out a little later on.

thanks guys. i used the bitread/write edit of the code and tweaked it a little and put it in my project. works a treat.
i just need to map out the eeprom memory now to how many locations i can save to.

have you seen anything to do with rotary encoders to scroll through eeprom address locations for the user to save and restore values
from the address selected from the encoder?

thanks once again :slight_smile:

kh602:
thanks guys. i used the bitread/write edit of the code and tweaked it a little and put it in my project. works a treat.
i just need to map out the eeprom memory now to how many locations i can save to.

have you seen anything to do with rotary encoders to scroll through eeprom address locations for the user to save and restore values
from the address selected from the encoder?

thanks once again :slight_smile:

have a look at Liudrs PHI libraries - he has rotary encoder support in there. If i were doing this i would use a 20x4 LCD as the user interface and let the users select from values presented on the LCD - those values would then reference the correct EEPROM location

Craig

I will sure look in to it. Thanks.

With the EEPROM having a life span of around 100,000
Does the count increase when reading from memory as well or is it
Just writing up to 100,000 times that matters?

I am not doing anything intense like logging, but just curious about it.

kh602:
I will sure look in to it. Thanks.

With the EEPROM having a life span of around 100,000
Does the count increase when reading from memory as well or is it
Just writing up to 100,000 times that matters?

I am not doing anything intense like logging, but just curious about it.

It's 100,000 writes. You can read as much as you like without wearing out the EEPROM.

thats good to know. thanks again

is there a similar way of saving the output data to eeprom above but using 74ch595 shift registers so i can
have more output pins for leds?

cheers

kh602:
is there a similar way of saving the output data to eeprom above but using 74ch595 shift registers so i can have more output pins for leds?

To send bits to a shift register you have to pack them into bytes. You can store those bytes in EEPROM between the time you pack them and the time you send them to the shift register.

johnwasser:

PaulS:
The bitRead() and bitWrite() "functions" do the same thing, but make it a bit clearer exactly what is happening. Since they are implemented as macros, there's not the overhead of a function call, so performance is not (seriously, if at all) impacted.

I prefer to write in C most of the time but, OK...

http://arduino.cc/en/Reference/BitRead
http://arduino.cc/en/Reference/BitWrite

// Save the state of the lights using bitWrite()

unsigned char lights = 0;
for (i=6; i<14; i++) {
   bitWrite(lights, i-6, digitalRead(i));
}
eepromWrite(location, lights);

// restore the lights using bitRead()
lights = eepromRead(location);
for (i=6; i<14; i++) {
   digitalWrite(i, bitRead(lights, i-6));
}




Of course anyone not familiar with those two 'functions' has to guess what the arguments mean and trust that the author has put them in the correct order. This is particularly confusing in the case of bitWrite() because if it were really a function the first argument would have to be passed by address (&light) in order for the 'function' to modify the value. Since it isn't passed by address one would naturally assume that the value is not modified because that is how C functions work.

Hi I'm trying to use your code to store the status of 4 leds, this is the message I receive when I verify it :

sketch_sep05a:3: error: expected unqualified-id before 'for'
sketch_sep05a:3: error: expected constructor, destructor, or type conversion before '<' token
sketch_sep05a:3: error: expected constructor, destructor, or type conversion before '++' token
sketch_sep05a:6: error: expected constructor, destructor, or type conversion before '(' token
sketch_sep05a:9: error: expected constructor, destructor, or type conversion before '=' token
sketch_sep05a:10: error: expected unqualified-id before 'for'
sketch_sep05a:10: error: expected constructor, destructor, or type conversion before '<' token

could you please help me ?
sketch_sep05a:10: error: expected constructor, destructor, or type conversion before '++' token

kh602:
how do you go about writing output pins (led pin) states to eeprom in order to load them back up
exactly how you saved them?

i have 8 pins (pins 6-13) setup for led outputs. The arduino reads serial data in order to toggle individual led outputs on/off
using simple if statements. eg

 if (dataByte == 0){

if (digitalRead (13) == LOW)
        digitalWrite (13, HIGH);  //turn on led
      else
        digitalWrite (13, LOW);  //turn off led




this is more or less repeated for each output led 6-13. 

Whats puzzling me is how to take the 8 output led states (byte) of data and have a switch to write to address/location 0, and then if i want to load that setting, press a load setting switch to
light up the leds that were stored to that address?

somethings giving me the idea of getting the 8 output led states and putting it in some sort of array and storing that array to address 0 and then loading it up when needed yet i dont understand how to do that.

anyone help me out with trying to get my head around the code to do this
thanks

Hi I have the same need, could you please send me the complete code ? Mine is not working unfortunately.
thanks,
Domenico

Hi I'm trying to use your code to store the status of 4 leds, this is the message I receive when I verify it :

That code was a snippet, to define how to do a small task, not a complete sketch. Show your complete sketch.

PaulS:

Hi I'm trying to use your code to store the status of 4 leds, this is the message I receive when I verify it :

That code was a snippet, to define how to do a small task, not a complete sketch. Show your complete sketch.

Hi thanks for the reply, here is the sketch I'm using, with this sketch I can turn on/off one or more led and I would like to use the above mentioned code to store leds combination when another pushbutton (store) is pressed.

//pushbutton= pin
int Rosso_Button = 30;
int Giallo_Button = 31;
int Verde_Button = 32;

//pushbutton current state
int stato_bottoni[3];
boolean stato_switch[3];

// led = pin
int Led_Rosso = 11;
int Led_Giallo = 10;
int Led_Verde = 9;

void setup()
{

// pushbuttons
pinMode (Rosso_Button, INPUT);
pinMode (Giallo_Button, INPUT);
pinMode (Verde_Button, INPUT);

// Led
pinMode (Led_Rosso, OUTPUT);
pinMode (Led_Giallo, OUTPUT);
pinMode (Led_Verde, OUTPUT);

stato_bottoni[0] = 0;
stato_bottoni[1] = 0;
stato_bottoni[2] = 0;

digitalWrite (Led_Rosso, LOW);
digitalWrite (Led_Giallo, LOW);
digitalWrite (Led_Verde, LOW);

}

void aggiornaStato(int bottone, int& statoBottoneVecchio, boolean& statoSwitch)
{
//read pushbutton state
int statoBottoneCorrente = digitalRead (bottone);

//if the state has changed (new state)
//managing the new state
if (statoBottoneCorrente != statoBottoneVecchio)
{
//bouncing
if (statoBottoneCorrente == HIGH)
{
delay(100);
statoSwitch = !statoSwitch;
}
//new state is the current one
statoBottoneVecchio = statoBottoneCorrente;
}
}

void controllaPulsante()
{
aggiornaStato(Rosso_Button, stato_bottoni[0], stato_switch[0]);
aggiornaStato(Giallo_Button, stato_bottoni[1], stato_switch[1]);
aggiornaStato(Verde_Button, stato_bottoni[2], stato_switch[2]);
}

void loop()
{

controllaPulsante();

//**** red led (rosso) is activated by red (rosso) pushbutton

if (stato_switch[0] == 1)

{
digitalWrite(Led_Rosso, HIGH);
}
else
{
digitalWrite(Led_Rosso, LOW);
}

//**** yellow led (giallo) is activated by yellow (giallo) pushbutton

if (stato_switch[1] == 1)

{
digitalWrite(Led_Giallo, HIGH);
}
else
{
digitalWrite(Led_Giallo, LOW);
}

//***** gren led (verde) is activated by green(verde) pushbutton

if (stato_switch[2] == 1)

{
digitalWrite(Led_Verde, HIGH); // turn led on
}
else
{
digitalWrite(Led_Verde, LOW); //turn off led
}

}

I would like to use the above mentioned code to store leds combination when another pushbutton (store) is pressed.

OK. Fine. Permission granted.