Advertisement
Guest User

Untitled

a guest
Feb 8th, 2025
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | Source Code | 0 0
  1. #include <FastLED.h>
  2.  
  3. #define NUM_LEDS 600
  4. #define DATA_PIN 1
  5. #define BAUD_RATE 576000 // Set your desired baud rate
  6.  
  7. CRGB leds[NUM_LEDS];
  8.  
  9. // Define the sync frame (choose a unique sequence)
  10. const uint8_t SYNC_FRAME[] = {255, 0, 0, 255};
  11. const size_t SYNC_FRAME_SIZE = sizeof(SYNC_FRAME);
  12. #define SERIAL_RX_BUFFER_SIZE 1024
  13.  
  14. TaskHandle_t FastLEDTask; // Task handle for LED updates
  15.  
  16. void ledLoop(void *parameter) {
  17. while (1) {
  18. FastLED.show(); // Update LEDs on Core 0
  19. }
  20. }
  21.  
  22. void setup() {
  23. Serial.begin(BAUD_RATE);
  24. delay(500); // Allow time for initialization
  25.  
  26. FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS);
  27. FastLED.clear();
  28. FastLED.show();
  29.  
  30. Serial.println("Serial connection initialized. Ready to receive data.");
  31.  
  32. // Create LED task on Core 0
  33. xTaskCreatePinnedToCore(ledLoop, "FastLEDTask", 1000, NULL, 3, &FastLEDTask, 0);
  34. }
  35.  
  36. void loop() {
  37. static size_t receivedBytes = 0; // Bytes received so far
  38. static size_t syncIndex = 0; // Tracks progress in detecting the sync frame
  39.  
  40. while (Serial.available() > 0) {
  41. uint8_t byte = Serial.read();
  42.  
  43. if (syncIndex < SYNC_FRAME_SIZE) {
  44. if (byte == SYNC_FRAME[syncIndex]) {
  45. syncIndex++;
  46. if (syncIndex == SYNC_FRAME_SIZE) {
  47. Serial.println("Sync frame detected. Ready for LED data.");
  48. receivedBytes = 0;
  49. }
  50. } else {
  51. syncIndex = 0;
  52. }
  53. continue;
  54. }
  55.  
  56. if (syncIndex == SYNC_FRAME_SIZE) {
  57. if (receivedBytes % 3 == 0) leds[receivedBytes / 3].r = byte;
  58. if (receivedBytes % 3 == 1) leds[receivedBytes / 3].g = byte;
  59. if (receivedBytes % 3 == 2) leds[receivedBytes / 3].b = byte;
  60.  
  61. receivedBytes++;
  62.  
  63. if (receivedBytes >= NUM_LEDS * 3) {
  64. Serial.println("LED data received.");
  65. receivedBytes = 0;
  66. syncIndex = 0;
  67. }
  68. }
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement