Advertisement
pleasedontcode

**LED Control** rev_01

Apr 6th, 2025
507
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 Control**
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2025-04-06 20:32:56
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* WRITE A CODE FOR ESP32 USING FASTLED CLOCKLESS IN */
  21.     /* ESPHOME TO CONTROL TWO LED STRIP FROM 2 INPUT */
  22.     /* BOOLEAN IN HOME ASSISTANT WHEN BUTTON 1 IS PRESSED */
  23.     /* THE STRIP ONE WILL HAVE EFFECT WIPE FROM LAST TO */
  24.     /* FIRST LED AND AFTER THAT STRIP 2 HAVE THE WIPE */
  25.     /* EFFECT */
  26. /****** END SYSTEM REQUIREMENTS *****/
  27.  
  28. /* START CODE */
  29.  
  30. /****** DEFINITION OF LIBRARIES *****/
  31. // Include necessary libraries for ESP32 and FastLED
  32. #include <FastLED.h>
  33.  
  34. /****** FUNCTION PROTOTYPES *****/
  35. void setup(void);
  36. void loop(void);
  37. void wipeEffect(CRGB *leds, int numLeds, CRGB color);
  38.  
  39. /****** GLOBAL VARIABLES *****/
  40. #define NUM_LEDS1 30  // Number of LEDs in strip 1
  41. #define NUM_LEDS2 30  // Number of LEDs in strip 2
  42. CRGB leds1[NUM_LEDS1]; // Array for strip 1
  43. CRGB leds2[NUM_LEDS2]; // Array for strip 2
  44.  
  45. // Define button pin
  46. const int buttonPin = 2; // Pin where the button is connected
  47. bool lastButtonState = LOW; // Previous state of the button
  48.  
  49. void setup(void)
  50. {
  51.     // put your setup code here, to run once:
  52.     // Initialize FastLED
  53.     FastLED.addLeds<WS2812B, 18, GRB>(leds1, NUM_LEDS1);
  54.     FastLED.addLeds<WS2812B, 19, GRB>(leds2, NUM_LEDS2);
  55.    
  56.     // Initialize button pin
  57.     pinMode(buttonPin, INPUT);
  58. }
  59.  
  60. void loop(void)
  61. {
  62.     // put your main code here, to run repeatedly:
  63.     bool currentButtonState = digitalRead(buttonPin);
  64.    
  65.     // Check if button is pressed
  66.     if (currentButtonState == HIGH && lastButtonState == LOW) {
  67.         // Wipe effect on strip 1
  68.         wipeEffect(leds1, NUM_LEDS1, CRGB::Red); // Change color as needed
  69.         FastLED.show();
  70.         delay(500); // Delay to see the effect
  71.        
  72.         // Wipe effect on strip 2
  73.         wipeEffect(leds2, NUM_LEDS2, CRGB::Blue); // Change color as needed
  74.         FastLED.show();
  75.     }
  76.    
  77.     lastButtonState = currentButtonState; // Update button state
  78. }
  79.  
  80. void wipeEffect(CRGB *leds, int numLeds, CRGB color) {
  81.     // Wipe effect from last to first LED
  82.     for (int i = numLeds - 1; i >= 0; i--) {
  83.         leds[i] = color; // Set LED color
  84.         FastLED.show();
  85.         delay(50); // Delay for effect
  86.         leds[i] = CRGB::Black; // Turn off LED after wipe
  87.     }
  88. }
  89.  
  90. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement