Problem with SD.remove(), SD.exists() and SD.mkdir()

Hello everyone,

I have a write a program that should control WS2811 Leds, therefore wait till data via Serie port comes. If after 10s nothing comes then goes in demo mode untill someting comes. The demo data is saved in a SD card. When data via serie comes, it should depending on te first 4-bit, play the data or play and save it for Demo. The last 4 bit closes the play/play and save mode and then arduino repeats this process.

Everithing above written works properly, but i have to erase the Archive before i save a new one, because if i don´t, then the Demo data is the old demo and the new one, something i don´t want.

I have tryed by many ways to do it, but as soon i use SD.remove(), SD.exists() or SD.mkdir() the Program “stops” working: if i send data in Play and save, won´t be saved, and demo mode won´t open the data already saved in the SD.

I dont have anymore ideas about the problem…

here is the code:

Thanks!

LedFertig1.2.ino (3.45 KB)

Sorry, here is the code ::slight_smile:

#include <SPI.h>
#include <SD.h>
#include "FastLED.h"
#define NUM_LEDS 240  //LEDs Zahl
#define DATA_PIN 3  //Port von Arduino wo DataIn steht

bool Sd;
byte led; //Zahl der LED, der wird angeschaltet ab jetzt ADR
int Time = 0;
int Demo = 0;
byte sent=0;
byte buff[4];
File Archive;

// Define the array of leds
CRGB leds[NUM_LEDS];
void setup() { 
       
       FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS); //Initialize FastLED für WS2812B
       Serial.begin(250000);  //Initialize Serie 9600 Baud
       Serial.write('1');
       pinMode(4, OUTPUT);
       Sd=SD.begin(4);
       if (!Sd){    //LED 22 will be RED if the SD card wasnt properly connected, else LED 22 will be green.
            leds[22]=CRGB(0,255,0);
            FastLED.show();
            return;
            }
       leds[22]=CRGB(255,0,0);FastLED.show();
}

void loop() { 
  while(Serial.available() < 4){  //Wait until MODE Byte comes
  Time++;
  delay(1);
  if(Time>=10000||Demo==1){  //After 10 seconds waiting starts demo mode, the isn´t 10s delay between Demo modes.
    Time=0;
    Demo=1;
    DEMO();
  }
  }
  Time=0; //Sets the variables to 0, when data stops coming, arduino will wait 10s until starting the Demo mode.
  Demo=0;
  Serial.readBytes( (byte*)(&buff), 4); 
  if(buff[0]==240&&buff[2]==220){
  SHOW();
  }else if (buff[0]==240&&buff[2]==240){
   SAVEandSHOW();
  }
}
void DEMO(){
  leds[19]=CRGB(0,255,0);FastLED.show();
  if(Archive != false){
  Archive.close();
  }
  Archive = SD.open("RGB-SD.txt",FILE_READ);
  //delay(200);
  if (Archive != false){
  leds[20]=CRGB(255,0,0);FastLED.show();
  while (Archive.available() > 4&&Serial.available() < 4){//&&Serial.available() < 4
  leds[20]=CRGB(0,0,0);FastLED.show();
  //reads 4 bytes from Archive
  Archive.read(buff,4);
  leds[buff[0]] = CRGB(buff[1],buff[2],buff[3]);FastLED.show();
  leds[20]=CRGB(255,0,0);FastLED.show();
  }
  leds[20]=CRGB(0,0,0);FastLED.show();
  Archive.close();
  leds[20]=CRGB(255,0,0);FastLED.show();
  }else{
  leds[20]=CRGB(0,0,0);FastLED.show();
  leds[20]=CRGB(0,255,0);FastLED.show();
}
}
void SAVEandSHOW(){
  leds[19]=CRGB(255,0,0);FastLED.show();
  if(buff[3]==240){
   Archive.close(); 
    //SD.remove((const char *)root.c_str()); //Must use (char *)filename.c_str() to work.
    //SD.remove("RGB-SD.TXT");
    //SD.remove(filename);
    delay(450);
    }
  if(Archive != false){
  Archive.close();
  }
  //SD.mkdir("A");
  Archive = SD.open("RGB-SD.txt",FILE_WRITE);
  while(Serial.available() < 4){}
  while(buff[2]!=255){//buff[0]!=240&&
  while(Serial.available() < 4){}
  //Serial.readBytes( (byte*)(&led), 1);  
  Serial.readBytes( (byte*)(&buff), 4);  //Lisst ROT, GRÜN und BLAU und schreibt in led[]
  leds[buff[0]]=CRGB(buff[1],buff[2],buff[3]);FastLED.show();    //Anschalt die LED(ADR,ROT,GRÜN,BLAU) 
  if (Archive != false){
  leds[20]=CRGB(255,0,0);FastLED.show();  
  Archive.write(buff[0]);
  Archive.write(buff[1]);
  Archive.write(buff[2]);
  Archive.write(buff[3]); 
  leds[20]=CRGB(0,0,0);FastLED.show();
  } else {
  leds[20]=CRGB(0,0,0);FastLED.show();
  leds[20]=CRGB(0,255,0);FastLED.show();  
  }
  }
  Archive.close();
}
void SHOW(){
  leds[19]=CRGB(0,0,255);FastLED.show();
  while(Serial.available() < 4){}
  while(buff[2]!=255){//buff[0]!=240&&
  while(Serial.available() < 4){} 
  Serial.readBytes( (byte*)(&buff), 4);  //Lisst ROT, GRÜN und BLAU und schreibt in led[]
  leds[buff[0]]=CRGB(buff[1],buff[2],buff[3]);FastLED.show();    //Anschalt die LED(ADR,ROT,GRÜN,BLAU) 
  }
}

I've not read your code (yet) but the communication between the microcontroller and the SD card uses SPI, and so does the FastLED library for the WS2811 if I'm not mistaken (at pretty low level).

are you taking this into consideration?

I haven´t, i am pretty new with both...

I am using a arduino uno, the WS2812b(sorry they aren´t ws2811) are conected on pin 3, so i shouldn´t have any problem, if i have understood correctly i am not using the hardware SPI to control the WS2812b´s. To control the Sd i am using the harware SPI and pin 4 as CS.

The exact problem that i have is:

I am using only 13 WS2812b, but i have 30 connected to arduino. I use Led 19,20 and 22 as debugger.
Led 20 will blink green if the Sd archive is correctly opened and red if isn´t.

here is the code that does it:
SAVEandShow():

if (Archive != false){
  leds[20]=CRGB(255,0,0);FastLED.show();  
  Archive.write(buff[0]);
  Archive.write(buff[1]);
  Archive.write(buff[2]);
  Archive.write(buff[3]); 
  leds[20]=CRGB(0,0,0);FastLED.show();
  } else {
  leds[20]=CRGB(0,0,0);FastLED.show();
  leds[20]=CRGB(0,255,0);FastLED.show();  
  }

DEMO():

if (Archive != false){
  leds[20]=CRGB(255,0,0);FastLED.show();
  while (Archive.available() > 4&&Serial.available() < 4){//&&Serial.available() < 4
  leds[20]=CRGB(0,0,0);FastLED.show();
  //reads 4 bytes from Archive
  Archive.read(buff,4);
  leds[buff[0]] = CRGB(buff[1],buff[2],buff[3]);FastLED.show();
  leds[20]=CRGB(255,0,0);FastLED.show();
  }
  leds[20]=CRGB(0,0,0);FastLED.show();
  Archive.close();
  leds[20]=CRGB(255,0,0);FastLED.show();
  }else{
  leds[20]=CRGB(0,0,0);FastLED.show();
  leds[20]=CRGB(0,255,0);FastLED.show();
}

If i use the code like without SD.remove(), evething works properly, DEMO() and SAVEandSHOW() haven´t any problem to save and play the data in/from the SD and led 20 blinks green.

As soon as i use SD.remove() led 20 blinks red and DEMO() and SAVEandSHOW() doesn´t work.

Indeed if you are on a UNO the library will not use SPI for your LEDs connected on pin 3 and move to Bit banging (the library automatically uses the hardware SPI only if the data pin and clock pin happened to be pins that have hardware SPI available)

what type of SD card do yo have? did you write a sample code that is just doing what you want without the LEDs?

Sorry for my Delay, Exams season it´s coming.... :o :o :o

I am using the UNO, wireless SD shield and a Trascend 2GB card for testing purpouses and a nano ch340 with a SD module as the following one (didn´t found a serial number) for the final product: |500x500

the SD card is the same in both cases, i have also a Trascend of 10GB but the result is exact the same.

CRGB leds[NUM_LEDS];Could that be part of your problem?

did you write a sample code that is just doing what you want without the LEDs?

i did test another programs from internet that use SD.remove() without the LEDs, and they work properly but i can understand if the Problem source are my LEDs, why the problem appears only when i use SD.remove(), SD.exists() or SD.mkdir().

i am going to do a test with those Programs adding LEDs to check if the still work.

AWOL: CRGB leds[NUM_LEDS];Could that be part of your problem?

AWOL, i don't uderstand what you mean... :-\

did you write a sample code that is just doing what you want without the LEDs?

Update:

I have tested out the example of the SD library called “Files” it uses SD.open() and SD.remove(),
as long as i use the original code, everithing works properly. As soon as i define the LEDs with

FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);

the program stops working properly, so i would say that as J.M.L said, there is a compatibility problem between LEDs and SD, but how i can solve it? maybe changing the data pin of the LEDs? And why didn’t appear in my program this problem until i use SD.remove()?!

Files code:

/*
  SD card basic file example

 This example shows how to create and destroy an SD card file
 The circuit:
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)

 created   Nov 2010
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe

 This example code is in the public domain.

 */
#include <SPI.h>
#include <SD.h>

File myFile;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  if (SD.exists("example.txt")) {
    Serial.println("example.txt exists.");
  } else {
    Serial.println("example.txt doesn't exist.");
  }

  // open a new file and immediately close it:
  Serial.println("Creating example.txt...");
  myFile = SD.open("example.txt", FILE_WRITE);
  myFile.close();

  // Check to see if the file exists:
  if (SD.exists("example.txt")) {
    Serial.println("example.txt exists.");
  } else {
    Serial.println("example.txt doesn't exist.");
  }

  // delete the file:
  Serial.println("Removing example.txt...");
  SD.remove("example.txt");

  if (SD.exists("example.txt")) {
    Serial.println("example.txt exists.");
  } else {
    Serial.println("example.txt doesn't exist.");
  }
}

void loop() {
  // nothing happens after setup finishes.
}

Files modified code:

/*
  SD card basic file example

 This example shows how to create and destroy an SD card file
 The circuit:
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)

 created   Nov 2010
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe

 This example code is in the public domain.

 */
#include <SPI.h>
#include <SD.h>
#include "FastLED.h"
#define NUM_LEDS 240  //LEDs Zahl
#define DATA_PIN 3

File myFile;
CRGB leds[NUM_LEDS];

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);

  Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  if (SD.exists("example.txt")) {
    Serial.println("example.txt exists.");
  } else {
    Serial.println("example.txt doesn't exist.");
  }

  // open a new file and immediately close it:
  Serial.println("Creating example.txt...");
  myFile = SD.open("example.txt", FILE_WRITE);
  myFile.close();

  // Check to see if the file exists:
  if (SD.exists("example.txt")) {
    Serial.println("example.txt exists.");
  } else {
    Serial.println("example.txt doesn't exist.");
  }

  // delete the file:
  Serial.println("Removing example.txt...");
  SD.remove("example.txt");

  if (SD.exists("example.txt")) {
    Serial.println("example.txt exists.");
  } else {
    Serial.println("example.txt doesn't exist.");
  }
}

void loop() {
  // nothing happens after setup finishes.
  leds[22]=CRGB(255,0,0);FastLED.show();
  leds[22]=CRGB(0,0,0);FastLED.show();
}

AWOL, i don't uderstand what you mean...

How much memory are you using? How much do you really have available at run time? When you open a file?

http://playground.arduino.cc/Code/AvailableMemory

PaulS: How much memory are you using? How much do you really have available at run time? When you open a file?

http://playground.arduino.cc/Code/AvailableMemory

15.094 Bytes ROM (46%) 1.706 Bytes RAM (83%)

I become a inestability warning

Should i use a ligther library?

Try halving the number of LEDs and see if the code behaves as you expect.

Should i use a ligther library?

For what? Why do you suppose that a different library will provide all the needed functionality while using less SRAM? Do you suppose that the author of the library(s) that you are using deliberately pissed away memory?

AWOL: Try halving the number of LEDs and see if the code behaves as you expect.

PaulS: How much memory are you using? How much do you really have available at run time? When you open a file?

http://playground.arduino.cc/Code/AvailableMemory

that was the problem! :drooling_face: Now its working properly with SD.remove :) :) :) :) And the best thing of all it's that i only use 13 LEDs..... I did choose 240 as a random number that didn't limit in the future my LED number

Thank you all