Advertisement
pleasedontcode

Track Time rev_04

May 11th, 2024
796
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Track Time
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-05-11 22:02:42
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* I need a counter to track seconds upon board is */
  21.     /* switched on. The counter starts at 0 on flashing. */
  22.     /* Counter value to be store in EEPROM when the board */
  23.     /* is powering off. */
  24. /****** SYSTEM REQUIREMENT 2 *****/
  25.     /* At each power on the counter shall be read from */
  26.     /* EEPROM and continue to sum the seconds. */
  27. /****** END SYSTEM REQUIREMENTS *****/
  28.  
  29.  
  30. /********* User code review feedback **********
  31. #### Feedback 1 ####
  32. - track minutes, not seconds.
  33. #### Feedback 2 ####
  34. - minutesCounter shall be updated each minute using millis functio
  35. n.
  36. #### Feedback 3 ####
  37. - update of secondsCounter shall be updated with exact increase of
  38.  difference between currentMillis and previousMillis.
  39. ********* User code review feedback **********/
  40.  
  41. /****** DEFINITION OF LIBRARIES *****/
  42. #include <EasyButton.h>  // https://github.com/evert-arias/EasyButton
  43. #include <EEPROM.h>
  44.  
  45. /****** FUNCTION PROTOTYPES *****/
  46. void setup(void);
  47. void loop(void);
  48. void saveCounterToEEPROM(void);
  49. void powerOff(void);
  50.  
  51. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  52. const uint8_t button_PushButton_PIN_D4 = 4;
  53.  
  54. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  55. // Arduino pin where the button is connected to.
  56. #define BUTTON_PIN 4
  57.  
  58. // Instance of the button.
  59. EasyButton button(BUTTON_PIN);
  60.  
  61. // Counter to track seconds
  62. unsigned long secondsCounter = 0;
  63. unsigned long previousMillis = 0; // Variable to store the previous millis value
  64.  
  65. void onPressedForDuration()
  66. {
  67.   // Code to be executed when the button is pressed for the given duration.
  68. }
  69.  
  70. void setup()
  71. {
  72.   // put your setup code here, to run once:
  73.   pinMode(button_PushButton_PIN_D4, INPUT_PULLUP);
  74.  
  75.   // Read the counter value from EEPROM
  76.   EEPROM.begin(sizeof(secondsCounter));
  77.   EEPROM.get(0, secondsCounter);
  78.  
  79.   // Initialize the button.
  80.   button.begin();
  81.   // Add the callback function to be called when the button is pressed for at least the given time.
  82.   button.onPressedFor(120000, onPressedForDuration); // 120,000 milliseconds = 2 minutes
  83.  
  84.   // Convert secondsCounter to minutes (assuming each minute is 60 seconds)
  85.   unsigned long minutesCounter = secondsCounter / 60;
  86.  
  87.   // Update the previousMillis variable
  88.   previousMillis = millis();
  89. }
  90.  
  91. void loop()
  92. {
  93.   // put your main code here, to run repeatedly:
  94.   // Continuously read the status of the button.
  95.   button.read();
  96.  
  97.   // Update the seconds counter using millis function
  98.   unsigned long currentMillis = millis();
  99.   unsigned long elapsedTime = currentMillis - previousMillis;
  100.   if (elapsedTime >= 1000) {
  101.     secondsCounter += (elapsedTime / 1000); // Update secondsCounter with the exact increase
  102.     previousMillis = currentMillis;
  103.   }
  104.  
  105.   // Update the minutes counter
  106.   unsigned long minutesCounter = secondsCounter / 60;
  107.  
  108.   if (secondsCounter % 60 == 0) {
  109.     minutesCounter++;
  110.   }
  111.  
  112.   // Save the counter value to EEPROM every minute
  113.   if (secondsCounter % 60 == 0) {
  114.     saveCounterToEEPROM();
  115.   }
  116. }
  117.  
  118. void saveCounterToEEPROM()
  119. {
  120.   // Save the counter value to EEPROM
  121.   EEPROM.put(0, secondsCounter);
  122.   EEPROM.commit();
  123. }
  124.  
  125. void powerOff()
  126. {
  127.   // Call the function to save the counter value to EEPROM
  128.   saveCounterToEEPROM();
  129.  
  130.   // Power off the board
  131. }
  132.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement