Advertisement
pleasedontcode

Button Control rev_01

Apr 18th, 2024
70
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: 2024-04-18 12:15:50
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The system control an RGB LED and a piezo using */
  21.     /* Arduino UNO.A short press on the push button (less */
  22.     /* than 300ms) shall change the RGB status in a */
  23.     /* sequence of: dimmed, red, green, blue. A long */
  24.     /* press and release (more than 500ms) shall trigger */
  25.     /* the piezo. */
  26. /****** END SYSTEM REQUIREMENTS *****/
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <EasyButton.h>
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34. void onPressedForDuration();
  35.  
  36. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  37. const uint8_t myButton_PushButton_PIN_D2 = 2;
  38.  
  39. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  40. const uint8_t myRgb_LEDRGB_Red_PIN_D4 = 4;
  41. const uint8_t myRgb_LEDRGB_Green_PIN_D5 = 5;
  42. const uint8_t myRgb_LEDRGB_Blue_PIN_D6 = 6;
  43.  
  44. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  45. /***** used to store raw data *****/
  46. bool myRgb_LEDRGB_Red_PIN_D4_rawData = 0;
  47. bool myRgb_LEDRGB_Green_PIN_D5_rawData = 0;
  48. bool myRgb_LEDRGB_Blue_PIN_D6_rawData = 0;
  49.  
  50. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  51. /***** used to store data after characteristic curve transformation *****/
  52. float myRgb_LEDRGB_Red_PIN_D4_phyData = 0.0;
  53. float myRgb_LEDRGB_Green_PIN_D5_phyData = 0.0;
  54. float myRgb_LEDRGB_Blue_PIN_D6_phyData = 0.0;
  55.  
  56. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  57. EasyButton button(myButton_PushButton_PIN_D2);
  58.  
  59. void setup(void)
  60. {
  61.   // put your setup code here, to run once:
  62.  
  63.   pinMode(myButton_PushButton_PIN_D2, INPUT_PULLUP);
  64.  
  65.   pinMode(myRgb_LEDRGB_Red_PIN_D4, OUTPUT);
  66.   pinMode(myRgb_LEDRGB_Green_PIN_D5, OUTPUT);
  67.   pinMode(myRgb_LEDRGB_Blue_PIN_D6, OUTPUT);
  68.  
  69.   button.begin();
  70.   button.onPressedFor(300, onPressedForDuration); // Set the duration to 300ms for a short press
  71.   button.onPressedFor(500, onPressedForDuration); // Set the duration to 500ms for a long press
  72. }
  73.  
  74. void loop(void)
  75. {
  76.   // put your main code here, to run repeatedly:
  77.  
  78.   button.read();
  79. }
  80.  
  81. void onPressedForDuration()
  82. {
  83.   if (button.wasPressed()) {
  84.     // Code to be executed when the button is pressed
  85.     if (button.pressedFor(300)) {
  86.       // Code to be executed for a short press (less than 300ms)
  87.       // Change the RGB status in a sequence of: dimmed, red, green, blue
  88.     }
  89.     else if (button.pressedFor(500)) {
  90.       // Code to be executed for a long press (more than 500ms)
  91.       // Trigger the piezo
  92.     }
  93.   }
  94. }
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement