Advertisement
pleasedontcode

Button Control rev_03

Dec 29th, 2023
73
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 Control
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2023-12-29 09:24:34
  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 in void */
  25.     /* loop */
  26. /****** END SYSTEM REQUIREMENTS *****/
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <Arduino.h>
  30. #include <EasyButton.h>
  31. #include <avr/eeprom.h>
  32.  
  33. /****** FUNCTION PROTOTYPES *****/
  34. void setup(void);
  35. void loop(void);
  36. void saveStateToEEPROM(uint8_t state);
  37.  
  38. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  39. const uint8_t P1_PushButton_PIN_D2 = 2;
  40. const uint8_t P2_PushButton_PIN_D3 = 3;
  41.  
  42. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  43. const uint8_t led_LED_PIN_D4 = 4;
  44.  
  45. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  46. EasyButton button1(P1_PushButton_PIN_D2);
  47. EasyButton button2(P2_PushButton_PIN_D3);
  48.  
  49. void setup(void)
  50. {
  51.   // put your setup code here, to run once:
  52.   pinMode(led_LED_PIN_D4, OUTPUT);
  53.  
  54.   button1.begin();
  55.   button2.begin();
  56.  
  57.   // Read the saved state from EEPROM and set the LED accordingly
  58.   uint8_t savedState = eeprom_read_byte((uint8_t*)0);
  59.   digitalWrite(led_LED_PIN_D4, savedState);
  60. }
  61.  
  62. void loop(void)
  63. {
  64.   // put your main code here, to run repeatedly:
  65.   button1.read();
  66.   button2.read();
  67.  
  68.   // Check if button1 is pressed and save the state to EEPROM
  69.   if (button1.isPressed()) {
  70.     digitalWrite(led_LED_PIN_D4, HIGH);
  71.     saveStateToEEPROM(HIGH);
  72.   }
  73.  
  74.   // Check if button2 is pressed and save the state to EEPROM
  75.   if (button2.isPressed()) {
  76.     digitalWrite(led_LED_PIN_D4, LOW);
  77.     saveStateToEEPROM(LOW);
  78.   }
  79. }
  80.  
  81. void saveStateToEEPROM(uint8_t state)
  82. {
  83.   eeprom_write_byte((uint8_t*)0, state);
  84.   eeprom_busy_wait();
  85. }
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement