Advertisement
pleasedontcode

LED Toggle rev_01

Aug 27th, 2024
128
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 Toggle
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-08-27 09:57:23
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* when pressin button1 led1 turns on and off whith */
  21.     /* each press. while led1 is on i can press button2 */
  22.     /* to turn led off while pressing the button2 but if */
  23.     /* i release button2 the led1 turns on again */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <EasyButton.h> //https://github.com/evert-arias/EasyButton
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup(void);
  31. void loop(void);
  32. void updateOutputs(void); // Function prototype for updating outputs
  33.  
  34. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  35. const uint8_t button1_PushButton_PIN_D2     = 2;
  36. const uint8_t Button2_PushButton_PIN_D3     = 3;
  37.  
  38. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  39. const uint8_t LED1_LED_PIN_D4       = 4;
  40.  
  41. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  42. /***** used to store raw data *****/
  43. bool LED1_LED_PIN_D4_rawData = false; // Initialize LED state to off
  44.  
  45. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  46. // Initialize EasyButton objects for both buttons
  47. EasyButton button1(button1_PushButton_PIN_D2); // Create an EasyButton object for button 1
  48. EasyButton button2(Button2_PushButton_PIN_D3);  // Create an EasyButton object for button 2
  49.  
  50. void setup(void)
  51. {
  52.     // Initialize serial communication for debugging
  53.     Serial.begin(115200);
  54.  
  55.     // Initialize button pins with internal pull-up resistors
  56.     pinMode(button1_PushButton_PIN_D2, INPUT_PULLUP);
  57.     pinMode(Button2_PushButton_PIN_D3, INPUT_PULLUP);
  58.  
  59.     // Initialize LED pin as output
  60.     pinMode(LED1_LED_PIN_D4, OUTPUT);
  61.  
  62.     // Begin the EasyButton instances
  63.     button1.begin(); // Initialize button 1
  64.     button2.begin(); // Initialize button 2
  65.  
  66.     // Attach functions to button press events
  67.     button1.onPressed([]() {
  68.         Serial.println("Button 1 pressed");
  69.         LED1_LED_PIN_D4_rawData = !LED1_LED_PIN_D4_rawData; // Toggle LED state
  70.     });
  71.  
  72.     button2.onPressed([]() {
  73.         Serial.println("Button 2 pressed");
  74.         // Turn off LED while button 2 is pressed
  75.         LED1_LED_PIN_D4_rawData = false;
  76.     });
  77. }
  78.  
  79. void loop(void)
  80. {
  81.     // Read the button states
  82.     button1.read(); // Read button 1 state
  83.     button2.read(); // Read button 2 state
  84.  
  85.     // If button 2 is released, turn LED back on if it was previously on
  86.     if (button2.isReleased() && LED1_LED_PIN_D4_rawData) {
  87.         LED1_LED_PIN_D4_rawData = true; // Keep LED on
  88.     }
  89.  
  90.     // Refresh output data
  91.     updateOutputs();
  92. }
  93.  
  94. void updateOutputs()
  95. {
  96.     digitalWrite(LED1_LED_PIN_D4, LED1_LED_PIN_D4_rawData); // Update LED state based on raw data
  97. }
  98.  
  99. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement