inaccurate serial communication

I have LED strip of 300 pixels, and I'm sending data (only commands, not pixel-by-pixel) through the serial to an Uno, however when I try to use more pixels (requiring more memory), I get inaccuracies in the serial communication. If I lower the number of LEDs, I get more and more stable communication. So it is perfectly stable when the compiler says "leaving 1634 bytes for local variables" (20 LEDs), but when using 80 LEDs the free memory is at 1454 (so 70% of all memory) it gets unstable, meaning that the commands mostly get through, however most of the time with wrong arguments, and sometimes it gets it right. Higher LED counts lower the accuracy.

Do you have any idea why is this happening? Am I using maybe serial incorrectly? I'm using the serial command library (GitHub - scogswell/ArduinoSerialCommand: An Arduino library to tokenize and parse commands received over a serial port.), and the FastLED library.

#include <FastLED.h>
#include <SerialCommand.h>

#define LED_PIN     6
#define BRIGHTNESS  255
#define LED_TYPE    WS2812B
#define COLOR_ORDER GRB
#define NUM_LEDS    150

CRGB leds[NUM_LEDS];

SerialCommand sCmd;

uint16_t noiseParam[8];

void setup() {
//  delay(3000);
  LEDS.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
  LEDS.setBrightness(BRIGHTNESS);
  FastLED.setCorrection( CRGB( 255, 115, 70) );
  FastLED.setDither( 0 );

  Serial.begin(9600);
  while (!Serial) ;

  sCmd.addCommand("BRI", cBRI);             

  sCmd.addCommand("FSH", cFSH);             //fill solid hsv
  sCmd.addCommand("FGH", cFGH);             //fill gradient hsv

  sCmd.setDefaultHandler(unrecognized);

}

void loop() {
  sCmd.readSerial();
  
  FastLED.show();
  FastLED.delay(100);

}

void cFSH() {
  int argInt[3];
  char *arg;

  for (byte i = 0; i < 3; i++) {
    arg = sCmd.next();
    if (arg != NULL) {
      argInt[i] = atoi(arg);    // Converts a char string to an integer
    }
  }
  
    fill_solid(leds, NUM_LEDS, CHSV(argInt[0], argInt[1], argInt[2]));
    Serial.print(F("fill solid hue "));
    Serial.print(argInt[0]); Serial.print(F(" "));
    Serial.print(argInt[1]); Serial.print(F(" "));
    Serial.println(argInt[2]);
  
}


void cFGH() {
  int argInt[2];
  char *arg;


  for (byte i = 0; i < 2; i++) {
    arg = sCmd.next();
    if (arg != NULL) {
      argInt[i] = atoi(arg);    // Converts a char string to an integer
    }
  }

  fill_gradient (leds, 0, CHSV(argInt[0], 255, 255), NUM_LEDS - 1, CHSV(argInt[1], 255, 255), SHORTEST_HUES);
  Serial.print(F("fill gradient hue "));
  Serial.print(argInt[0]);
  Serial.print(F(" --> "));
  Serial.println(argInt[1]);
}

void cBRI() {
  int argInt;
  char *arg;

  arg = sCmd.next();
  if (arg != NULL) {
    argInt = atoi(arg);    // Converts a char string to an integer
  }

  LEDS.setBrightness(argInt);

  Serial.print(F("Brightness: "));
  Serial.println(argInt);
}


void unrecognized(const char *command) {
  Serial.println(F("?"));
}

Because FastLED mallocs memory?
(Just a guess, but you have the source)

What do you mean by mallocs?

Some of the examples work perfectly with 300 leds, so I don't think it's FastLED's fault.

drcak:
What do you mean by mallocs?

Short for memory allocation. M and alloc ----- malloc.

Could be you're sending commands too frequently, and screwing the timing for the LED command stream

the data to the leds go through pin 6, and the Uno communicates with the computer through USB.

I'm typing the commands, I don't think it's too fast (and also at lower led count, I sent them from processing pretty frequently, without problems)

But longer LED strings will take longer to update.

but the first hand typed command does not get through correctly

Could probably also think about hardware --- or hardware requirements.... maybe. Like, how are you powering your LEDs? Is the power supply adequate?

And....for testing purposes only.... what happens if you put a line like 'delay(1000)' in your loop? For testing only.

Also....what do you mean by 'lowers the accuracy'? Eg..... the accuracy of what? Do you mean.... you type in a command, and the arduino doesn't receive the command strings accurately? So, even when the LED strips are not connected, the serially-transmitted commands from computer to arduino are received (by the arduino) with errors? And these erroneous strings etc are seen (observed) in Serial Monitor?

thanks, there was a delay in the main loop, however it was the fastled delay, which keeps refreshing the leds "while paused". I changed that to a regular delay, and it works fine now. Thanks for your tips.

By accuracy I meant that I had higher success ratio of sending commands successfully. I changed the "unrecognised" command to echo back the command it received, and there were missing characters randomly.

about the power supply: I'm using this one:

however it shifts towards red towards the end of the strip (and also vibrates constantly a bit). I can just plug in power at the other end, it would work fine, right?

AWOL:
Could be you're sending commands too frequently, and screwing the timing for the LED command stream

I guess this is the case indeed. Now it is working almost fine, however, when sending too much in short time (I should solve this in Processing also), Arduino receives incorrect data.

Could you help me understand how the timing works here? I thought that the following code will first read the data from serial (which calls a function which changes the leds variable), then send to the leds with the show() function. But even if multiple commands are in the serial buffer, first it should use that data, and only after the buffer is empty, it can move on to the next line of code and start sending data to the leds. Or are these happening in parallel? Also one is going on pin 6, the other is on serial, I would think they are independent, but can these two interfere with each other?

void loop() {
 sCmd.readSerial();
 delay(100);
 FastLED.show();
 delay(100);
}

Thanks!