Advertisement
pleasedontcode

Button Sequence rev_01

Mar 2nd, 2024
63
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 Sequence
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2024-03-02 15:51:19
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* I have regular push buttons on pins 2 to 10 and a */
  21.     /* light on 11 of my arduino uno. Create a code so */
  22.     /* that you can use the buttons as a password, so the */
  23.     /* password is pin 2, 3, 4, and 10. If you press them */
  24.     /* one after the other, the light will come on. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <EasyButton.h> //https://github.com/evert-arias/EasyButton
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33.  
  34. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  35. const uint8_t button_PushButton_PIN_D2 = 2;
  36. const uint8_t button_PushButton_PIN_D3 = 3;
  37. const uint8_t button_PushButton_PIN_D4 = 4;
  38. const uint8_t button_PushButton_PIN_D10 = 10;
  39. const uint8_t light_PIN_D11 = 11;
  40.  
  41. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  42. EasyButton button_D2(button_PushButton_PIN_D2);
  43. EasyButton button_D3(button_PushButton_PIN_D3);
  44. EasyButton button_D4(button_PushButton_PIN_D4);
  45. EasyButton button_D10(button_PushButton_PIN_D10);
  46.  
  47. bool isPasswordCorrect = false;
  48. bool button_D2_Pressed = false;
  49. bool button_D3_Pressed = false;
  50. bool button_D4_Pressed = false;
  51. bool button_D10_Pressed = false;
  52.  
  53. void setup(void)
  54. {
  55.     // put your setup code here, to run once:
  56.     pinMode(button_PushButton_PIN_D2, INPUT_PULLUP);
  57.     pinMode(button_PushButton_PIN_D3, INPUT_PULLUP);
  58.     pinMode(button_PushButton_PIN_D4, INPUT_PULLUP);
  59.     pinMode(button_PushButton_PIN_D10, INPUT_PULLUP);
  60.     pinMode(light_PIN_D11, OUTPUT);
  61.  
  62.     // Initialize the buttons
  63.     button_D2.begin();
  64.     button_D3.begin();
  65.     button_D4.begin();
  66.     button_D10.begin();
  67. }
  68.  
  69. void loop(void)
  70. {
  71.     // put your main code here, to run repeatedly:
  72.  
  73.     // Read the state of the buttons
  74.     button_D2.read();
  75.     button_D3.read();
  76.     button_D4.read();
  77.     button_D10.read();
  78.  
  79.     // Check if button D2 is pressed
  80.     if (button_D2.isPressed())
  81.     {
  82.         button_D2_Pressed = true;
  83.     }
  84.  
  85.     // Check the sequence of button presses
  86.     if (button_D2_Pressed)
  87.     {
  88.         if (button_D3.isPressed())
  89.         {
  90.             button_D3_Pressed = true;
  91.         }
  92.  
  93.         if (button_D3_Pressed && button_D4.isPressed())
  94.         {
  95.             button_D4_Pressed = true;
  96.         }
  97.  
  98.         if (button_D4_Pressed && button_D10.isPressed())
  99.         {
  100.             button_D10_Pressed = true;
  101.         }
  102.     }
  103.  
  104.     // Check if the password is entered correctly
  105.     if (button_D10_Pressed)
  106.     {
  107.         isPasswordCorrect = true;
  108.     }
  109.  
  110.     // Turn on the light if the password is correct
  111.     if (isPasswordCorrect)
  112.     {
  113.         digitalWrite(light_PIN_D11, HIGH);
  114.     }
  115. }
  116.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement