Advertisement
pleasedontcode

LED Control rev_01

Sep 14th, 2024
77
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: LED Control
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-09-14 16:50:18
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Implement a simple LED control system using a push */
  21.     /* button connected to digital pin D4, utilizing the */
  22.     /* EasyButton library for debouncing and state */
  23.     /* management. The system should toggle the LED state */
  24.     /* on button press. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <EasyButton.h> // https://github.com/evert-arias/EasyButton
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33.  
  34. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  35. const uint8_t Switch_PushButton_PIN_D4 = 4; // Pin for the push button
  36. const uint8_t LED_PIN = 2; // Pin for the LED
  37.  
  38. /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
  39. // Create an instance of the EasyButton class for the push button
  40. // The constructor takes the pin number as an argument
  41. EasyButton button(Switch_PushButton_PIN_D4);
  42.  
  43. /****** CALLBACK FUNCTION PROTOTYPES *****/
  44. // Function to be called when the button is pressed
  45. void onPressed(void);
  46.  
  47. /****** SETUP FUNCTION *****/
  48. void setup(void)
  49. {
  50.     // Initialize Serial for debugging purposes
  51.     Serial.begin(115200);
  52.     Serial.println();
  53.     Serial.println(">>> EasyButton LED Control Example <<<");
  54.  
  55.     // Initialize the button
  56.     button.begin();
  57.    
  58.     // Initialize the LED pin as output
  59.     pinMode(LED_PIN, OUTPUT);
  60.     digitalWrite(LED_PIN, LOW); // Start with LED off
  61.  
  62.     // Add the callback function to be called when the button is pressed
  63.     button.onPressed(onPressed);
  64. }
  65.  
  66. /****** LOOP FUNCTION *****/
  67. void loop(void)
  68. {
  69.     // Continuously read the status of the button
  70.     button.read();
  71. }
  72.  
  73. /****** CALLBACK FUNCTION IMPLEMENTATION *****/
  74. // Function that gets called when the button is pressed
  75. void onPressed(void)
  76. {
  77.     // Toggle the LED state
  78.     static bool ledState = LOW; // Variable to hold the current state of the LED
  79.     ledState = !ledState; // Toggle the state
  80.     digitalWrite(LED_PIN, ledState); // Update the LED state
  81.     Serial.println(ledState ? "LED is ON" : "LED is OFF"); // Print the current state
  82. }
  83.  
  84. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement