Advertisement
pleasedontcode

**Button Counter** rev_01

Mar 3rd, 2025
206
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 Counter**
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2025-03-03 15:01:43
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* allora quando io premo il pulsante collegato al */
  21.     /* pin 0 deve scrivere sul display il numero 0, */
  22.     /* continuando in su fino al pin nove che a sua volta */
  23.     /* dovra scrivere/stampare il numero 9 sul display */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /* START CODE */
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <EasyButton.h> //https://github.com/evert-arias/EasyButton
  30. #include <LCD-SOLDERED.h>   //https://github.com/SolderedElectronics/Soldered-LCD-library
  31.  
  32. /****** FUNCTION PROTOTYPES *****/
  33. void setup(void);
  34. void loop(void);
  35.  
  36. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  37. const uint8_t PushButton_PIN_D2 = 2; // Fixed naming convention
  38.  
  39. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  40. EasyButton button(PushButton_PIN_D2); // Create an instance of EasyButton
  41. LCD lcd; // Create an instance of the LCD class
  42.  
  43. int counter = 0; // Counter to keep track of button presses
  44.  
  45. void setup(void)
  46. {
  47.     // put your setup code here, to run once:
  48.     pinMode(PushButton_PIN_D2, INPUT_PULLUP); // Set the button pin as input with pull-up
  49.  
  50.     lcd.begin(); // Initialize the LCD
  51.     lcd.print("Press the button"); // Initial message on the LCD
  52.  
  53.     button.begin(); // Initialize the button
  54.     button.onPressed(incrementCounter); // Set the function to call on button press
  55. }
  56.  
  57. void loop(void)
  58. {
  59.     // put your main code here, to run repeatedly:
  60.     button.read(); // Read the button state
  61. }
  62.  
  63. // Function to increment the counter and update the display
  64. void incrementCounter() {
  65.     if (counter < 9) {
  66.         counter++; // Increment the counter
  67.     } else {
  68.         counter = 0; // Reset the counter if it exceeds 9
  69.     }
  70.     lcd.clear(); // Clear the LCD
  71.     lcd.print(counter); // Display the current counter value
  72. }
  73.  
  74. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement