Advertisement
pleasedontcode

"LED Patterns" rev_01

Nov 21st, 2024
60
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: "LED Patterns"
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-11-21 11:28:47
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* If the button is not pressed, red LED should be */
  21.     /* blinking for an interval of 0.5s, if the button */
  22.     /* was pressed, both Red and Blue LED should light up */
  23.     /* alternately for an interval of 200ms. */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /* START CODE */
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <EasyButton.h> //https://github.com/evert-arias/EasyButton
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34.  
  35. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  36. const uint8_t Bhuton_PushButton_PIN_D2      = 2;
  37. const int redLEDPin = 11; // LED pin for red LED
  38. const int blueLEDPin = 13; // LED pin for blue LED
  39.  
  40. // Define the blinking intervals
  41. const int redLEDInterval = 500; // 0.5 seconds
  42. const int blueLEDInterval = 200; // 200 milliseconds
  43.  
  44. // Define the state variables
  45. bool buttonState = false;
  46. unsigned long previousMillis = 0;
  47.  
  48. void setup(void)
  49. {
  50.     // put your setup code here, to run once:
  51.     pinMode(Bhuton_PushButton_PIN_D2, INPUT_PULLUP);
  52.    
  53.     // Set the LED pins as outputs
  54.     pinMode(redLEDPin, OUTPUT);
  55.     pinMode(blueLEDPin, OUTPUT);
  56. }
  57.  
  58. void loop(void)
  59. {
  60.     // put your main code here, to run repeatedly:
  61.     // Read the button state
  62.     buttonState = digitalRead(Bhuton_PushButton_PIN_D2); // Updated to use Bhuton_PushButton_PIN_D2
  63.  
  64.     // Check if the button is pressed
  65.     if (buttonState == HIGH) {
  66.         // Blink the red and blue LEDs alternately
  67.         unsigned long currentMillis = millis();
  68.         if (currentMillis - previousMillis >= blueLEDInterval) {
  69.             previousMillis = currentMillis;
  70.  
  71.             // Toggle the state of the LEDs
  72.             digitalWrite(redLEDPin, !digitalRead(redLEDPin));
  73.             digitalWrite(blueLEDPin, !digitalRead(blueLEDPin));
  74.         }
  75.     } else {
  76.         // Blink the red LED only
  77.         unsigned long currentMillis = millis();
  78.         if (currentMillis - previousMillis >= redLEDInterval) {
  79.             previousMillis = currentMillis;
  80.  
  81.             // Toggle the state of the red LED
  82.             digitalWrite(redLEDPin, !digitalRead(redLEDPin));
  83.         }
  84.     }
  85. }
  86.  
  87. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement