Works for mega but laggy

This code works great for the Arduino mega 2560.

// header in vixen must be >>####<<  #### will be your total number of leds , 1224 for these 2 matrix, >>1224<<
// Many thanks to all who helped at doityourselfchristmas.com
// You must download and install the library from http://fastled.io/
#include <FastLED.h>

#define matrix_a 968                    // 22*44  Pin 8
#define matrix_b 968                    // 8*32   Pin 10
#define NUM_LEDS matrix_a + matrix_b    // 968+256=1224  or 968*2=1936

CRGB leds[matrix_a + matrix_b];         // 1224 or 1936


void setup() {
  // Define the speed of the serial port
  Serial.begin(1048576);   //115200 worked now I'm checking (slow 230400),(ok 921600),1048576
}

void loop() {
  // Set some counter / temporary storage variables
  int cnt;
  unsigned int num_leds;
  unsigned int d1, d2, d3, d4;

  // Begin an endless loop to receive and process serial data
  for (;;) {
    // Set a counter to 0. This couter keeps track of the pixel colors received.
    cnt = 0;
    //Begin waiting for the header to be received on the serial bus
    //1st character
    while (!Serial.available());
    if (Serial.read() != '>') {
      continue;
    }
    //second character
    while (!Serial.available());
    if (Serial.read() != '>') {
      continue;
    }
    //get the first digit from the serial bus for the number of pixels to be used
    while (!Serial.available());
    d1 = Serial.read();
    //get the second digit from the serial bus for the number of pixels to be used
    while (!Serial.available());
    d2 = Serial.read();
    //get the third digit from the serial bus for the number of pixels to be used
    while (!Serial.available());
    d3 = Serial.read();
    //get the forth digit from the serial bus for the number of pixels to be used
    while (!Serial.available());
    d4 = Serial.read();
    //get the end of the header
    while (!Serial.available());
    if (Serial.read() != '<') {
      continue;
    }
    while (!Serial.available());
    if (Serial.read() != '<') {
      continue;
    }
    // calculate the number of pixels based on the characters provided in the header digits
    num_leds = (d1 - '0') * 1000 + (d2 - '0') * 100 + (d3 - '0') * 10 + (d4 - '0');
    // ensure the number of pixels does not exceed the number allowed
    if (num_leds > NUM_LEDS) {
      continue;
    }

    // CODE ADDED - tell FastLED there's 63 PIXEL leds on pin 8, starting at index 0 in the led array
    //    FastLED.addLeds<WS2811, 8, RGB>(leds, 0, NUM_LEDS_PER_STRIP);
    // CODE ADDED - tell FastLED there's 63 PIXEL leds on pin 9, starting at index 64 in the led array
    //    FastLED.addLeds<WS2811, 9, RGB>(leds, NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
    // CODE ADDED - tell FastLED there's 63 PIXEL leds on pin 10, starting at index 127 in the led array
    //    FastLED.addLeds<WS2811, 10, RGB>(leds, NUM_LEDS_PER_STRIP * 2, NUM_LEDS_PER_STRIP);
    // CODE ADDED - tell FastLED there's 63 PIXEL leds on pin 11, starting at index 190 in the led array
    //    FastLED.addLeds<WS2811, 11, RGB>(leds, NUM_LEDS_PER_STRIP * 3, NUM_LEDS_PER_STRIP);
    
    // CODE ADDED - tell FastLED there's 968 PIXEL leds on pin 8, starting at index 0 in the led array 
    FastLED.addLeds<WS2811, 8, GRB>(leds, 0, matrix_a);
    // CODE ADDED - tell FastLED there's 256 PIXEL leds on pin 10, starting at index 968 in the led array 
    FastLED.addLeds<WS2811, 10, GRB>(leds, matrix_a, matrix_b);


    do {
      while (!Serial.available());
      leds[cnt].r = Serial.read();
      while (!Serial.available());
      leds[cnt].g = Serial.read();
      while (!Serial.available());
      leds[cnt++].b = Serial.read();
    }
    while (--num_leds);   // Tell the FastLED Library it is time to update the strip of pixels
    FastLED.show();       // WOO HOO... We are all done and are ready to start over again!
  }
}

This is so that Vixen Lights (very need lighting program, same concept as xlights just different interface.) can control the pixel grids that I have made.

This works great on the arduino as stated. however, since the mega 2560 is somewhat limited, it is a bit laggy, does not do all the changes at the correct time. I have read that the ESP32 has enough computing power to run these small grids without lagging so I wanted to try it. However, cannot seem to get the ESP32-wroom to realize that it should be doing something with the signals that it is receiving. Since I read that the language is the same I thought it would work however, this code does not do anything with the ESP32 even if i change the pins. I realize that I do not really have the time to completely figure this out is the is what would be called a hobby as only have a couple of hours a week to figure it out. So if someone could point me in the right direction to get it to work, like mainly the differences between the two and what I am missing. There is obviously something needed to have it realize that it needs to read the information being sent to it and then send that information to the pixel so the pixel knows what and when it is supposed to do something. this code is compilation of years of searching the internet to find one that works. Many thanks to the original creators not sure if it still mentions them, and I do not recall now who they all where. Until I find them again think one of them was Brian L .

Sorry for the ramble just trying to get all the information here.

Again thanks for any help in advance.

Hi Brian,

Quick look at the code..
Seems like you are trying to enforce one command per loop and using quite allot of blocking whiles to accomplish this..
And you have another forever loop inside your main loop, not so much an issue for the mega but esp32's watchdog won't be fed and it will reset..

You really need to loose all the while loops and that forever for loop..
Replace it with a state machine to signal when a new command is received..

This will have to be done for ESP which has RTOS on top of things, on the Mega you'll just get some lag..

But once you tiding things up the mega will fly too..

Oh, that baud rate, did you confirm that it works also with ESP, looks funny too me..

good luck.. ~q

The FastLED.addLED() lines may cause problems being executed repeatedly in loop(), those would normally be in setup().

What do you mean by laggy? With 1936 LEDs, it will take about 18.46mS to transfer the data for the LEDs, not counting the header. It will then take 58mS to update the LEDs, during which time interrupts are disabled, so no serial data can be received (at least not on a Mega, not familiar with the ESP32). At that point, if you are constantly sending serial data, it could take another 18.5mS to sync up with the header of the serial stream.

Using one of the Teensy boards that uses DMA for the LEDs, and has plenty of ram, should allow for processing the serial data while the LEDs are being updated.

here's a quick stab at making the code non-blocking..
not fully tested but should be functionally the same as your original sketch..
ended up with 2 state machines, main receiver and color receiver..
each iteration of the loop will process a char through the state machines..
bear in mind like already stated updating that many leds will take some time..

should be easier to port to esp and sorry no, not sure how good/bad esp32 handles fastled..

// header in vixen must be >>####<<  #### will be your total number of leds , 1224 for these 2 matrix, >>1224<<
// Many thanks to all who helped at doityourselfchristmas.com
// You must download and install the library from http://fastled.io/
#include <FastLED.h>

#define matrix_a 968                    // 22*44  Pin 8
#define matrix_b 968                    // 8*32   Pin 10
#define NUM_LEDS matrix_a + matrix_b    // 968+256=1224  or 968*2=1936

CRGB leds[matrix_a + matrix_b];         // 1224 or 1936

int cnt;
unsigned long num_leds;
char buf[10];
byte recvState = 0;
byte color = 0;



void setup() {
  // Define the speed of the serial port
  Serial.begin(1048576);   //115200 worked now I'm checking (slow 230400),(ok 921600),1048576
  // CODE ADDED - tell FastLED there's 968 PIXEL leds on pin 8, starting at index 0 in the led array
  FastLED.addLeds<WS2811, 8, GRB>(leds, 0, matrix_a);
  // CODE ADDED - tell FastLED there's 256 PIXEL leds on pin 10, starting at index 968 in the led array
  FastLED.addLeds<WS2811, 10, GRB>(leds, matrix_a, matrix_b);

}


void loop() {

  char *end = nullptr;

  if (Serial.available()) {
    char achar = Serial.read();
    switch (recvState) {
      case 0: if (achar == '>') {
          recvState++;
        }
        break;
      case 1: if (achar == '>') {
          recvState++;
          cnt = 0;
        } else {
          recvState--;
        } break;
      case 2: if (isdigit(achar))
        { buf[cnt] = achar;
          cnt++;
          if (cnt == 4) {
            recvState++;
          }
        } else {
          recvState = 0;
          memset(buf, 0, sizeof(buf));
        }
        break;
      case 3: if (achar == '<') {
          recvState++;
        } else {
          recvState = 0;
          memset(buf, 0, sizeof(buf));
        } break;
      case 4: if (achar == '<') {
          recvState++;
        } else {
          recvState = 0;
          memset(buf, 0, sizeof(buf));
        } break;
      case 5: num_leds = strtoul(buf, &end, 10);
        if (num_leds > NUM_LEDS) {
          recvState = 0;
          memset(buf, 0, sizeof(buf));
        } else
        {
          recvState++;
          cnt = 0;
        }
        break;
      case 6: {
          switch (color) {
            case 0: leds[cnt].r = achar; color++; break;
            case 1: leds[cnt].g = achar; color++; break;
            case 2: leds[cnt].b = achar; color = 0; cnt++; break;
          }
          if (cnt == num_leds) {
            FastLED.show();
            memset(buf, 0, sizeof(buf));
            cnt = 0;
            recvState = 0;
          }
          break;
        }
    }
  }
}

good luck.. ~q

The code did display on the mega, which does streamline it thank you, however when I have a bit more time to look at it will have to figure out why the colors are now off. computer going slightly weird this morning.

hove not got it to connect to the ESP32 this morning to see if that would work.

1 Like

Thank you qubits-us for streamlining it. will let you know if the ESP 32 will like it.

1 Like

with mega it is accually off. still not getting my esp32 to connect at all not even to upload, might have to try using NEOPIXEL. and possibly getting the teensy which I have no clear way figure that out yet will have to research.

1 Like

Another thought as I was looking for the Teensy. I think I might have a raspberry pico here, think that might work? Or is that a totally different type of language that would take months to even start understanding.

Did not realize that had gotten copied into the loop. Have moved thank you.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.