How to remember int when power shuts off

8551

else  // changed to released
    {
      // als de huidige staat HIGH dan was de button released
      Serial.print(", newly released at ");
      Serial.print(millis());
      Serial.println(" ms");

      buttonHasBeenPressedForThisTime = millis() - buttonBecamePressedAt;
      buttonHasBeenPressedForTotal = buttonHasBeenPressedForTotal + buttonHasBeenPressedForThisTime;
      Serial.print("   This press: ");
      Serial.print(buttonHasBeenPressedForThisTime);
      Serial.print(" ms");

      Serial.print(", Total: ");
      Serial.print(buttonHasBeenPressedForTotal);
      Serial.println(" ms");
      
      hour = buttonHasBeenPressedForTotal / 1000;
        
      Serial.println(projectnummer);

      
      EEPROM.put(AdressRoom, hour);
      Serial.println(EEPROM.get(AdressRoom, hour));     
    }

What value are you expecting?
Please post more of the serial output.

Okay this is my (working code)

#include <EEPROM.h>

// deze constante veranderen niet
const int  buttonPin = 3;    // waar de button op aangesloten is
//   the button moet aangesloten zijn vanaf pin naar ground, pinmode is een input_pullup

// Deze variabelen veranderen 
bool buttonState;         // momentele status van de button
bool lastButtonState;     // vorige status van de button

unsigned long buttonBecamePressedAt;
unsigned long buttonHasBeenPressedForTotal = 0;
unsigned long buttonHasBeenPressedForThisTime = 0;
int hour;



void setup()
{
  // initialize serial communication:
  Serial.begin(9600);
  Serial.println(".... Hoe lang is de button ingedrukt? ....");
  Serial.print("Created: ");
  Serial.print(__TIME__);
  Serial.print(", ");
  Serial.println(__DATE__);
  Serial.println(__FILE__);

  // Initializa de button pin als een input with pullup active low.
  // verzeker dat de button van PIN naar GROUND
  pinMode(buttonPin, INPUT_PULLUP);

  //initialize button states
  buttonState = digitalRead(buttonPin);
  lastButtonState = buttonState;

  Serial.println("Setup done");
  Serial.println(" ");
}

void loop()
{
  // lezen van de pushbutton buttonpin:
  buttonState = digitalRead(buttonPin);
   

  // vergelijkt de buttonstaat naar de vorige staat
  if (buttonState != lastButtonState) // betekend dat hij veranderd is, welke kant nog onduidelijk
  {
    if (buttonState == LOW)  // veranderd naar ingedrukt
    {
      // als de momentele staat is LOW dan was de button ingedrukt
      Serial.print("Newly pressed at ");
      Serial.print(millis());
      Serial.print(" ms");
      buttonBecamePressedAt = millis();


    }
    else  // changed to released
    {
      // als de huidige staat HIGH dan was de button released
      Serial.print(", newly released at ");
      Serial.print(millis());
      Serial.println(" ms");

      buttonHasBeenPressedForThisTime = millis() - buttonBecamePressedAt;
      buttonHasBeenPressedForTotal = buttonHasBeenPressedForTotal + buttonHasBeenPressedForThisTime;
      Serial.print("   This press: ");
      Serial.print(buttonHasBeenPressedForThisTime);
      Serial.print(" ms");

      Serial.print(", Total: ");
      Serial.print(buttonHasBeenPressedForTotal);
      Serial.println(" ms");
      
      hour = buttonHasBeenPressedForTotal / 1000;

      Serial.println(hour);
      
      
   

      
    }
    // Delay om "stuiteren" te voorkomen, delay van 50ms is genoeg hiervoor
    delay(50);
  }
  // Verander de huidige staat als de vorige staat, voor de volgende keer door de loop
  lastButtonState = buttonState;
  
}

The output of my working code it counts the total ammount of seconds my input 3 was on:

0
Newly pressed at 25053 ms, newly released at 25751 ms
   This press: 698 ms, Total: 1035 ms
1
Newly pressed at 25994 ms, newly released at 26871 ms
   This press: 878 ms, Total: 1913 ms
1
Newly pressed at 27279 ms, newly released at 29432 ms
   This press: 2152 ms, Total: 4065 ms
4
Newly pressed at 29789 ms, newly released at 32500 ms
   This press: 2710 ms, Total: 6775 ms
6

It needs it to memorise the total time of being on. When i close the seriële output monitor or shut the power to the arduino, it restarts at 0. But it needs to keep counting (in this case it ends at total time: 6, so the next time the arduino will be shut on, it needs to keep counting from 6 and now it will start again at 0. The counting works, but thats why i need to EEPROM so it continious with counting the next time the arduino has power.. This is the code i have with EEPROM that does not work.. This is the code (with EEPROM) but it still does not safe the old variable (hour) and starts counting again from 0.......

#include <EEPROM.h>

// deze constante veranderen niet
const int  buttonPin = 3;    // waar de button op aangesloten is
//   the button moet aangesloten zijn vanaf pin naar ground, pinmode is een input_pullup

// Deze variabelen veranderen 
bool buttonState;         // momentele status van de button
bool lastButtonState;     // vorige status van de button

unsigned long buttonBecamePressedAt;
unsigned long buttonHasBeenPressedForTotal = 0;
unsigned long buttonHasBeenPressedForThisTime = 0;
int hour;
int AdressRoom = 15;


void setup()
{
  // initialize serial communication:
  Serial.begin(9600);
  Serial.println(".... Hoe lang is de button ingedrukt? ....");
  Serial.print("Created: ");
  Serial.print(__TIME__);
  Serial.print(", ");
  Serial.println(__DATE__);
  Serial.println(__FILE__);

  // Initializa de button pin als een input with pullup active low.
  // verzeker dat de button van PIN naar GROUND
  pinMode(buttonPin, INPUT_PULLUP);

  //initialize button states
  buttonState = digitalRead(buttonPin);
  lastButtonState = buttonState;

  Serial.println("Setup done");
  Serial.println(" ");
}

void loop()
{
  // lezen van de pushbutton buttonpin:
  buttonState = digitalRead(buttonPin);
   

  // vergelijkt de buttonstaat naar de vorige staat
  if (buttonState != lastButtonState) // betekend dat hij veranderd is, welke kant nog onduidelijk
  {
    if (buttonState == LOW)  // veranderd naar ingedrukt
    {
      // als de momentele staat is LOW dan was de button ingedrukt
      Serial.print("Newly pressed at ");
      Serial.print(millis());
      Serial.print(" ms");
      buttonBecamePressedAt = millis();


    }
    else  // changed to released
    {
      // als de huidige staat HIGH dan was de button released
      Serial.print(", newly released at ");
      Serial.print(millis());
      Serial.println(" ms");

      buttonHasBeenPressedForThisTime = millis() - buttonBecamePressedAt;
      buttonHasBeenPressedForTotal = buttonHasBeenPressedForTotal + buttonHasBeenPressedForThisTime;
      Serial.print("   This press: ");
      Serial.print(buttonHasBeenPressedForThisTime);
      Serial.print(" ms");

      Serial.print(", Total: ");
      Serial.print(buttonHasBeenPressedForTotal);
      Serial.println(" ms");
      
      hour = buttonHasBeenPressedForTotal / 1000;


      EEPROM.put(AdressRoom, hour);
      Serial.println(hour);
      
      
   

      
    }
    // Delay om "stuiteren" te voorkomen, delay van 50ms is genoeg hiervoor
    delay(50);
  }
  // Verander de huidige staat als de vorige staat, voor de volgende keer door de loop
  lastButtonState = buttonState;
  
}

In order to have the value previously saved to EEPROM preserved after a shutdown or reboot, which is what happens when you open the Serial monitor, you must read the value from EEPROM in setup() and initialise the variable with that value

Are you doing that ?

I dont think i’ve done that. I’ll get back within a few hours.. i am not home right now. I will try and reply later.

Do i need to initialize the variable in the setup aswell? Or can i do that in the loop?

You initialise the variable in setup() with the value that you read from EEPROM. That way it has the same value as the last time that you saved it

Right now i have this in my setup:

void setup()
{
  // initialize serial communication:
  Serial.begin(9600);
  EEPROM.write(AdressRoom, 0);

So that should set the room to 0 but it still wont countup, it keeps starting from 0. This code is in my loop.

EEPROM.get(AdressRoom, hour);
Serial.println(hour);
EEPROM.get(AdressRoom, hour);
Serial.println(hour);

This code you have in loop() should be in setup().

The code in setup() is just incorrect in two ways. First, you should not be setting the stored value to 0. You are just wiping out the previously stored value which you want to retrieve and use. Furthermore, using .write() instead of .put() to do this wrong thing, is only clearing one of the two bytes of storage space needed to hold the integer value.

No wonder when you set it to zero on startup or reboot

You were supposed to read the value from EEPROM on startup/reboot, not write it

Okay. i think i did it kind of right now. The code somewhat works. Now if i reboot the first number that i see is the last number it was when i was counting the time of pin 3 being high. After i see the good number it goes back to 0 and restarts counting. It needs to start counting from the point it last was. Here the console code:

This is working! It is counting the ms of pin 3 being high! now i turn the power off.

Newly pressed at 9550 ms, newly released at 9762 ms
6
   This press: 213 ms, Total: 6918 ms
6
Newly pressed at 13737 ms, newly released at 14102 ms
6
   This press: 365 ms, Total: 7286 ms
7

After i turn the power back on i get this, the first number is good (last number before turning off was 7). But than it starts counting from 0 again :((.

Newly pressed at 1564 ms, newly released at 3216 ms
7
   This press: 1651 ms, Total: 1654 ms
1
Newly pressed at 3747 ms, newly released at 4504 ms
1
   This press: 757 ms, Total: 2414 ms
2

This is my full code now, can anyone tell / highlight what i need to change?, I tried putting the "EEPROM.put(AdressRoom, hour);" in my setup, but that didnt work aswell...

#include <EEPROM.h>
// deze constante veranderen niet
const int  buttonPin = 3;    // waar de button op aangesloten is
//   the button moet aangesloten zijn vanaf pin naar ground, pinmode is een input_pullup

// Deze variabelen veranderen 
bool buttonState;         // momentele status van de button
bool lastButtonState;     // vorige status van de button

unsigned long buttonBecamePressedAt;
unsigned long buttonHasBeenPressedForTotal = 0;
unsigned long buttonHasBeenPressedForThisTime = 0;
int hour;
int AdressRoom = 50;


void setup()
{
  // initialize serial communication:
  Serial.begin(9600);
  
  EEPROM.read(AdressRoom);
  
  Serial.println(".... Hoe lang is de button ingedrukt? ....");
  Serial.print("Created: ");
  Serial.print(__TIME__);
  Serial.print(", ");
  Serial.println(__DATE__);
  Serial.println(__FILE__);

  // Initializa de button pin als een input with pullup active low.
  // verzeker dat de button van PIN naar GROUND
  pinMode(buttonPin, INPUT_PULLUP);

  //initialize button states
  buttonState = digitalRead(buttonPin);
  lastButtonState = buttonState;

  Serial.println("Setup done");
  Serial.println(" ");
}

void loop()
{
  // lezen van de pushbutton buttonpin:
  buttonState = digitalRead(buttonPin);

  // vergelijkt de buttonstaat naar de vorige staat
  if (buttonState != lastButtonState) // betekend dat hij veranderd is, welke kant nog onduidelijk
  {
    if (buttonState == LOW)  // veranderd naar ingedrukt
    {
      // als de momentele staat is LOW dan was de button ingedrukt
      Serial.print("Newly pressed at ");
      Serial.print(millis());
      Serial.print(" ms");
      buttonBecamePressedAt = millis();

    }
    else  // changed to released
    {
      // als de huidige staat HIGH dan was de button released
      Serial.print(", newly released at ");
      Serial.print(millis());
      Serial.println(" ms");

      buttonHasBeenPressedForThisTime = millis() - buttonBecamePressedAt;
      buttonHasBeenPressedForTotal = buttonHasBeenPressedForTotal + buttonHasBeenPressedForThisTime + Serial.println(EEPROM.get(AdressRoom, hour));;
      Serial.print("   This press: ");
      Serial.print(buttonHasBeenPressedForThisTime);
      Serial.print(" ms");

      Serial.print(", Total: ");
      Serial.print(buttonHasBeenPressedForTotal);
      Serial.println(" ms");
      hour = buttonHasBeenPressedForTotal / 1000;

      EEPROM.put(AdressRoom, hour);
      Serial.println(hour);
     
      
      
      
    }
    // Delay om "stuiteren" te voorkomen, delay van 50ms is genoeg hiervoor
    delay(50);
  }
  // Verander de huidige staat als de vorige staat, voor de volgende keer door de loop
  lastButtonState = buttonState;

} 

I am sure that we have been here before in this topic but here we go again

int AdressRoom = 50;

AddressRoom is an int. It takes 2 bytes of storage to hold its value

  EEPROM.read(AdressRoom);

EEPROM.read() reads a single byte

You have previously been advised to use EEPROM.get() to load an int from EEPROM

Yes i tried it but it still won't work when i put:

 EEPROM.get(AdressRoom, hour);

it still counts again from 0 when i restart instead of keep counting where it was..... i dont know what i am doing wrong, anyone who can edit my code so i might understand better what i am doing wrong...

I won't "edit" your code for you but if you post the latest version I will read it.

This does not do much. Read and throw away the result. You should use EEPROM.get() as indicated a number of times.

This is my latest version:

Someone reply'd i should add EEPROM.put(AdressRoom, hour); into the setup() instead of loop() but that didn't work aswell.

#include <EEPROM.h>
// deze constante veranderen niet
const int  buttonPin = 3;    // waar de button op aangesloten is
//   the button moet aangesloten zijn vanaf pin naar ground, pinmode is een input_pullup

// Deze variabelen veranderen 
bool buttonState;         // momentele status van de button
bool lastButtonState;     // vorige status van de button

unsigned long buttonBecamePressedAt;
unsigned long buttonHasBeenPressedForTotal = 0;
unsigned long buttonHasBeenPressedForThisTime = 0;
int hour;
int AdressRoom = 50;


void setup()
{
  // initialize serial communication:
  Serial.begin(9600);
  
  EEPROM.get(AdressRoom, hour);
  
  Serial.println(".... Hoe lang is de button ingedrukt? ....");
  Serial.print("Created: ");
  Serial.print(__TIME__);
  Serial.print(", ");
  Serial.println(__DATE__);
  Serial.println(__FILE__);

  // Initializa de button pin als een input with pullup active low.
  // verzeker dat de button van PIN naar GROUND
  pinMode(buttonPin, INPUT_PULLUP);

  //initialize button states
  buttonState = digitalRead(buttonPin);
  lastButtonState = buttonState;

  Serial.println("Setup done");
  Serial.println(" ");
}

void loop()
{
  // lezen van de pushbutton buttonpin:
  buttonState = digitalRead(buttonPin);

  // vergelijkt de buttonstaat naar de vorige staat
  if (buttonState != lastButtonState) // betekend dat hij veranderd is, welke kant nog onduidelijk
  {
    if (buttonState == LOW)  // veranderd naar ingedrukt
    {
      // als de momentele staat is LOW dan was de button ingedrukt
      Serial.print("Newly pressed at ");
      Serial.print(millis());
      Serial.print(" ms");
      buttonBecamePressedAt = millis();

    }
    else  // changed to released
    {
      // als de huidige staat HIGH dan was de button released
      Serial.print(", newly released at ");
      Serial.print(millis());
      Serial.println(" ms");

      buttonHasBeenPressedForThisTime = millis() - buttonBecamePressedAt;
      buttonHasBeenPressedForTotal = buttonHasBeenPressedForTotal + buttonHasBeenPressedForThisTime + EEPROM.get(AdressRoom, hour);
      Serial.print("   This press: ");
      Serial.print(buttonHasBeenPressedForThisTime);
      Serial.print(" ms");

      Serial.print(", Total: ");
      Serial.print(buttonHasBeenPressedForTotal);
      Serial.println(" ms");
      hour = buttonHasBeenPressedForTotal / 1000;

      
      Serial.println(hour);
      EEPROM.put(AdressRoom, hour);
     
      
      
      
    }
    // Delay om "stuiteren" te voorkomen, delay van 50ms is genoeg hiervoor
    delay(50);
  }
  // Verander de huidige staat als de vorige staat, voor de volgende keer door de loop
  lastButtonState = buttonState;

} 
      hour = buttonHasBeenPressedForTotal / 1000;
      Serial.println(hour);

What is printed here ?

the time of pin3 being connected (in this case connected to the ground). It needs to count the time of pin3 being high. After arduino reboot / shutdown it needs to memorise the last number it was at. And keep counting foward from that. You can see in de code below when i pressed it and released it. The number underneith (in this case 2, 4, 5) is the ammount of seconds it was connected in total. So after a reboot, it needs to continue from 5. Now the counting gets lost, and starts over again from 0.

Newly pressed at 2907 ms, newly released at 4948 ms
   This press: 2040 ms, Total: 2834 ms
2
Newly pressed at 10070 ms, newly released at 11373 ms
   This press: 1302 ms, Total: 4138 ms
4
Newly pressed at 11983 ms, newly released at 12850 ms
   This press: 868 ms, Total: 5010 ms
5

See comments I've added to your code, above. Add to that the incorrect usage of longs and ints, and you're a long ways from getting this right.
C

  1. always check calculations for correct units of all elements
  2. use appropriate variable sizes - working with millis(), all variables and constants should be long.
  3. A variable called "hour" implies conversions from ms, to s, to m, to h; I see only conversions from ms to s, and only some of those.
    C

You need add on to the saved value of your variable, not just assign a new value to it.

//hour = buttonHasBeenPressedForTotal / 1000;
hour = hour + buttonHasBeenPressedForTotal / 1000;
//alternative syntax hour += buttonHasBeenPressedForTotal / 1000;