Advertisement
pleasedontcode

LED Animation rev_03

May 1st, 2024
94
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 Animation
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2024-05-01 09:21:26
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The LED chipset is WS2812B RGB  Number of LED is */
  21.     /* 20  Digital output is D5  write a code:  led fade */
  22.     /* in and out randomly, increments 5.  set fade in */
  23.     /* value at 0,  set fade out value at 255  set fade */
  24.     /* time from 0 to 255 at 5 seconds  use only warm */
  25.     /* colors */
  26. /****** END SYSTEM REQUIREMENTS *****/
  27.  
  28.  
  29. /********* User code review feedback **********
  30. #### Feedback 1 ####
  31. - The colors for each led are random, use warm colors only
  32. ********* User code review feedback **********/
  33.  
  34. /****** DEFINITION OF LIBRARIES *****/
  35. #include <FastLED.h>    //https://github.com/FastLED/FastLED
  36.  
  37. /****** FUNCTION PROTOTYPES *****/
  38. void setup(void);
  39. void loop(void);
  40. void updateOutputs(void);
  41. void ledFadeInOut(void);
  42.  
  43. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  44. const uint8_t WS2812B_LEDRGB_PIN_D5 = 5; // Digital output pin D5 for WS2812B LED
  45.  
  46. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  47. CRGB leds[20]; // Define LED array with 20 LEDs
  48.  
  49. void setup(void)
  50. {
  51.     // put your setup code here, to run once:
  52.     FastLED.addLeds<WS2812B, WS2812B_LEDRGB_PIN_D5, GRB>(leds, 20); // Initialize FastLED object with 20 LEDs on pin D5
  53.  
  54.     pinMode(WS2812B_LEDRGB_PIN_D5, OUTPUT); // Set D5 pin as output
  55. }
  56.  
  57. void loop(void)
  58. {
  59.     // put your main code here, to run repeatedly:
  60.     updateOutputs(); // Refresh output data
  61.     ledFadeInOut(); // Perform LED fade in and out randomly
  62. }
  63.  
  64. void updateOutputs()
  65. {
  66.     // No need to update outputs in this case
  67. }
  68.  
  69. void ledFadeInOut()
  70. {
  71.     static uint8_t fadeValue = 0;
  72.     static bool fadeDirection = true;
  73.  
  74.     if (fadeDirection) {
  75.         fadeValue += 5;
  76.         if (fadeValue >= 255) {
  77.             fadeDirection = false;
  78.         }
  79.     } else {
  80.         fadeValue -= 5;
  81.         if (fadeValue <= 0) {
  82.             fadeDirection = true;
  83.         }
  84.     }
  85.  
  86.     // Set warm color based on fade value
  87.     CRGB color = CRGB(fadeValue, fadeValue * 0.7, fadeValue * 0.3);
  88.  
  89.     // Set LED color
  90.     for (int i = 0; i < 20; i++) {
  91.         leds[i] = color;
  92.     }
  93.  
  94.     // Show LED color
  95.     FastLED.show();
  96.  
  97.     // Delay for 5 seconds
  98.     delay(5000);
  99. }
  100.  
  101. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement