Advertisement
pleasedontcode

Track Time rev_02

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