Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <FastLED.h>
- #define NUM_LEDS 600
- #define DATA_PIN 1
- #define BAUD_RATE 576000 // Set your desired baud rate
- CRGB leds[NUM_LEDS];
- // Define the sync frame (choose a unique sequence)
- const uint8_t SYNC_FRAME[] = {255, 0, 0, 255};
- const size_t SYNC_FRAME_SIZE = sizeof(SYNC_FRAME);
- #define SERIAL_RX_BUFFER_SIZE 1024
- TaskHandle_t FastLEDTask; // Task handle for LED updates
- void ledLoop(void *parameter) {
- while (1) {
- FastLED.show(); // Update LEDs on Core 0
- }
- }
- void setup() {
- Serial.begin(BAUD_RATE);
- delay(500); // Allow time for initialization
- FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS);
- FastLED.clear();
- FastLED.show();
- Serial.println("Serial connection initialized. Ready to receive data.");
- // Create LED task on Core 0
- xTaskCreatePinnedToCore(ledLoop, "FastLEDTask", 1000, NULL, 3, &FastLEDTask, 0);
- }
- void loop() {
- static size_t receivedBytes = 0; // Bytes received so far
- static size_t syncIndex = 0; // Tracks progress in detecting the sync frame
- while (Serial.available() > 0) {
- uint8_t byte = Serial.read();
- if (syncIndex < SYNC_FRAME_SIZE) {
- if (byte == SYNC_FRAME[syncIndex]) {
- syncIndex++;
- if (syncIndex == SYNC_FRAME_SIZE) {
- Serial.println("Sync frame detected. Ready for LED data.");
- receivedBytes = 0;
- }
- } else {
- syncIndex = 0;
- }
- continue;
- }
- if (syncIndex == SYNC_FRAME_SIZE) {
- if (receivedBytes % 3 == 0) leds[receivedBytes / 3].r = byte;
- if (receivedBytes % 3 == 1) leds[receivedBytes / 3].g = byte;
- if (receivedBytes % 3 == 2) leds[receivedBytes / 3].b = byte;
- receivedBytes++;
- if (receivedBytes >= NUM_LEDS * 3) {
- Serial.println("LED data received.");
- receivedBytes = 0;
- syncIndex = 0;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement