Advertisement
pleasedontcode

"FastLED Control" rev_01

Jul 3rd, 2024
765
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: "FastLED Control"
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-07-03 20:21:27
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* "Convert the glediator code for ESP32 so that I */
  21.     /* can control my LED strip with the RemoteLight app */
  22.     /* via USB connection on a PC." */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <FastLED.h>  //https://github.com/FastLED/FastLED
  27.  
  28. /****** FUNCTION PROTOTYPES *****/
  29. void setup(void);
  30. void loop(void);
  31. void updateOutputs(void);
  32. void readSerialData(void);
  33.  
  34. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  35. const uint8_t Ledstrip_WS2812_DIN_PIN_D4 = 4;
  36.  
  37. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  38. /***** used to store raw data *****/
  39. bool Ledstrip_WS2812_DIN_PIN_D4_rawData = 0;
  40.  
  41. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  42. /***** used to store data after characteristic curve transformation *****/
  43. float Ledstrip_WS2812_DIN_PIN_D4_phyData = 0.0;
  44.  
  45. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  46. #define NUM_LEDS 60
  47. #define DATA_PIN 4
  48. CRGB leds[NUM_LEDS];
  49.  
  50. void setup(void) {
  51.   // Initialize serial communication at 115200 baud rate
  52.   Serial.begin(115200);
  53.  
  54.   // Initialize the FastLED library
  55.   FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS);
  56.   FastLED.setBrightness(50); // Set brightness to 50 out of 255
  57. }
  58.  
  59. void loop(void) {
  60.   // Read data from serial
  61.   readSerialData();
  62.  
  63.   // Refresh output data
  64.   updateOutputs();
  65.  
  66.   // Show the LED data
  67.   FastLED.show();
  68. }
  69.  
  70. void updateOutputs() {
  71.   digitalWrite(Ledstrip_WS2812_DIN_PIN_D4, Ledstrip_WS2812_DIN_PIN_D4_rawData);
  72. }
  73.  
  74. void readSerialData() {
  75.   static uint16_t dataIndex = 0;
  76.   static uint8_t buffer[NUM_LEDS * 3]; // Buffer to store incoming data
  77.  
  78.   while (Serial.available()) {
  79.     buffer[dataIndex++] = Serial.read();
  80.  
  81.     // If buffer is full, update LED strip
  82.     if (dataIndex >= sizeof(buffer)) {
  83.       for (uint16_t i = 0; i < NUM_LEDS; i++) {
  84.         leds[i].r = buffer[i * 3];
  85.         leds[i].g = buffer[i * 3 + 1];
  86.         leds[i].b = buffer[i * 3 + 2];
  87.       }
  88.       dataIndex = 0; // Reset index for next frame
  89.     }
  90.   }
  91. }
  92.  
  93. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement