Advertisement
pleasedontcode

Track Time rev_03

May 11th, 2024
911
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 21:53:06
  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. ********* User code review feedback **********/
  37.  
  38. /****** DEFINITION OF LIBRARIES *****/
  39. #include <EasyButton.h>  // https://github.com/evert-arias/EasyButton
  40. #include <EEPROM.h>
  41.  
  42. /****** FUNCTION PROTOTYPES *****/
  43. void setup(void);
  44. void loop(void);
  45. void saveCounterToEEPROM(void);
  46. void powerOff(void);
  47.  
  48. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  49. const uint8_t button_PushButton_PIN_D4 = 4;
  50.  
  51. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  52. // Arduino pin where the button is connected to.
  53. #define BUTTON_PIN 4
  54.  
  55. // Instance of the button.
  56. EasyButton button(BUTTON_PIN);
  57.  
  58. // Counter to track seconds
  59. unsigned long secondsCounter = 0;
  60.  
  61. void onPressedForDuration()
  62. {
  63.   // Code to be executed when the button is pressed for the given duration.
  64. }
  65.  
  66. void setup()
  67. {
  68.   // put your setup code here, to run once:
  69.   pinMode(button_PushButton_PIN_D4, INPUT_PULLUP);
  70.  
  71.   // Read the counter value from EEPROM
  72.   EEPROM.begin(sizeof(secondsCounter));
  73.   EEPROM.get(0, secondsCounter);
  74.  
  75.   // Initialize the button.
  76.   button.begin();
  77.   // Add the callback function to be called when the button is pressed for at least the given time.
  78.   button.onPressedFor(120000, onPressedForDuration); // 120,000 milliseconds = 2 minutes
  79.  
  80.   // Convert secondsCounter to minutes (assuming each minute is 60 seconds)
  81.   minutesCounter = secondsCounter / 60;
  82. }
  83.  
  84. void loop()
  85. {
  86.   // put your main code here, to run repeatedly:
  87.   // Continuously read the status of the button.
  88.   button.read();
  89.  
  90.   // Update the seconds counter using millis function
  91.   unsigned long currentMillis = millis();
  92.   if (currentMillis - previousMillis >= 1000) {
  93.     secondsCounter++;
  94.     previousMillis = currentMillis;
  95.   }
  96.  
  97.   // Update the minutes counter
  98.   if (secondsCounter % 60 == 0) {
  99.     minutesCounter++;
  100.   }
  101. }
  102.  
  103. void saveCounterToEEPROM()
  104. {
  105.   // Save the counter value to EEPROM
  106.   EEPROM.put(0, secondsCounter);
  107.   EEPROM.commit();
  108. }
  109.  
  110. void powerOff()
  111. {
  112.   // Call the function to save the counter value to EEPROM
  113.   saveCounterToEEPROM();
  114.  
  115.   // Power off the board
  116. }
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement