FastLED and SD corrupts output

Hi,

I'm having trouble to get fastLED and SD.h to work togheter. When I run the code with the line FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, ANTALL_LED); it corrupts my output. If I comment out this line the code runs fine.

Correct output:
initialization done. Fil ok!

And the result with the line is a bunch of squares and questionmarks.

The entire code:

#include <SPI.h>
#include <SD.h>
#include <FastLED.h>

#define ANTALL_LED 300
#define LED_PIN 2
#define CS_PIN 10



File myFile;
CRGB leds[ANTALL_LED];

#define MAX_DATA_LENGTH 50

void setup() {
  delay(3000);
  Serial.begin(9600);
  while (!Serial);

  if (!SD.begin(CS_PIN)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");

  myFile = SD.open("Land.csv");

  //FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, ANTALL_LED); //This line makes things weird.
  FastLED.setBrightness(10);


  char inData[MAX_DATA_LENGTH];
  char ID[MAX_DATA_LENGTH];
  char Lokasjon[MAX_DATA_LENGTH];
  char Farge[MAX_DATA_LENGTH];

if(myFile){Serial.println("Fil ok!");}
else {Serial.println("Fil Problem!");}
  while (myFile.available()) {
    int index = 0;
    char c;
    
    while ((c = myFile.read()) != '\n' && index < MAX_DATA_LENGTH - 1) {
      if (c != '\r') {
        inData[index++] = c;
      }
    }
    
    inData[index] = '\0'; 
    
    char *token = strtok(inData, ";");
    int ID = atoi(token);
    token = strtok(NULL, ";");
    strncpy(Lokasjon, token, MAX_DATA_LENGTH);
    token = strtok(NULL, ";");
    strncpy(Farge, token, MAX_DATA_LENGTH);

    if (strcmp(Farge, "Hvit") == 0){leds[ID] = CRGB::FloralWhite;}
    if (strcmp(Farge, "AV") == 0){leds[ID] = CRGB::Black;}
    if (strcmp(Farge, "Rod") == 0){leds[ID] = CRGB::Red;}
    if (strcmp(Farge, "Gronn") == 0){leds[ID] = CRGB::Green;}
    if (strcmp(Farge, "Gul") == 0){leds[ID] = CRGB::Yellow;}
    if (strcmp(Farge, "Rosa") == 0){leds[ID] = CRGB::Pink;}
    if (strcmp(Farge, "Bla") == 0){leds[ID] = CRGB::Blue;}
    if (strcmp(Farge, "Lilla") == 0){leds[ID] = CRGB::Purple;}
    if (strcmp(Farge, "Oransje") == 0){leds[ID] = CRGB::Orange;}

  }

  myFile.close();
  FastLED.show();

}
void loop() {}

What "output"?

what the LEDs show?
Output to serial?
Output you write on SD-Card?

How does the "corruption" look like?

what microcontroller are you using?

You are working on an informatic project and what is most needed in an informatic project is sufficient detailed information.

best regards Stefan

Sorry about the lack of information. The Arduino is a nano, the leds are ws2812, a total of 300 leds. I'm talking about the serial output in this case. The leds are not lighting up either. The corruption looks like this.
Screenshot

I guess you will have to change to a microcontroller with more RAM or reduce the number of LEDs on your strip or not using an SD-Card

This is what the compiler reports

Sketch uses 13970 bytes (45%) of program storage space. Maximum is 30720 bytes.
Global variables use 1966 bytes (95%) of dynamic memory, leaving 82 bytes for local variables. Maximum is 2048 bytes.
Low memory available, stability problems may occur.

I'm not really sure the SD-card buffer takes another 512 bytes

best regards Stefan

Oh, so this buffer is not included in the compiler report?

I got 622 WS2812 on a Nano, but without an SD. Try without the SD calls. If that works, re-add the SD calls and keep adjusting the WS2812 number until they all fit.

Depending on what you do with the data stored on SD.

Especcially if the number of bytes is rather low and / or you change this data only once every 6 weeks it might be a solution to use an I2C-EEPROM or an I2C-FRAM-chip for storing this data.

But I haven't analysed your code what you do with the data on the SD-Card.

best regards Stefan

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