/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: Track Time
	- Source Code NOT compiled for: ESP32 DevKit V1
	- Source Code created on: 2024-05-11 21:51:21

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* I need a counter to track seconds upon board is */
	/* switched on. The counter starts at 0 on flashing. */
	/* Counter value to be store in EEPROM when the board */
	/* is powering off. */
/****** SYSTEM REQUIREMENT 2 *****/
	/* At each power on the counter shall be read from */
	/* EEPROM and continue to sum the seconds. */
/****** END SYSTEM REQUIREMENTS *****/


/********* User code review feedback **********
#### Feedback 1 ####
- track minutes, not seconds.
********* User code review feedback **********/

/****** DEFINITION OF LIBRARIES *****/
#include <EasyButton.h>  // https://github.com/evert-arias/EasyButton
#include <EEPROM.h>

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void saveCounterToEEPROM(void);
void powerOff(void);

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t button_PushButton_PIN_D4 = 4;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
// Arduino pin where the button is connected to.
#define BUTTON_PIN 4

// Instance of the button.
EasyButton button(BUTTON_PIN);

// Counter to track minutes
unsigned long minutesCounter = 0;

void onPressedForDuration()
{
  // Code to be executed when the button is pressed for the given duration.
}

void setup()
{
  // put your setup code here, to run once:
  pinMode(button_PushButton_PIN_D4, INPUT_PULLUP);

  // Read the counter value from EEPROM
  EEPROM.begin(sizeof(minutesCounter));
  EEPROM.get(0, minutesCounter);

  // Initialize the button.
  button.begin();
  // Add the callback function to be called when the button is pressed for at least the given time.
  button.onPressedFor(120000, onPressedForDuration); // 120,000 milliseconds = 2 minutes

  // Convert secondsCounter to minutes (assuming each minute is 60 seconds)
  minutesCounter = secondsCounter / 60;
}

void loop()
{
  // put your main code here, to run repeatedly:
  // Continuously read the status of the button.
  button.read();

  // Update the minutes counter
  minutesCounter++;
}

void saveCounterToEEPROM()
{
  // Save the counter value to EEPROM
  EEPROM.put(0, minutesCounter);
  EEPROM.commit();
}

void powerOff()
{
  // Call the function to save the counter value to EEPROM
  saveCounterToEEPROM();

  // Power off the board
}
