Advertisement
pleasedontcode

Button LED rev_02

Dec 29th, 2023
81
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 compiled for: Arduino Uno
  14.     - Source Code created on: 2023-12-29 08:25:55
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* if pushbutton P1 is switched ON then Output led */
  21.     /* will turn ON and remain ON, this output led status */
  22.     /* will be saved to EEPROM. And if pushbutton p2 is */
  23.     /* switched ON then output led will turn OFF and */
  24.     /* remain OFF. This will also save to EEPROM. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <Arduino.h>
  29. #include <EasyButton.h>
  30. #include <EEPROM.h>
  31.  
  32. /****** FUNCTION PROTOTYPES *****/
  33. void setup(void);
  34. void loop(void);
  35.  
  36. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  37. const uint8_t P1_PushButton_PIN_D2 = 2;
  38. const uint8_t P2_PushButton_PIN_D3 = 3;
  39.  
  40. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  41. const uint8_t led_LED_PIN_D4 = 4;
  42.  
  43. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  44. EasyButton button1(P1_PushButton_PIN_D2);
  45. EasyButton button2(P2_PushButton_PIN_D3);
  46.  
  47. bool ledState = false; // Variable to store the state of the LED
  48.  
  49. void setup(void)
  50. {
  51.   // put your setup code here, to run once:
  52.  
  53.   pinMode(P1_PushButton_PIN_D2, INPUT_PULLUP);
  54.   pinMode(P2_PushButton_PIN_D3, INPUT_PULLUP);
  55.  
  56.   pinMode(led_LED_PIN_D4, OUTPUT);
  57.  
  58.   // Read the LED state from EEPROM
  59.   ledState = EEPROM.read(0);
  60.  
  61.   // Set the initial state of the LED
  62.   digitalWrite(led_LED_PIN_D4, ledState);
  63.  
  64.   // Set callback functions for button1 and button2
  65.   button1.onPressed([]() {
  66.     ledState = true;
  67.     digitalWrite(led_LED_PIN_D4, ledState);
  68.     EEPROM.write(0, ledState);
  69.   });
  70.  
  71.   button2.onPressed([]() {
  72.     ledState = false;
  73.     digitalWrite(led_LED_PIN_D4, ledState);
  74.     EEPROM.write(0, ledState);
  75.   });
  76.  
  77.   // Begin button interrupts
  78.   button1.begin();
  79.   button2.begin();
  80. }
  81.  
  82. void loop(void)
  83. {
  84.   // put your main code here, to run repeatedly:
  85.  
  86.   // Update button states
  87.   button1.read();
  88.   button2.read();
  89. }
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement