Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* 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 22:02:42
- ********* 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.
- #### Feedback 2 ####
- - minutesCounter shall be updated each minute using millis functio
- n.
- #### Feedback 3 ####
- - update of secondsCounter shall be updated with exact increase of
- difference between currentMillis and previousMillis.
- ********* 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 seconds
- unsigned long secondsCounter = 0;
- unsigned long previousMillis = 0; // Variable to store the previous millis value
- 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(secondsCounter));
- EEPROM.get(0, secondsCounter);
- // 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)
- unsigned long minutesCounter = secondsCounter / 60;
- // Update the previousMillis variable
- previousMillis = millis();
- }
- void loop()
- {
- // put your main code here, to run repeatedly:
- // Continuously read the status of the button.
- button.read();
- // Update the seconds counter using millis function
- unsigned long currentMillis = millis();
- unsigned long elapsedTime = currentMillis - previousMillis;
- if (elapsedTime >= 1000) {
- secondsCounter += (elapsedTime / 1000); // Update secondsCounter with the exact increase
- previousMillis = currentMillis;
- }
- // Update the minutes counter
- unsigned long minutesCounter = secondsCounter / 60;
- if (secondsCounter % 60 == 0) {
- minutesCounter++;
- }
- // Save the counter value to EEPROM every minute
- if (secondsCounter % 60 == 0) {
- saveCounterToEEPROM();
- }
- }
- void saveCounterToEEPROM()
- {
- // Save the counter value to EEPROM
- EEPROM.put(0, secondsCounter);
- EEPROM.commit();
- }
- void powerOff()
- {
- // Call the function to save the counter value to EEPROM
- saveCounterToEEPROM();
- // Power off the board
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement