Creating an infinite epprom counter for reset times and minutes running

Hi there,
I have been trying to make a program that counts the reset times of the arduino and the minutes (6secs in the example) it has been running since last poweron. Then I save these values to epprom adress that changes after 90000 times(5 write times in the example) jumps and writes to another address.
Its crucial to make a counter that can count up to a billion, as i will increase it every minute.

The problem is that I dont know how to test my code as I have a servo attached near the RST button of arduino, so it resets it. It takes a very long time for the eeAddress to reach the Epprom length.
I seem to have a problem when the epprom address of the eeAddress exceeds the Epprom length, then eeAddress becomes “3” and then for some reason that i cant determine, the timespoweredon becomes 0
Even if i change the maxwritetimes from 5 to 50, the problem occurs exactly as before.

Any help would be appreciated.

The Code (WITHOUT debug Serial prints):

#include <EEPROM.h>

//unsigned long timespwredon = 4294967295;
unsigned long timespwredon = 0;
unsigned long timerunningafterpwrup = 0;
unsigned long  jumptonextadrrmax = 0;
unsigned long maxwritetimes = 5;
unsigned long previousRunningMillis = 0;        // will store last time LED was updated
int eepreviousaddress1 = 0;
int eepreviousaddress2 = 1;
int eepreviousaddress3 = 2;
//int addres1=235;
// int addres2=3;
//int addres3=2;
int addres1 = 0;
int addres2 = 0;
int addres3 = 0;

// constants won't change :
const long RunningInterval = 6000;           //  (1hour = 60000x60 = 3600000)

int eeAddress;   //Location we want the data to be put.

struct MyObject {
  unsigned long field1;
  unsigned long field2;
};

void setup() {

  Serial.begin(9600);
  Serial.println( "NEW PROGRAM!" );
  //EEPROM.write(eepreviousaddress1, addres1);
  //EEPROM.write(eepreviousaddress2, addres2);
  //EEPROM.write(eepreviousaddress3, addres3);
  addres1 = EEPROM.read(eepreviousaddress1);
  addres2 = EEPROM.read(eepreviousaddress2);
  addres3 = EEPROM.read(eepreviousaddress3);

  if (addres3 == 0) {
    addres3 = 1;
    jumptonextadrrmax = addres3 * maxwritetimes;
  }
  //maxwritetimes=addres3*maxwritetimes;


  if (addres3 != 1) {
    jumptonextadrrmax = addres3 * maxwritetimes;
  }


  //EDOOOOO ELSE

  if ((addres1 == 0) && (addres2 == 0)) {
    eeAddress = 3 + addres1 + (addres2 * 256);
  }
  else {
    eeAddress = addres1 + (addres2 * 256);
  }


  eppromread(); //Run the next test.
  timespwredon++;
  // timerunningafterpwrup = millis();

  eppromwrite(timespwredon, timerunningafterpwrup);






}

void loop() { /* Empty loop */

  eppromread(); //Run the next test.
  eppromwrite(timespwredon, timerunningafterpwrup);
}

void eppromread() {
  
  MyObject customVar; //Variable to store custom object read from EEPROM.
  EEPROM.get( eeAddress, customVar );
  timespwredon = customVar.field1;
  timerunningafterpwrup = customVar.field2;
  Serial.print( "timespwredon: " );
  Serial.print( customVar.field1 );
  Serial.print( " timerunningafterpwrup: " );
  Serial.print( customVar.field2 );
  Serial.print( " timespwron: " );
  Serial.print(timespwredon);
  Serial.print( " Minutes:  " );
  Serial.print( timerunningafterpwrup);
  Serial.print( " Addr: " );
  Serial.print( eeAddress );
  Serial.println( "   " );

}



unsigned long eppromwrite(unsigned long timespwredon, unsigned long timerunningafterpwrup) {

  //Start running time check
  unsigned long RunningcurrentMillis = millis();

  if (RunningcurrentMillis - previousRunningMillis >= RunningInterval) {
    // save the last time you blinked the LED
    previousRunningMillis = RunningcurrentMillis;
    timerunningafterpwrup = timerunningafterpwrup + 1;
    Serial.println( " previousRunningMillis = RunningcurrentMillis;  " );
  }
  //End running time check


  //Data to store.
  MyObject customVar = {
    timespwredon,
    timerunningafterpwrup
  };

  if (timespwredon > jumptonextadrrmax) {

    eeAddress += sizeof(unsigned long); //Move address to the next byte after unsigned long
    eeAddress += sizeof(unsigned long); //Move address to the next byte after unsigned long
    //delay(50);
    addres3++;
    jumptonextadrrmax = addres3 * maxwritetimes;
    // addres1 += sizeof(unsigned long);
    // addres1 += sizeof(unsigned long);
    // addres2 += sizeof(unsigned long);
    // addres2 += sizeof(unsigned long);
    addres2 = eeAddress / 256;
    addres1 = eeAddress - (addres2 * 256);
    EEPROM.write(eepreviousaddress1, addres1);
    EEPROM.write(eepreviousaddress2, addres2);
    EEPROM.write(eepreviousaddress3, addres3);

  }
  if (eeAddress >= (EEPROM.length() - sizeof(unsigned long))) {
    eeAddress = 3;
  }
  EEPROM.put( eeAddress, customVar );

}

It's really not such a good idea to power your servo directly from the Arduino board. Some servos will draw a fair bit of current when starting and when under load. Try using a separate power supply for it.

I seem to have a problem when the epprom address of the eeAddress exceeds the Epprom length, then eeAddress becomes "3" and then for some reason that i cant determine, the timespoweredon becomes 0

I'm not sure exactly what you're saying here. Do you mean that you're writing to the end of the EEPROM, then your address is automatically 'wrapping' around to the beginning of the EEPROM again? Have you written to it with 90000 writes to each address already?

The Code (WITH debug Serial prints):

#include <EEPROM.h>
//unsigned long timespwredon = 4294967295;
unsigned long timespwredon = 0;
unsigned long timerunningafterpwrup = 0;
unsigned long  jumptonextadrrmax = 0;
unsigned long maxwritetimes = 5;
unsigned long previousRunningMillis = 0;        // will store last time LED was updated
int eepreviousaddress1=0;
int eepreviousaddress2=1;
int eepreviousaddress3=2;
//int addres1=235;
// int addres2=3;
//int addres3=2;
int addres1=0;
int addres2=0;
int addres3=0;

// constants won't change :
const long RunningInterval = 6000;           //  (1hour = 60000x60 = 3600000)

int eeAddress;   //Location we want the data to be put.

struct MyObject {
  unsigned long field1;
  unsigned long field2;
};

void setup() {

  Serial.begin(9600);
  Serial.println( "NEW PROGRAM!" );
    Serial.print( " addres11112: " );
  Serial.print( addres1 );
   Serial.print( " addres13223:: " );
  Serial.print( addres3 );
//EEPROM.write(eepreviousaddress1, addres1);
//EEPROM.write(eepreviousaddress2, addres2);
//EEPROM.write(eepreviousaddress3, addres3);
addres1 = EEPROM.read(eepreviousaddress1);
addres2 = EEPROM.read(eepreviousaddress2);
 addres3 = EEPROM.read(eepreviousaddress3);
   Serial.print( " addres11113: " );
  Serial.print( addres1 );
   Serial.print( " addres13333:: " );
  Serial.print( addres3 );
    if (addres3==0) {
     addres3=1;
      jumptonextadrrmax=addres3*maxwritetimes;
        Serial.print( " 11addres3!=1: " );
  Serial.print( jumptonextadrrmax );
   }
   //maxwritetimes=addres3*maxwritetimes;
        Serial.print( " 22addres3!=1: " );
  Serial.print( jumptonextadrrmax );
   
   if (addres3!=1) {
     jumptonextadrrmax=addres3*maxwritetimes;
        Serial.print( " 33addres3!=1: " );
  Serial.print( jumptonextadrrmax );
   }
   
   
  //EDOOOOO ELSE
  
 if ((addres1==0)&&(addres2==0)) {
   
   eeAddress=3+addres1+(addres2*256);
   Serial.print( " eeAddress1111556: " );
  Serial.print( eeAddress );
 }
 else {
     eeAddress=addres1+(addres2*256);
Serial.print( " eeAddress1111557: " );
  Serial.print( eeAddress );
 }
      Serial.print( " addres1111: " );
  Serial.print( addres1 );
   Serial.print( " addres2222: " );
  Serial.print( addres2 );
   Serial.print( " eeAddress: " );
  Serial.print( eeAddress );
  
  eppromread(); //Run the next test.
  timespwredon++;
 // timerunningafterpwrup = millis();

eppromwrite(timespwredon,timerunningafterpwrup);



   Serial.print( " timespwron: " );
  Serial.print(timespwredon);


}

void loop() { /* Empty loop */
Serial.print( " Addr:6 " );
  Serial.print( eeAddress );
eppromread(); //Run the next test.
Serial.print( " Addr7: " );
  Serial.print( eeAddress );
eppromwrite(timespwredon,timerunningafterpwrup);

Serial.print( " Addr8: " );
  Serial.print( eeAddress );

}

void eppromread() {
  // int eeAddress = sizeof(unsigned long); //Move address to the next byte after unsigned long 'f'.
Serial.print( " Addr5: " );
  Serial.print( eeAddress );
  MyObject customVar; //Variable to store custom object read from EEPROM.
  EEPROM.get( eeAddress, customVar );
timespwredon=customVar.field1;
timerunningafterpwrup=customVar.field2;
  Serial.print( "timespwredon: " );
  Serial.print( customVar.field1 );
  Serial.print( " timerunningafterpwrup: " );
  Serial.print( customVar.field2 );
   Serial.print( " timespwron: " );
  Serial.print(timespwredon);
  Serial.print( " Minutes:  " );
  Serial.print( timerunningafterpwrup);
  Serial.print( " Addr: " );
  Serial.print( eeAddress );
  Serial.println( "   " );
  
}

unsigned long eppromwrite(unsigned long timespwredon,unsigned long timerunningafterpwrup) {
    //Start running time check
unsigned long RunningcurrentMillis = millis();
 
  if(RunningcurrentMillis - previousRunningMillis >= RunningInterval) {
    // save the last time you blinked the LED 
    previousRunningMillis = RunningcurrentMillis;   
timerunningafterpwrup=timerunningafterpwrup+1;
Serial.print( " Addr4: " );
  Serial.print( eeAddress );
Serial.println( " previousRunningMillis = RunningcurrentMillis;  " );
  }
//End running time check

   //Data to store.
  MyObject customVar = {
    timespwredon,
    timerunningafterpwrup
  };
 Serial.print( " maxwritetimes_538: " );
  Serial.print( maxwritetimes );
     Serial.print( " timespwredon_538: " );
  Serial.print( timespwredon );
if (timespwredon>jumptonextadrrmax) {
  Serial.print( " eeAddress_548: " );
  Serial.print( eeAddress );
   Serial.print( " maxwritetimes_548: " );
  Serial.print( jumptonextadrrmax );
     Serial.print( " timespwredon_548: " );
  Serial.print( timespwredon );
  eeAddress += sizeof(unsigned long); //Move address to the next byte after unsigned long
  eeAddress += sizeof(unsigned long); //Move address to the next byte after unsigned long 
  
   //delay(50);
   Serial.print( " addres3_9991:: " );
  Serial.println( addres3 );
  addres3++;
   Serial.print( " addres3_9992:: " );
  Serial.print( addres3 );
    jumptonextadrrmax=addres3*maxwritetimes;
     Serial.print( " addres3_9993:: " );
  Serial.print( addres3 );
    Serial.print( "OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO:  " ); 
  Serial.print( " maxwritetimes_558: " );
  Serial.print( maxwritetimes );
    Serial.print( " timespwredon_558: " );
  Serial.print( timespwredon );
  Serial.print( " addres1_558: " );
  Serial.print( addres1 );
   Serial.print( " addres2_558: " );
  Serial.print( addres2 );
   Serial.print( " eeAddress_558: " );
  Serial.print( eeAddress );
  
// addres1 += sizeof(unsigned long);
// addres1 += sizeof(unsigned long);  
// addres2 += sizeof(unsigned long);
// addres2 += sizeof(unsigned long);
addres2=eeAddress/256;

  addres1=eeAddress-(addres2*256);

  Serial.print( " addres1568: " );
  Serial.print( addres1 );
   Serial.print( " addres2568: " );
  Serial.print( addres2 );
   Serial.print( " eeAddress2568: " );
  Serial.print( eeAddress );
 
  EEPROM.write(eepreviousaddress1, addres1);
  EEPROM.write(eepreviousaddress2, addres2);
 EEPROM.write(eepreviousaddress3, addres3);
    Serial.print( "WRITED TO EPPROM! 111" );
   Serial.print( " addres1557: " );
  Serial.print( addres1 );
   Serial.print( " addres2557: " );
  Serial.print( addres2 );
  Serial.print( " timespwredon>=maxwritetimes  " );
  Serial.print( " Addr2557: " );
  Serial.print( eeAddress );
}
 if (eeAddress >= (EEPROM.length()-sizeof(unsigned long))) {
    eeAddress = 3;
     Serial.print( " addres3_9996:: " );
  Serial.print( addres3 );
}
Serial.print( " Addr576: " );
  Serial.print( eeAddress );
  
   Serial.print( " addres3_9995:: " );
  Serial.print( addres3 );


Serial.print( " addres3_9997:: " );
  Serial.print( addres3 );


  //eeAddress += sizeof(unsigned long); //Move address to the next byte after unsigned long 'f'.
Serial.print( " Addr2: " );
  Serial.print( eeAddress );

  EEPROM.put( eeAddress, customVar );
  //Serial.print( "Written custom data type! \n\nView the example sketch eeprom_get to see how you can retrieve the values!" );

Serial.print( " Addr3: " );
  Serial.print( eeAddress );
  Serial.print( " addres3_9998:: " );
  Serial.print( addres3 );
  //EEPROM.write(eepreviousaddress3, addres3);



}

The Log (this is where the problem occurs):
You will find the log in my next post, as it exceeded the post character limit.

OldSteve: It's really not such a good idea to power your servo directly from the Arduino board. Some servos will draw a fair bit of current when starting and when under load. Try using a separate power supply for it. I'm not sure exactly what you're saying here. Do you mean that you're writing to the end of the EEPROM, then your address is automatically 'wrapping' around to the beginning of the EEPROM again? Have you written to it with 90000 writes to each address already?

I power it not directly and i also use another arduino to make it move. the problem is not my servo, but having to wait for lots of times until i see the epprom getting full. So the thing is that "timespoweredon" shouldnt get to 5 but to keep increasing after the code exceeds the epprom length. Instead of writing 90000 times, I replace maxwritetimes from 90000 to 5. So i see faster the epprom eeAddress to increase.

What the code actually does is that it write for "maxwritetimes" (90000 which is 5 in the example) to Epprom's address 3 and then after the maxwritetimes to jump to the next 8 addresses. When the eeAddress is close to the Epprom.length, I need it to make the eeAddress again 3. When this happens successfully, the timespoweredon is set to (90000 which is 5 in the example) (equal to maxwritetimes) and is not increasing

nathanas: I power it not directly and i also use another arduino to make it move. the problem is not my servo, but having to wait for lots of times until i see the epprom getting full.

Ah, now I understand. When you said "I have a servo attached near the RST button of arduino, so it resets it", I thought you meant that the servo was connected to pins near the reset button and that it was (unintentionally) resetting the board. So really it's a just a code problem, not a spike problem AND a code problem, which is what I thought. I'll need to look more closely at your code when I have time. Someone else might beat me to that, I have to do some other things right now.

The Log (where actually the problem occurs):

 Addr7: 1011 maxwritetimes_538: 50 timespwredon_538: 6349 Addr576:~NEW PROGRAM!
 addres11112: 0 addres13223:: 0 addres11113: 243 addres13333:: 127 22addres3!=1: 0 33addres3!=1: 6350 eeAddress1111557: 1011 addres1111: 243 addres2222: 3 eeAddress: 1011 Addr5: 1011timespwredon: 6349 timerunningafterpwrup: 29 timespwron: 6349 Minutes:  29 Addr: 1011   
 maxwritetimes_538: 50 timespwredon_538: 6350 Addr576: 1011 addres3_9995:: 127 addres3_9997:: 127 Addr2: 1011 Addr3: 1011 addres3_9998:: 127 timespwron: 6350 Addr:6 1011 Addr5: 1011timespwredon: 6350 timerunningafterpwrup: 29 timespwron: 6350 Minutes:  29 Addr: 1011   
 Addr7: 1011 maxwritetimes_538: 50 timespwredon_538: 6350 Addr576: 1011 addres3_9995:: 127 addres3_9997:: 127 Addr2: 1011 Addr3: 1011 addres3_9998:: 127 Addr8: 1011 Addr:6 1011 Addr5: 1011timespwredon: 6350 timerunningafterpwrup: 29 timespwron: 6350 Minutes:  29 Addr: 1011   
 Addr7: 1011 maxwritetimes_538: 50 timespwredon_538: 6350 Addr576: 1011 addres3_9995:: 127 addres3_9997:: 127 Addr2: 1011 Addr3: 1011 addres3_9998:: 127 Addr8: 1011 Addr:6 1011 Addr5: 1011timespwredon: 6350 timerunningafterpwrup: 29 timespwron: 6350 Minutes:  29 Addr: 1011   
 Addr7: 1011 maxwritetimes_538: 50 timespwredon_538: 6350 Addr576: 1011 addres3_9995:: 127 addres3_9997:: 127 Addr2: 1011 Addr3: 1011 addres3_9998:: 127 Addr8: 1011 Addr:6 1011 Addr5: 1011timespwredon: 6350 timerunningafterpwrup: 29 timespwron: 6350 Minutes:  29 Addr: 1011   
 Addr7: 1011 maxwritetimes_538: 50 timespwredon_538: 6350 Addr576: 1011 addresNEW PROGRAM!
 addres11112: 0 addres13223:: 0 addres11113: 243 addres13333:: 127 22addres3!=1: 0 33addres3!=1: 6350 eeAddress1111557: 1011 addres1111: 243 addres2222: 3 eeAddress: 1011 Addr5: 1011timespwredon: 6350 timerunningafterpwrup: 29 timespwron: 6350 Minutes:  29 Addr: 1011   
 maxwritetimes_538: 50 timespwredon_538: 6351 eeAddress_548: 1011 maxwritetimes_548: 6350 timespwredon_548: 6351 addres3_9991:: 127
 addres3_9992:: 128 addres3_9993:: 128OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO:   maxwritetimes_558: 50 timespwredon_558: 6351 addres1_558: 243 addres2_558: 3 eeAddress_558: 1019 addres1568: 251 addres2568: 3 eeAddress2568: 1019WRITED TO EPPROM! 111 addres1557: 251 addres2557: 3 timespwredon>=maxwritetimes   Addr2557: 1019 Addr576: 1019 addres3_9995:: 128 addres3_9997:: 128 Addr2: 1019 Addr3: 1019 addres3_9998:: 128 timespwron: 6351 Addr:6 1019 Addr5: 1019timespwredon: 6351 timerunningafterpwrup: 29 timespwron: 6351 Minutes:  29 Addr: 1019   
 Addr7: 1019 maxwritetimes_538: 50 timespwredon_538: 6351 Addr576: 1019 addres3_9995:: 128 addres3_9997:: 128 Addr2: 1019 Addr3: 1019 addres3_9998:: 128 Addr8: 1019 Addr:6 1019 Addr5: 1019timespwredon: 6351 timerunningafterpwrup: 29 timespwron: 6351 Minutes:  29 Addr: 1019   
 Addr7: 1019 maxwritetimes_538: 50 timespwredon_538: 6351 Addr576: 1019 addres3_9995:: 128 addres3_9997:: 128 Addr2: 1019 Addr3: 1019 addres3_9998:: 128 Addr8: 1019 Addr:6 1019 Addr5: 1019timespwredon: 6351 t}NEW PROGRAM!
 addres11112: 0 addres13223:: 0 addres11113: 0 addres13333:: 0 11addres3!=1: 50 22addres3!=1: 50 eeAddress1111556: 3 addres1111: 0 addres2222: 0 eeAddress: 3 Addr5: 3timespwredon: 50 timerunningafterpwrup: 2 timespwron: 50 Minutes:  2 Addr: 3   
 maxwritetimes_538: 50 timespwredon_538: 51 eeAddress_548: 3 maxwritetimes_548: 50 timespwredon_548: 51 addres3_9991:: 1
 addres3_9992:: 2 addres3_9993:: 2OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO:   maxwritetimes_558: 50 timespwredon_558: 51 addres1_558: 0 addres2_558: 0 eeAddress_558: 11 addres1568: 11 addres2568: 0 eeAddress2568: 11WRITED TO EPPROM! 111 addres1557: 11 addres2557: 0 timespwredon>=maxwritetimes   Addr2557: 11 Addr576: 11 addres3_9995:: 2 addres3_9997:: 2 Addr2: 11 Addr3: 11 addres3_9998:: 2 timespwron: 51 Addr:6 11 Addr5: 11timespwredon: 51 timerunningafterpwrup: 2 timespwron: 51 Minutes:  2 Addr: 11   
 Addr7: 11 maxwritetimes_538: 50 timespwredon_538: 51 Addr576: 11 addres3_9995:: 2 addres3_9997:: 2 Addr2: 11 Addr3: 11 addres3_9998:: 2 Addr8: 11 Addr:6 11 Addr5: 11timespwredon: 51 timerunningafterpwrup: 2 timespwron: 51 Minutes:  2 Addr: 11   
 Addr7: 11 maxwritetimes_538: 50 timespwredon_538: 51 Addr576: 11 addres3_9995:: 2 addres3_9997:: 2 Addr2: 11 Addr3: 11 addres3_9998:: 2 Addr8: 11 Addr:6 11 Addr5: 11timespwredon: 51 timerunningafterpwrup: 2 timespwron: 51 Minutes:  2 Addr: 11   
 Addr7: 11 maxwritetimes_538: 50 timespwredon_538: 51 Addr576: 11 addreNEW PROGRAM!
 addres11112: 0 addres13223:: 0 addres11113: 11 addres13333:: 2 22addres3!=1: 0 33addres3!=1: 100 eeAddress1111557: 11 addres1111: 11 addres2222: 0 eeAddress: 11 Addr5: 11timespwredon: 51 timerunningafterpwrup: 2 timespwron: 51 Minutes:  2 Addr: 11   
 maxwritetimes_538: 50 timespwredon_538: 52 Addr576: 11 addres3_9995:: 2 addres3_9997:: 2 Addr2: 11 Addr3: 11 addres3_9998:: 2 timespwron: 52 Addr:6 11 Addr5: 11timespwredon: 52 timerunningafterpwrup: 2 timespwron: 52 Minutes:  2 Addr: 11   
 Addr7: 11 maxwritetimes_538: 50 timespwredon_538: 52 Addr576: 11 addres3_9995:: 2 addres3_9997:: 2 Addr2: 11 Addr3: 11 addres3_9998:: 2 Addr8: 11 Addr:6 11 Addr5: 11timespwredon: 52 timerunningafterpwrup: 2 timespwron: 52 Minutes:  2 Addr: 11   
 Addr7: 11 maxwritetimes_538: 50 timespwredon_538: 52 Addr576: 11 addres3_9995:: 2 addres3_9997:: 2 Addr2: 11 Addr3: 11 addres3_9998:: 2 Addr8: 11 Addr:6 11 Addr5: 11timespwredon: 52 timerunningafterpwrup: 2 timespwron: 52 Minutes:  2 Addr: 11   
 Addr7: 11 maxwritetimes_538: 50 timespwredon_538: 52 Addr576: 11 addres3_9995:: 2 addres3_9997:: 2 Addr2: 11 Addr3: 11 addres3_9998:: 2 Addr8: 11 Addr:6 11 Addr5: 11timespwredon: 52 timerunningafterpwrup: 2 timespwron: 52 Minutes:  2 Addr: 11   
 Addr7: 11 maxwritetimes_538: 50 timespwredon_538: 52 Addr576: 11 addres3_9995:: 2 addres3_9997:: 2 Addr2: 11 Addr3: 11 addres3_9998:: 2 Addr8: 11 Addr:6 11 Addr5: 11timespwredon: 52 timerunningafterpwrup: 2 timespwron: uNEW PROGRAM!

OldSteve:
Ah, now I understand. When you said “I have a servo attached near the RST button of arduino, so it resets it”, I thought you meant that the servo was connected to pins near the reset button and that it was (unintentionally) resetting the board. So really it’s a just a code problem, not a spike problem AND a code problem, which is what I thought.
I’ll need to look more closely at your code when I have time. Someone else might beat me to that, I have to do some other things right now.

I hope you can help me out on making it work for lots of years even by updating it every minute.
I am waiting for your recommandations when you have the time!
Whoever feels that can help me out here, I’ll be glad

   Serial.print( " addres13333:: " );
  Serial.print( addres3 );

I tried to follow your code and your output, but I got lost with this [u]crap[/u]. It makes NO sense to says that the data is from one variable when it CLEARLY from another.

PaulS:   Serial.print( " addres13333:: " );   Serial.print( addres3 );

I tried to follow your code and your output, but I got lost with this [u]crap[/u]. It makes NO sense to says that the data is from one variable when it CLEARLY from another.

I didn't look at the code earlier. Glad I didn't now. It's impossible to follow. 90% unfathomable Serial.print() statements, very poor formatting, lots of commented out sections and empty lines. Overall, a nightmare.

nathanas:
The Code (WITH debug Serial prints):

I think you are trying to learn how to store to EEPROM and how to assemble from EEPROM an array of bytes which form a number big enough to count for a very long time…

You can try to use a simple Union to solve this easily like this:

#include <EEPROM.h>

const int eepromLocation = 0;

union TimeStamp{
  unsigned long totalResets;
  byte segment[4];
};

TimeStamp timeStamp;


void setup()
{
  Serial.begin(9600);
  for(int i = 0; i < 4; i++)
  {
    timeStamp.segment[i] = EEPROM.read(eepromLocation + i);
  }
  Serial.print("Starting EEPROM Value=");
  Serial.println(timeStamp.totalResets);
}

void loop()
{
  static unsigned long lastWriteTime = 0;
  if(millis() - lastWriteTime > 10*1000UL)
  {
    timeStamp.totalResets++;
    for(int i = 0; i < 4; i++)
    {
      EEPROM.write(eepromLocation + i, timeStamp.segment[i]);
    }
    Serial.print("Current EEPROM Value=");
    Serial.println(timeStamp.totalResets);
    lastWriteTime = millis();
  }
}

compiled and tested.

don’t forget to clear your EEPROM of any garbage before you start:

#include <EEPROM.h>

void setup() 
{
  for (int i = 0; i < 256; i++)
  {
    EEPROM.write(i, 0x00);
  }

}

void loop() 
{

}

run this once to make every EEPROM address = 0.

You can try to use a simple Union to solve this easily like this:

If you want to store large data types, just use EEPROM.put()/EEPROM.get():

#include <EEPROM.h>
 
const int eepromLocation = 0;
unsigned long totalResets;
 
void setup()
{
  Serial.begin(9600);
  EEPROM.get( eepromLocation, totalResets );
  Serial.print("Starting EEPROM Value=");
  Serial.println(totalResets);
}
 
void loop()
{
  static unsigned long lastWriteTime = 0;
  if(millis() - lastWriteTime > 10*1000UL)
  {
    totalResets++;
    EEPROM.put( eepromLocation, totalResets );
    Serial.print("Current EEPROM Value=");
    Serial.println(totalResets);
    lastWriteTime = millis();
  }
}

I made the code work. The thing now is how do I make it use less memory and storage?

/***
   This sketch is about counting the times the microcontroller it has been reset
   and the time it has been running from first installation.
   The values are stored in Epprom.
   To do so the sketch uses 3 standard (not changeable) addresses.
   2 for knowing the Epprom address of "My Object" before the power off and one
   standard address for the maxwritetimes last value.
   (jumptonextadrrmax=addres3*maxwritetimes)

   When the eeAddress is written more than "maxwritetimes" the eeAddress changes.
   If the eeAddress reaches the Microcontroller epprom length, then eeAddress becomes 3 again.

   Written by Nick Athanasoulas 2015
   Released under IonCortex licence.
***/

#include <EEPROM.h>

//unsigned long timespwredon = 4294967295;
unsigned long timespwredon = 0;
unsigned long timerunningafterpwrup = 0;
unsigned long  jumptonextadrrmax = 0;
unsigned long  writetriggercounter = 0;
unsigned long maxwritetimes = 10;  // it is going to be 90000
unsigned long previousRunningMillis = 0;        // will store last time LED was updated
int eepreviousaddress1 = 0;
int eepreviousaddress2 = 1;
int eepreviousaddress3 = 2;
//int addres1=235;
//int addres2=3;
//int addres3=2;
int addres1 = 0;
int addres2 = 0;
int addres3 = 0;
int triggereppromwrite = 0;


// constants won't change :
const long RunningInterval = 5000;           //  (1hour = 60000x60 = 3600000)

int eeAddress;   //Location we want the data to be put.

struct MyObject {
 unsigned long field1;
 unsigned long field2;
};


void setup() {

 Serial.begin(9600);
 Serial.println( "PROGRAM START" );

 //EEPROM.write(eepreviousaddress1, addres1);
 //EEPROM.write(eepreviousaddress2, addres2);
 //EEPROM.write(eepreviousaddress3, addres3);
 addres1 = EEPROM.read(eepreviousaddress1);
 addres2 = EEPROM.read(eepreviousaddress2);
 addres3 = EEPROM.read(eepreviousaddress3);

 if (addres3 == 0) {
   addres3 = 1;
  // jumptonextadrrmax = addres3 * maxwritetimes;
 }

 if (addres3 >= 1) {
   jumptonextadrrmax = addres3 * maxwritetimes;
 }

 if ((addres1 == 0) && (addres2 == 0)) {
   eeAddress = 3 + addres1 + (addres2 * 256);
 }
 else {
   eeAddress = addres1 + (addres2 * 256);
 }
 eppromread(); //Run the next test.
 timespwredon++;
 writetriggercounter++;
 triggereppromwrite = 1;
 // timerunningafterpwrup = millis();
 writetriggercounter = jumptonextadrrmax; //afto de leitourgei gia tin proti fora se
 eppromwrite(timespwredon, timerunningafterpwrup);
}

void loop() { 
 eppromread(); //Run the next test.
 eppromwrite(timespwredon, timerunningafterpwrup);
}

void eppromread() {
 MyObject customVar; //Variable to store custom object read from EEPROM.
 EEPROM.get( eeAddress, customVar );
 timespwredon = customVar.field1;
 timerunningafterpwrup = customVar.field2;
}



unsigned long eppromwrite(unsigned long timespwredon, unsigned long timerunningafterpwrup) {

 //Start running time check
 unsigned long RunningcurrentMillis = millis();

 if (RunningcurrentMillis - previousRunningMillis >= RunningInterval) {
   // Check if a second has past
   previousRunningMillis = RunningcurrentMillis;
   timerunningafterpwrup++;
   writetriggercounter++;
   triggereppromwrite = 1;
   Serial.println( "previousRunningMillis = RunningcurrentMillis" );
 }
 //End running time check


 //Data to store.
 MyObject customVar = {
   timespwredon,
   timerunningafterpwrup
 };

 if (writetriggercounter > jumptonextadrrmax) {
   eeAddress += sizeof(unsigned long); //Move address to the next byte after unsigned long
   eeAddress += sizeof(unsigned long); //Move address to the next byte after unsigned long
   addres3++;
   jumptonextadrrmax = addres3 * maxwritetimes;

   if (eeAddress >= (EEPROM.length() - sizeof(unsigned long) - sizeof(unsigned long))) {
     eeAddress = 3;
   }

   addres2 = eeAddress / 256;
   addres1 = eeAddress - (addres2 * 256);
   EEPROM.write(eepreviousaddress1, addres1);
   EEPROM.write(eepreviousaddress2, addres2);
   EEPROM.write(eepreviousaddress3, addres3);

 }

 if (triggereppromwrite == 1) {
   EEPROM.put( eeAddress, customVar );
   Serial.println( "TRIGGERED WRITE IN EEPROM!" );
   triggereppromwrite = 0;
 }
 //EEPROM.put( eeAddress, customVar );
 //Serial.println( " WRITTEN EEPROM_2" );
 /*
 Serial.print( " addres1: " );
 Serial.print( addres1 );
 Serial.print( " addres2: " );
 Serial.print( addres2 );
 Serial.print( " addres3: " );
 Serial.print( addres3 );
  Serial.print( " customVar.field1: " );
 Serial.print( customVar.field1 );
 Serial.print( " timerunningafterpwrup: " );
 Serial.print( timerunningafterpwrup );
 //Serial.print( " customVar.field2: " );
 //Serial.print( customVar.field2 );
 */
 Serial.print( " timespwron: " );
 Serial.print(timespwredon);
 Serial.print( "  " );
 Serial.print( timerunningafterpwrup);
 Serial.print( " Seconds" );
 Serial.print( " maxwritetimes: " );
 Serial.print( maxwritetimes );
 Serial.print( " jumptonextadrrmax: " );
 Serial.print( jumptonextadrrmax );
 Serial.print( " writetriggercounter: " );
 Serial.print( writetriggercounter );
  Serial.print( " eeAddress: " );
 Serial.print( eeAddress );

 Serial.println( "   " );

}
unsigned long  writetriggercounter = 0;
unsigned long maxwritetimes = 10;  // it is going to be 90000
unsigned long previousRunningMillis = 0;        // will store last time LED was updated

Consistency is good. Your use of capital letters in names is NOT consistent.

How can anyone reasonably be expected to look at a name like previousRunningMillis and infer that it contains anything like the last time an LED was whatever it was.

Clearly, the comment has nothing to do with the variable, so get rid of it. And, the name is still stupid. The time stored in the variable is not the last time anything was running.

Consistency is good. Your use of capital letters in names is NOT consistent.

I didn’t know that, as most of the Arduino examples contain Capital letters.

How can anyone reasonably be expected to look at a name like previousRunningMillis and infer that it contains anything like the last time an LED was whatever it was.

Clearly, the comment has nothing to do with the variable, so get rid of it. And, the name is still stupid. The time stored in the variable is not the last time anything was running.

I changed it. Sorry for missing that out
Any recommendations on how to make it work with less memory and storage?

/***
    This sketch is about counting the times the microcontroller it has been reset
    and the time it has been running from first installation.
    The values are stored in Epprom.
    To do so the sketch uses 3 standard (not changeable) addresses.
    2 for knowing the Epprom address of "My Object" before the power off and one
    standard address for the maxwritetimes last value.
    (jumptonextadrrmax=addres3*maxwritetimes)

    When the eeAddress is written more than "maxwritetimes" the eeAddress changes.
    If the eeAddress reaches the Microcontroller epprom length, then eeAddress becomes 3 again.

    Written by Nick Athanasoulas 2015
    Released under IonCortex licence.
***/

#include <EEPROM.h>

//unsigned long timespwredon = 4294967295;
unsigned long timespwredon = 0;
unsigned long timerunningafterpwrup = 0;
unsigned long  jumptonextadrrmax = 0;
unsigned long  writetriggercounter = 0;
unsigned long maxwritetimes = 10;  // it is going to be 90000
unsigned long previousRunningMillis = 0;        
int eepreviousaddress1 = 0;
int eepreviousaddress2 = 1;
int eepreviousaddress3 = 2;
//int addres1=235;
//int addres2=3;
//int addres3=2;
int addres1 = 0;
int addres2 = 0;
int addres3 = 0;
int triggereppromwrite = 0;


// constants won't change :
const long RunningInterval = 5000;           //  (1hour = 60000x60 = 3600000)

int eeAddress;   //Location we want the data to be put.

struct MyObject {
  unsigned long field1;
  unsigned long field2;
};


void setup() {

  Serial.begin(9600);
  Serial.println( "PROGRAM START" );

  //EEPROM.write(eepreviousaddress1, addres1);
  //EEPROM.write(eepreviousaddress2, addres2);
  //EEPROM.write(eepreviousaddress3, addres3);
  addres1 = EEPROM.read(eepreviousaddress1);
  addres2 = EEPROM.read(eepreviousaddress2);
  addres3 = EEPROM.read(eepreviousaddress3);

  if (addres3 == 0) {
    addres3 = 1;
   // jumptonextadrrmax = addres3 * maxwritetimes;
  }

  if (addres3 >= 1) {
    jumptonextadrrmax = addres3 * maxwritetimes;
  }

  if ((addres1 == 0) && (addres2 == 0)) {
    eeAddress = 3 + addres1 + (addres2 * 256);
  }
  else {
    eeAddress = addres1 + (addres2 * 256);
  }
  eppromread(); //Run the next test.
  timespwredon++;
  writetriggercounter++;
  triggereppromwrite = 1;
  // timerunningafterpwrup = millis();
  writetriggercounter = jumptonextadrrmax; 
  eppromwrite(timespwredon, timerunningafterpwrup);
}

void loop() { 
  eppromread(); //Run the next test.
  eppromwrite(timespwredon, timerunningafterpwrup);
}

void eppromread() {
  MyObject customVar; //Variable to store custom object read from EEPROM.
  EEPROM.get( eeAddress, customVar );
  timespwredon = customVar.field1;
  timerunningafterpwrup = customVar.field2;
}



unsigned long eppromwrite(unsigned long timespwredon, unsigned long timerunningafterpwrup) {

  //Start running time check
  unsigned long RunningcurrentMillis = millis();

  if (RunningcurrentMillis - previousRunningMillis >= RunningInterval) {
    // Check if a second has past
    previousRunningMillis = RunningcurrentMillis;
    timerunningafterpwrup++;
    writetriggercounter++;
    triggereppromwrite = 1;
    Serial.println( "previousRunningMillis = RunningcurrentMillis" );
  }
  //End running time check


  //Data to store.
  MyObject customVar = {
    timespwredon,
    timerunningafterpwrup
  };

  if (writetriggercounter > jumptonextadrrmax) {
    eeAddress += sizeof(unsigned long); //Move address to the next byte after unsigned long
    eeAddress += sizeof(unsigned long); //Move address to the next byte after unsigned long
    addres3++;
    jumptonextadrrmax = addres3 * maxwritetimes;

    if (eeAddress >= (EEPROM.length() - sizeof(unsigned long) - sizeof(unsigned long))) {
      eeAddress = 3;
    }

    addres2 = eeAddress / 256;
    addres1 = eeAddress - (addres2 * 256);
    EEPROM.write(eepreviousaddress1, addres1);
    EEPROM.write(eepreviousaddress2, addres2);
    EEPROM.write(eepreviousaddress3, addres3);

  }

  if (triggereppromwrite == 1) {
    EEPROM.put( eeAddress, customVar );
    Serial.println( "TRIGGERED WRITE IN EEPROM!" );
    triggereppromwrite = 0;
  }

  Serial.print( " timespwron: " );
  Serial.print(timespwredon);
  Serial.print( "  " );
  Serial.print( timerunningafterpwrup);
  Serial.print( " Seconds" );
  Serial.print( " maxwritetimes: " );
  Serial.print( maxwritetimes );
  Serial.print( " jumptonextadrrmax: " );
  Serial.print( jumptonextadrrmax );
  Serial.print( " writetriggercounter: " );
  Serial.print( writetriggercounter );
   Serial.print( " eeAddress: " );
  Serial.print( eeAddress );
  Serial.println( "   " );

}

nathanas: I would be grateful if you could spend some time looking at the new code.

At the risk of sounding PaulS-esque, if your code was a Rorshach test, I'd probably see an ink blot.

Read up on arrays, you'll simplify your code dramatically.

Also, maybe learn sprintf() to help you simplify your debug prints and make them less of the code

BulldogLowell: At the risk of sounding PaulS-esque, if your code was a Rorshach test, I'd probably see an ink blot.

Read up on arrays, you'll simplify your code dramatically.

Also, maybe learn sprintf() to help you simplify your debug prints and make them less of the code

by sprintf() you mean to add printf support like in this article? http://playground.arduino.cc/Main/Printf

by sprintf() you mean to add printf support like in this article?

sprintf() != printf()

I know that you are proud of the code that you wrote. But, when you no longer use code, DELETE IT! Don't just comment it out.

struct MyObject {
  unsigned long field1;
  unsigned long field2;
};

Surely you are not intending to store some plots of grass in this struct, are you? MyObject is a lousy name.

Sometime in the future, you may want to look back at this code and understand what is is doing. Names like you have there might as well be

struct someName {
  unsigned long someData1;
  unsigned long someData2;
};

for all the information that they convey.

  unsigned long RunningcurrentMillis = millis();

Simple names are good. Why not

  unsigned long now = millis();

?

There is nothing magic about having millis in the name. Get rid of it when it doesn't contribute anything.

I realize that it might sound like we are picking on you, but if we struggle to understand the code, you will to when you come back to it later. Don't make the code any more difficult to understand than it needs to be.

Everyone else is doing well at guiding you, but this point was overlooked, so I thought I'd re-visit it:-

PaulS said:-

Consistency is good. Your use of capital letters in names is NOT consistent.

Then you said:-

I didn't know that, as most of the Arduino examples contain Capital letters.

Using capitals in the right places is correct. What Paul actually meant was that sometimes you do use them, and sometimes you do not use them. It's very hard to read variable names that contain no capitals.

Paul was referring to this sort of thing:-

unsigned long  writetriggercounter = 0;
unsigned long maxwritetimes = 10;                // it is going to be 90000
unsigned long previousRunningMillis = 0;        // will store last time LED was updated

It would be easier to read if it looked like this:-

unsigned long writeTriggerCounter = 0;
unsigned long maxWriteTimes = 10;              // it is going to be 90000
unsigned long previousRunningMillis = 0;        // will store last time LED was updated

If you add capitals where appropriate all the way through your code, it will be far less painful to read.

I also agree with Paul regarding your reference to a LED. If you have no LED in the program, I'm sure you can think of a more accurate descriptive comment.

These points might seem trivial to you, and don't affect the program's operation, but it will make things much easier for us, and as mentioned by Paul, easier for you in the future if you need to review your own code when it isn't so fresh in your mind.

You're doing well at this point, so don't be disheartened by our comments - they are intended to help. :)

nathanas: Its crucial to make a counter that can count up to a billion, as i will increase it every minute.

Your sketch will run for one thousand nine hundred years?