Advertisement
pleasedontcode

"Button LED" rev_01

Jun 21st, 2024
416
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 LED"
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-06-21 14:40:29
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Develop an Arduino project using the EasyButton */
  21.     /* library to control an LED connected to pin D3. The */
  22.     /* LED should toggle its state each time a push */
  23.     /* button connected to pin D2 is pressed. Ensure the */
  24.     /* system initializes correctly in the setup */
  25.     /* function. */
  26. /****** END SYSTEM REQUIREMENTS *****/
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <EasyButton.h> //https://github.com/evert-arias/EasyButton
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34. void onPressed(void);
  35. void updateOutputs(void);
  36.  
  37. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  38. const uint8_t Botao_PushButton_PIN_D2 = 2;
  39.  
  40. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  41. const uint8_t Led_LED_PIN_D3 = 3;
  42.  
  43. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  44. /***** used to store raw data *****/
  45. bool Led_LED_PIN_D3_rawData = 0;
  46.  
  47. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  48. /***** used to store data after characteristic curve transformation *****/
  49. float Led_LED_PIN_D3_phyData = 0.0;
  50.  
  51. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  52. EasyButton button(Botao_PushButton_PIN_D2); // Initialize EasyButton object
  53.  
  54. void setup(void)
  55. {
  56.     // put your setup code here, to run once:
  57.  
  58.     pinMode(Botao_PushButton_PIN_D2, INPUT_PULLUP); // Set button pin as input with internal pull-up
  59.     pinMode(Led_LED_PIN_D3, OUTPUT); // Set LED pin as output
  60.  
  61.     Serial.begin(115200); // Initialize serial communication
  62.     Serial.println();
  63.     Serial.println(">>> EasyButton pressed example <<<");
  64.  
  65.     button.begin(); // Initialize the button
  66.     button.onPressed(onPressed); // Set the callback function for button press
  67. }
  68.  
  69. void loop(void)
  70. {
  71.     // put your main code here, to run repeatedly:
  72.  
  73.     button.read(); // Continuously read the status of the button
  74.     updateOutputs(); // Refresh output data
  75. }
  76.  
  77. void onPressed()
  78. {
  79.     Serial.println("Button pressed");
  80.     Led_LED_PIN_D3_rawData = !Led_LED_PIN_D3_rawData; // Toggle LED state
  81. }
  82.  
  83. void updateOutputs()
  84. {
  85.     digitalWrite(Led_LED_PIN_D3, Led_LED_PIN_D3_rawData); // Update the LED state
  86. }
  87.  
  88. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement