I built a seven segment display with all the segments being controlled by a shift register on each digit.
I need to be able to set the numbers on the display and then power off and still remember the last number that was displayed before it was turned off. This is a simple up and down 2 button control.
Could someone guide me in the right direction to what I need to research to figure out how to store the last state of the display. I think I just need to store the value of the integer that controls the display.
brandonppr:
I built a seven segment display with all the segments being controlled by a shift register on each digit.
I need to be able to set the numbers on the display and then power off and still remember the last number that was displayed before it was turned off. This is a simple up and down 2 button control.
Could someone guide me in the right direction to what I need to research to figure out how to store the last state of the display. I think I just need to store the value of the integer that controls the display.
As aarg, use the EEPROM to store the value.
Here's a code snippet that may help you:
#include <avr/eeprom.h> // needed header
static const uint16_t last_value EEMEM = 0; // initial eeprom value will be 0
uint16_t current_value; // currently displayed value on 7 seg display
current_value = eeprom_read_word (last_value); // retrieve saved value, if any
// loop around
// pseudo code
// read up and down buttons
// increment or decrement "current_value" as necessary
eeprom_write_word (last_value, current_value); // write current value to eeprom
// display "current value" on 7 seg display
// loop around
Note that the above code writes to the EEPROM each time the "current_value" is changed. You may wish to add some smarts to the code to only re-write the EEPROM when necessary to avoid prematurely wearing out the EEPROM cells.
Hope this helps.
P.S. this is not the "conventional" Arduino way of doing it, but I personally prefer it because it refers to EEPROM locations by name and you don't need to know or remember what address you stored the data to. This is the way GCC designed the EEPROM (and PROGMEM) to be used, not the half-azzed weird method used by the Arduino EEPROM library.
I think there are examples how to write to EEPROM only if the value has changed from within the IDE, but this is the function I wrote to do the job.
boolean safeWrite(byte eepromAddress, byte newData, boolean localDebugFlag)
{
boolean result = 0;
byte oldData = EEPROM.read(eepromAddress);
if (newData != oldData)
{
if (localDebugFlag)
{
Serial.print(F(", replacing "));
padHex2(oldData);
Serial.print(F(" with "));
padHex2(newData);
}
EEPROM.write(eepromAddress, newData);
result = 1;
}
else if (localDebugFlag)
{
Serial.print(F(", "));
padHex2(newData);
Serial.print(F(" OK"));
}
}
void padHex2(byte value)
{
Serial.print(F("0x"));
if (value < 16)
{
Serial.print(F("0"));
}
Serial.print(value, HEX);
}
The function returns zero if the new data is the same as the old data and one if the new data is different than the previous data.
The function "padHex2" is there to make the output prettier and because I didn't remember the proper way of padding with zeros (I'm pretty sure there's a nice simple way but I didn't bother to look it up).