Advertisement
pleasedontcode

"Toggle LED" rev_01

Jun 15th, 2024
360
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: "Toggle LED"
  13.     - Source Code NOT compiled for: Arduino Pro Mini 5V
  14.     - Source Code created on: 2024-06-15 10:33:45
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Develop a button press detection system using the */
  21.     /* EasyButton library. Initialize the button on pin */
  22.     /* D2 with an internal pull-up resistor. On button */
  23.     /* press, toggle an LED on pin D13 and print "Button */
  24.     /* State Changed" to the serial monitor. */
  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. void onPressed(void);
  34.  
  35. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  36. const uint8_t button_PushButton_PIN_D2 = 2;
  37. const uint8_t led_PIN_D13 = 13;
  38.  
  39. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  40. EasyButton button(button_PushButton_PIN_D2);  // Initialize EasyButton object with the pin number
  41.  
  42. /****** CALLBACK FUNCTION *****/
  43. void onPressed()
  44. {
  45.   // Toggle the LED state
  46.   static bool ledState = LOW;
  47.   ledState = !ledState;
  48.   digitalWrite(led_PIN_D13, ledState);
  49.  
  50.   // Print message to the serial monitor
  51.   Serial.println("Button State Changed");
  52. }
  53.  
  54. void setup(void)
  55. {
  56.   // Initialize Serial for debugging purposes
  57.   Serial.begin(115200);
  58.   Serial.println();
  59.   Serial.println(">>> EasyButton pressed example <<<");
  60.  
  61.   // Initialize the LED pin as an output
  62.   pinMode(led_PIN_D13, OUTPUT);
  63.   digitalWrite(led_PIN_D13, LOW);  // Ensure the LED is off initially
  64.  
  65.   // Initialize the button
  66.   button.begin();
  67.   // Add the callback function to be called when the button is pressed
  68.   button.onPressed(onPressed);
  69. }
  70.  
  71. void loop(void)
  72. {
  73.   // Continuously read the status of the button
  74.   button.read();
  75. }
  76.  
  77. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement