Advertisement
pleasedontcode

Button Tracker rev_01

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