7-segment display

Hi, I have a problem with this code. It works as follows: a 7-segment display that reads numbers 1 through 9 in ascending order; easy, right?
The problem is that every time I reset the arduino, the variables are erased and the counter starts from 0, and I wanted to improve it so that the counter would save it in an EEPROM memory, so every time I reset it the previous variable would remain.
Could you help me? Thank you very much, I attached the code here

int display7c[10]= {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x67};
 
byte a=2;
byte b=3;
byte c=4;
byte d=5;
byte e=6;
byte f=7;
byte g=8;
byte inc=9;

int contador=0;
 
void puerto(int bits,int ini,int fin){
  for(int i=ini;i<=fin;i++) {
    digitalWrite(i,bitRead(bits,i-ini));
  }
}
 
void setup() {

  for(int i=a;i<=g;i++){
    pinMode(i,OUTPUT);
  }
  pinMode(inc,INPUT);
}
 
void loop() {
    if(digitalRead(inc)){
      delay(100);
      while(digitalRead(inc)); //Anti-Rebote
      delay(100);
      contador++;
    }

    if(contador>9){
      contador=0;
    }

    puerto(display7c[contador],a,g);
}

EEPROM is not feasible for frequent writes. It is durable about 100k write/erase cycles. For frequent writes the SD card is definitely better solution
According your idea, it is needed to write each change into the permanent memory to ensure continuity after reset. However, this approach does not solve a case when write is no successful due to e.g. restart right at this moment.
I think, the only solution is to have a backup for power source similar to RTC. In this case, the memory will be permanently up.

Have you considered FRAM (Ferroelectric Random Access Memory) It's similar to Static random-access memory, only with a ferroelectric layer instead of a dielectric layer. This gives it stable handling (the bytes you write are non-volatile) with dynamic responsiveness (you can write them very fast!). Some of the advantages I see in FRAM are high speed reading and writing, non-volatile storage (it remembers its contents without needing power or battery backup), virtually unlimited read / write cycles - you can't wear it out unlike some other types of non-volatile memory. To get started try this link: Adafruit SPI Non-Volatile FRAM Breakout - 64Kbit / 8KByte : ID 1897 : $5.95 : Adafruit Industries, Unique & fun DIY electronics and kits There board has a write enable input so you can lock it down and read it on another machine without worry about trashing the date.

Hi, i started a project which i’m using a current sensor INA219 and i am struggling with the EEPROM cause i want to store the power value so that when i reset my arduino it remembers the last recorded power value but i dont know if i’ve implemented the EEPROM correctly, if any one can help it would be much appreciated.

#include <Wire.h>
#include <EEPROM.h>
#include <Adafruit_INA219.h>

Adafruit_INA219 ina219;

unsigned long timer;
int eeAddress = 0;
float shuntvoltage = 0;
float busvoltage = 0;
float current_mA = 0;
float loadvoltage = 0;
float power_mW = 0;
float energy_Wh = 0;

void setup(void)
{
Serial.begin(115200);
uint32_t currentFrequency;
Serial.println(“Hello!”);
ina219.begin();

}

void loop(void)
{
if(millis() - timer >60000)
{
Serial.println(“Measuring voltage and current with INA219 …”);
EEPROM.update(eeAddress, energy_Wh); //writes data only if it is different from the previous content since EEPROM has 100000 write cycle limit
Serial.println("Written to EEPROM: ");
for( int eeAddress; eeAddress <= 1; eeAddress++)
{

Serial.println(energy_Wh, 3);

}
Serial.print("Read from EEPROM: ");
EEPROM.read(eeAddress);//Get the float data from the EEPROM at position ‘eeAddress’
Serial.println(energy_Wh, 3); //This may print ‘ovf, nan’ if the data inside the EEPROM is not a valid float.*/
Serial.println();
timer = millis();
}
else
{
INA219_Val();
}

}
void INA219_Val(void)
{
shuntvoltage = ina219.getShuntVoltage_mV();
busvoltage = ina219.getBusVoltage_V();
current_mA = ina219.getCurrent_mA();
power_mW = ina219.getPower_mW();
loadvoltage = busvoltage + (shuntvoltage / 1000);
energy_Wh = loadvoltage * current_mA / 3600;

Serial.print(“Bus Voltage: “); Serial.print(busvoltage); Serial.println(” V”);
Serial.print(“Shunt Voltage: “); Serial.print(shuntvoltage); Serial.println(” mV”);
Serial.print(“Load Voltage: “); Serial.print(loadvoltage); Serial.println(” V”);
Serial.print(“Current: “); Serial.print(current_mA); Serial.println(” mA”);
Serial.print(“Power: “); Serial.print(power_mW); Serial.println(” W”);
Serial.print(“Energy: “); Serial.print(energy_Wh); Serial.println(” Wh”);
Serial.println("");
delay(5000);

 float energy_Wh = 0;

Float values are 4 bytes. You should use EEPROM.put() and EEPROM.get() to write and read them. The .update() method is only valid for a single byte.

Did you read the earlier postings in this thread? The point was that because of the 100K write specification, EEPROM storage may be inappropriate.

Hi! It turns out that my girlfriend’s anniversary is approaching and I wanted to give her an arduino circuit consisting of two 7-segment displays that show the number “12”.
Until there everything easy, but it turns out that when I restart, the counter returns to 0 and I get the number 0. Is there any way to store the variable counter in a memory of the arduino to remember the last number that remained?
Thank you very much, I would be very grateful if you could help me on this issue, since I don’t have much time left. I leave the code here

int display7c[10]= {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x67};
 
byte a=2;
byte b=3;
byte c=4;
byte d=5;
byte e=6;
byte f=7;
byte g=8;
byte inc=9;

int contador=0;
 
void puerto(int bits,int ini,int fin){
  for(int i=ini;i<=fin;i++) {
    digitalWrite(i,bitRead(bits,i-ini));
  }
}
 
void setup() {

  for(int i=a;i<=g;i++){
    pinMode(i,OUTPUT);
  }
  pinMode(inc,INPUT);
}
 
void loop() {
    if(digitalRead(inc)){
      delay(100);
      while(digitalRead(inc)); //Anti-Rebote
      delay(100);
      contador++;
    }

    if(contador>9){
      contador=0;
    }

    puerto(display7c[contador],a,g);
}

DeNick_ZaM:
I don't have much time left. I leave the code here

In that case it might be better if you post in the display forum, and leave the storage gurus in peace.

ohh okay thank you very much

Hi! It turns out that my girlfriend’s anniversary is approaching and I wanted to give her an arduino circuit consisting of two 7-segment displays that show the number “12”.
Until there everything easy, but it turns out that when I restart, the counter returns to 0 and I get the number 0. Is there any way to store the variable counter in a memory of the arduino to remember the last number that remained?
Thank you very much, I would be very grateful if you could help me on this issue, since I don’t have much time left. I leave the code here.

int display7c[10]= {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x67};
 
byte a=2;
byte b=3;
byte c=4;
byte d=5;
byte e=6;
byte f=7;
byte g=8;
byte inc=9;

int contador=0;
 
void puerto(int bits,int ini,int fin){
  for(int i=ini;i<=fin;i++) {
    digitalWrite(i,bitRead(bits,i-ini));
  }
}
 
void setup() {

  for(int i=a;i<=g;i++){
    pinMode(i,OUTPUT);
  }
  pinMode(inc,INPUT);
}
 
void loop() {
    if(digitalRead(inc)){
      delay(100);
      while(digitalRead(inc)); //Anti-Rebote
      delay(100);
      contador++;
    }

    if(contador>9){
      contador=0;
    }

    puerto(display7c[contador],a,g);
}

Right.

Well, you have not explained your circuit, or adequately what the intent of this is. You seem to have a button connected somehow to pin 9 - what is it supposed to do?

Multiple cross posted topics merged

Cross posting warning

Cross-posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting will result in a suspension from the forum.

In the future, please take some time to pick the forum board that best suits the topic of your question and then only post once to that forum board. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum board. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

UKHeliBob:
Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.

Easily 15 minutes or more for a detailed answer. :grinning:

Now that the three are merged, we can almost figure out what he is trying to do.

Sort of ... :astonished:

Paul__B:
Right.

Well, you have not explained your circuit, or adequately what the intent of this is. You seem to have a button connected somehow to pin 9 - what is it supposed to do?

It increase the number on the display :wink: