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 LED
- - Source Code compiled for: Arduino Uno
- - Source Code created on: 2024-04-06 11:55:07
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Brighten or dim LED to full when myButton1 or */
- /* myButton2 is pressed and kept pressed. Turn LED1 */
- /* OFF or ON, opposite to existing state when either */
- /* button is pressed momentarily */
- /****** END SYSTEM REQUIREMENTS *****/
- /****** DEFINITION OF LIBRARIES *****/
- #include <EasyButton.h> //https://github.com/evert-arias/EasyButton
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void updateOutputs(void);
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t myButton1_PushButton_PIN_D2 = 2;
- const uint8_t myButton2_PushButton_PIN_D3 = 3;
- /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
- const uint8_t LED1_LED_PIN_D4 = 4;
- /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
- bool LED1_LED_PIN_D4_rawData = 0;
- /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
- float LED1_LED_PIN_D4_phyData = 0.0;
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
- EasyButton myButton1(myButton1_PushButton_PIN_D2); // Create an instance of EasyButton for myButton1
- EasyButton myButton2(myButton2_PushButton_PIN_D3); // Create an instance of EasyButton for myButton2
- void setup(void)
- {
- // put your setup code here, to run once:
- pinMode(myButton1_PushButton_PIN_D2, INPUT_PULLUP);
- pinMode(myButton2_PushButton_PIN_D3, INPUT_PULLUP);
- pinMode(LED1_LED_PIN_D4, OUTPUT);
- myButton1.onPressedFor(1000, []() {
- // Callback function for button 1 press and hold for 1 second
- Serial.println("Button 1 pressed and held for 1 second");
- // Set the state of LED1 to full brightness
- LED1_LED_PIN_D4_rawData = 1;
- });
- myButton2.onPressedFor(1000, []() {
- // Callback function for button 2 press and hold for 1 second
- Serial.println("Button 2 pressed and held for 1 second");
- // Set the state of LED1 to full brightness
- LED1_LED_PIN_D4_rawData = 1;
- });
- myButton1.onPressed([]() {
- // Callback function for button 1 press and release
- Serial.println("Button 1 pressed and released");
- // Toggle the state of LED1
- LED1_LED_PIN_D4_rawData = !LED1_LED_PIN_D4_rawData;
- });
- myButton2.onPressed([]() {
- // Callback function for button 2 press and release
- Serial.println("Button 2 pressed and released");
- // Toggle the state of LED1
- LED1_LED_PIN_D4_rawData = !LED1_LED_PIN_D4_rawData;
- });
- myButton1.begin();
- myButton2.begin();
- }
- void loop(void)
- {
- // put your main code here, to run repeatedly:
- myButton1.read(); // Read the state of myButton1
- myButton2.read(); // Read the state of myButton2
- updateOutputs(); // Refresh output data
- }
- void updateOutputs()
- {
- digitalWrite(LED1_LED_PIN_D4, LED1_LED_PIN_D4_rawData);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement