Advertisement
pleasedontcode

Button Control rev_02

Jan 22nd, 2024
69
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 Nano
  14.     - Source Code created on: 2024-01-22 13:37:09
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* вмикає рандомно один з двух світлодіодів при */
  21.     /* натисканні кнопки */
  22. /****** SYSTEM REQUIREMENT 2 *****/
  23.     /* вмикає рандомно один з п'яти світлодіодів при */
  24.     /* натисканні кнопки */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <Arduino.h>
  29. #include <EasyButton.h>
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34. void toggleRandomLED(void);
  35. void toggleRandomMultiLED(void);
  36.  
  37. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  38. const uint8_t knopka_PushButton_PIN_D2 = 2;
  39.  
  40. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  41. const uint8_t svitlo_LED_PIN_D3 = 3;
  42. const uint8_t svitlo_LED_PIN_D4 = 4;
  43. const uint8_t svitlo_LED_PIN_D5 = 5;
  44. const uint8_t svitlo_LED_PIN_D6 = 6;
  45. const uint8_t svitlo_LED_PIN_D7 = 7;
  46.  
  47. /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
  48. EasyButton knopka_PushButton(knopka_PushButton_PIN_D2); // Create an instance of EasyButton for the push button
  49.  
  50. void setup(void)
  51. {
  52.     // put your setup code here, to run once:
  53.     pinMode(knopka_PushButton_PIN_D2, INPUT_PULLUP);
  54.     pinMode(svitlo_LED_PIN_D3, OUTPUT);
  55.     pinMode(svitlo_LED_PIN_D4, OUTPUT);
  56.     pinMode(svitlo_LED_PIN_D5, OUTPUT);
  57.     pinMode(svitlo_LED_PIN_D6, OUTPUT);
  58.     pinMode(svitlo_LED_PIN_D7, OUTPUT);
  59.  
  60.     knopka_PushButton.begin(); // Initialize the EasyButton instance
  61. }
  62.  
  63. void loop(void)
  64. {
  65.     // put your main code here, to run repeatedly:
  66.     knopka_PushButton.read(); // Read the state of the push button
  67.  
  68.     // System Requirement 1: Toggle a random LED when the button is pressed
  69.     if (knopka_PushButton.isPressed()) {
  70.         toggleRandomLED();
  71.     } else {
  72.         digitalWrite(svitlo_LED_PIN_D3, LOW);
  73.     }
  74.  
  75.     // System Requirement 2: Toggle a random multi-LED pattern when the button is pressed
  76.     if (knopka_PushButton.isPressed()) {
  77.         toggleRandomMultiLED();
  78.     } else {
  79.         digitalWrite(svitlo_LED_PIN_D4, LOW);
  80.         digitalWrite(svitlo_LED_PIN_D5, LOW);
  81.         digitalWrite(svitlo_LED_PIN_D6, LOW);
  82.         digitalWrite(svitlo_LED_PIN_D7, LOW);
  83.     }
  84. }
  85.  
  86. void toggleRandomLED(void)
  87. {
  88.     // Generate a random number between 3 and 7
  89.     uint8_t randomLED = random(3, 8);
  90.  
  91.     // Turn off all LEDs
  92.     digitalWrite(svitlo_LED_PIN_D3, LOW);
  93.     digitalWrite(svitlo_LED_PIN_D4, LOW);
  94.     digitalWrite(svitlo_LED_PIN_D5, LOW);
  95.     digitalWrite(svitlo_LED_PIN_D6, LOW);
  96.     digitalWrite(svitlo_LED_PIN_D7, LOW);
  97.  
  98.     // Toggle the random LED
  99.     if (randomLED == svitlo_LED_PIN_D3) {
  100.         digitalWrite(svitlo_LED_PIN_D3, HIGH);
  101.     } else if (randomLED == svitlo_LED_PIN_D4) {
  102.         digitalWrite(svitlo_LED_PIN_D4, HIGH);
  103.     } else if (randomLED == svitlo_LED_PIN_D5) {
  104.         digitalWrite(svitlo_LED_PIN_D5, HIGH);
  105.     } else if (randomLED == svitlo_LED_PIN_D6) {
  106.         digitalWrite(svitlo_LED_PIN_D6, HIGH);
  107.     } else if (randomLED == svitlo_LED_PIN_D7) {
  108.         digitalWrite(svitlo_LED_PIN_D7, HIGH);
  109.     }
  110. }
  111.  
  112. void toggleRandomMultiLED(void)
  113. {
  114.     // Generate a random number between 4 and 7
  115.     uint8_t randomMultiLED = random(4, 8);
  116.  
  117.     // Turn off all LEDs
  118.     digitalWrite(svitlo_LED_PIN_D3, LOW);
  119.     digitalWrite(svitlo_LED_PIN_D4, LOW);
  120.     digitalWrite(svitlo_LED_PIN_D5, LOW);
  121.     digitalWrite(svitlo_LED_PIN_D6, LOW);
  122.     digitalWrite(svitlo_LED_PIN_D7, LOW);
  123.  
  124.     // Toggle the random multi-LED pattern
  125.     if (randomMultiLED == 4) {
  126.         digitalWrite(svitlo_LED_PIN_D4, HIGH);
  127.     } else if (randomMultiLED == 5) {
  128.         digitalWrite(svitlo_LED_PIN_D5, HIGH);
  129.     } else if (randomMultiLED == 6) {
  130.         digitalWrite(svitlo_LED_PIN_D6, HIGH);
  131.     } else if (randomMultiLED == 7) {
  132.         digitalWrite(svitlo_LED_PIN_D7, HIGH);
  133.     }
  134. }
  135.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement