Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: Button Control
- - Source Code compiled for: Arduino Uno
- - Source Code created on: 2023-12-29 09:24:34
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* if pushbutton P1 is switched ON then Output led */
- /* will turn ON and remain ON, this output led status */
- /* will be saved to EEPROM. And if pushbutton p2 is */
- /* switched ON then output led will turn OFF and */
- /* remain OFF. This will also save to EEPROM in void */
- /* loop */
- /****** END SYSTEM REQUIREMENTS *****/
- /****** DEFINITION OF LIBRARIES *****/
- #include <Arduino.h>
- #include <EasyButton.h>
- #include <avr/eeprom.h>
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void saveStateToEEPROM(uint8_t state);
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t P1_PushButton_PIN_D2 = 2;
- const uint8_t P2_PushButton_PIN_D3 = 3;
- /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
- const uint8_t led_LED_PIN_D4 = 4;
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- EasyButton button1(P1_PushButton_PIN_D2);
- EasyButton button2(P2_PushButton_PIN_D3);
- void setup(void)
- {
- // put your setup code here, to run once:
- pinMode(led_LED_PIN_D4, OUTPUT);
- button1.begin();
- button2.begin();
- // Read the saved state from EEPROM and set the LED accordingly
- uint8_t savedState = eeprom_read_byte((uint8_t*)0);
- digitalWrite(led_LED_PIN_D4, savedState);
- }
- void loop(void)
- {
- // put your main code here, to run repeatedly:
- button1.read();
- button2.read();
- // Check if button1 is pressed and save the state to EEPROM
- if (button1.isPressed()) {
- digitalWrite(led_LED_PIN_D4, HIGH);
- saveStateToEEPROM(HIGH);
- }
- // Check if button2 is pressed and save the state to EEPROM
- if (button2.isPressed()) {
- digitalWrite(led_LED_PIN_D4, LOW);
- saveStateToEEPROM(LOW);
- }
- }
- void saveStateToEEPROM(uint8_t state)
- {
- eeprom_write_byte((uint8_t*)0, state);
- eeprom_busy_wait();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement