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;
}