SD.remove doesn't work

Greetings,

i've posted before about this problem burt i cannot seem to make it work... I've tried the SD.remove commmand in the original library, millions of examples from the internet, tried if the sensors actually work...IT all works except the damn SD.remove. Why i want to to this SD.remove is because by deleteing the file and that creating a new one, in which the highscore should be stored, i can easily than read from the sd card to write the high score on a LCD.
In the test.txt file the high score should be saved, but first the previously saved one should be removed...In the picture attached you can see a failed attempt on the serial monitor...Thank you very much for any kind of help.

/*
  SD card read/write

  This example shows how to read and write data to and from 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 <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

char line[25];
byte index = 0;

File myFile;
long highscore = 0;
bool reading;
const int button = 49;

unsigned long prejsni = 0;
const long interval = 100;

// the buttons
struct buttons
{
  byte pin;
  byte led;
  bool state;
  bool prevState;
  byte score;
  byte pressed;
};
const byte buttonPins[] = {13, 14, 15 , 16, 17, 18, 19, 20 , 21, 22};
const byte numberOfButtons = sizeof(buttonPins) / sizeof(buttonPins[0]);
buttons mybuttons[numberOfButtons];
const byte ledPins[] = {23, 24, 25 , 26, 27, 28, 29, 30, 31, 32};
const byte scores[] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};

long runningTotal = 0;
byte numberOfButtonsPressed = 0;

//the effect led
int effectLedInterval;
int effectLedIntervalMax = 100;
int effectLedIntervalMin = 30;
unsigned long previousMillisEffect;
bool effectState;
bool doEffect = false;

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

  lcd.begin(16, 2);
  lcd.clear();
  lcd.setCursor(4, 0);
  lcd.print("SUPER MARIO");
  lcd.setCursor(5, 1);
  lcd.print("PINBALL");
  delay(2500);
  Serial.println(" ");
  Serial.print("Initialising "); Serial.print(numberOfButtons); Serial.println(" buttons");
  for (int i = 0; i < numberOfButtons; i++)
  {
    mybuttons[i].pin = buttonPins[i];
    pinMode(mybuttons[i].pin, INPUT_PULLUP);
    mybuttons[i].led = ledPins[i];
    pinMode(mybuttons[i].led, OUTPUT);
    mybuttons[i].state = digitalRead(mybuttons[i].pin);
    mybuttons[i].prevState = mybuttons[i].state;
    mybuttons[i].score = scores[i];
    mybuttons[i].pressed = 0;
    Serial.print("Button: "); Serial.print(i);
    Serial.print(", Pin: "); Serial.print(mybuttons[i].pin);
    Serial.print(", Led: "); Serial.print(mybuttons[i].led);
    Serial.print(", Score: "); Serial.print(mybuttons[i].score);
    Serial.print(", Pressed: "); Serial.print(mybuttons[i].pressed);
    Serial.print(", State: "); Serial.print(mybuttons[i].state);
    Serial.print(", Prev state: "); Serial.println(mybuttons[i].prevState);
  }
  // Open serial communications and wait for port to open:

  pinMode(49, INPUT_PULLUP);

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

  if (!SD.begin(53)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");
  ////////////////////////////////////////////////////////////////////////
  //Izpis high score-a iz SD kartice ob ponovnem zagonu.
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      line[index] = (myFile.read());
      // check if character is '\r'
      if (line[index] == '\r')
      {
        // add terminatin NUL character
        line[index] == '\0';
        // done
        break;
      }
      // next character will go in next element of line
      index++;
    }
    // close the file:
    highscore = atol(line);
    myFile.close();
  }
}
void loop() {
  waitforbuttonpress();
  checkForButtonStateChange();
  if (doEffect) blinkLeds();
  text();
}

void checkForButtonStateChange()
{
  for (int i = 0; i < numberOfButtons; i++)
  {
    mybuttons[i].state = digitalRead(mybuttons[i].pin);
    // compare the buttonState to its previous state
    if (mybuttons[i].state != mybuttons[i].prevState) // means it changed... but which way?
    {
      if (mybuttons[i].state == LOW)  // changed to pressed
      {
        Serial.print(i);
        Serial.print(" newly pressed");
        if (mybuttons[i].pressed == 0)
        {
          mybuttons[i].pressed = 1;
          numberOfButtonsPressed++;
          Serial.print(", Unique buttons pressed "); Serial.print(numberOfButtonsPressed);

        }
        digitalWrite(mybuttons[i].led, HIGH);
        runningTotal = runningTotal + mybuttons[i].score;
        Serial.print(", Score "); Serial.println(runningTotal);
      }
      // poor man's de-bounce
      delay(50);
    }
    // save the current state as the last state, for next time through the loop
    mybuttons[i].prevState = mybuttons[i].state;
  }

  if (numberOfButtonsPressed == 5)
  {
    Serial.print("All leds on, score "); Serial.println(runningTotal);
    for (int i = 0; i < numberOfButtons; i++)
    {
      mybuttons[i].pressed = 0;
      //      audio.play("mario.wav", 1, 0);
    }
    doEffect = true;
    numberOfButtonsPressed = 0;
  }
} // checkForButtonStateChange()

void blinkLeds()
{
  static byte numberOfEffects = 0;
  if (millis() - previousMillisEffect >= effectLedInterval)
  {
    effectLedInterval = random(effectLedIntervalMin, effectLedIntervalMax);
    previousMillisEffect = millis();
    effectState = !effectState;
    for (int i = 0; i < numberOfButtons; i++)
    {
      digitalWrite(mybuttons[i].led, effectState);
    }
    numberOfEffects++;
  }

  if (numberOfEffects == 25)
  {
    doEffect = false;
    numberOfEffects = 0;
    for (int i = 0; i < numberOfButtons; i++)
    {
      digitalWrite(mybuttons[i].led, LOW);
    }
  }
}

void waitforbuttonpress()
{
  reading = digitalRead(49);
  if (reading == 0) { //button was pressed
    delay(100);
    if (runningTotal > highscore) {  //check if current score > highsore
      highscore = runningTotal;      //if current score> highscore, write that score to SD card
      delay(100);
       if (SD.remove("test.txt")) {
        Serial.println("test.txt removed.");
      } else {
        Serial.println("test.txt wasn't removed.");
      }
      if (SD.exists("test.txt")) {
        Serial.println("test.txt exists.");
      } else {
        Serial.println("test.txt doesn't exist.");
      }
//      delay(100);
//      myFile = SD.open("test.txt", FILE_WRITE);
//      if (myFile) {
//        Serial.print("Writing to test.txt...");
//        myFile.println(highscore);
//        myFile.close();
//      }
    }
  }
}

void text() {
  if (millis() - prejsni >= interval) {
    prejsni = millis();
    lcd.clear();
    lcd.setCursor(4, 0);
    lcd.print(runningTotal);
    lcd.setCursor(4, 1);
    lcd.print(highscore);
  }
}

  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)

You should remove that as it obviously isn't true in your case.

Why do you want to remove the file first? If you open it the way the commented code shows it will overwrite the previous content anyway.

My guess: the SD card is write protected.

pylon:
My guess: the SD card is write protected.

My guess too ... I've got lot of issues with SD lib... but not that one.
Don't waste time, swap to SDFat a smarter and faster lib.

I got two warnings when I compiled the sketch:

/Users/john/Documents/Arduino/sketch_feb28a/sketch_feb28a.ino: In function 'void setup()':
/Users/john/Documents/Arduino/sketch_feb28a/sketch_feb28a.ino:127:21: warning: statement has no effect [-Wunused-value]
         line[index] == '\0';
         ~~~~~~~~~~~~^~~~~~~
/Users/john/Documents/Arduino/sketch_feb28a/sketch_feb28a.ino: In function 'void blinkLeds()':
/Users/john/Documents/Arduino/sketch_feb28a/sketch_feb28a.ino:193:39: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if (millis() - previousMillisEffect >= effectLedInterval)
       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
Sketch uses 14782 bytes (45%) of program storage space. Maximum is 32256 bytes.
Global variables use 1390 bytes (67%) of dynamic memory, leaving 658 bytes for local variables. Maximum is 2048 bytes.

The first one is pointing out that you are using '==' (comparison) where you meant to use '=' (assignment).

The second one can be fixed by using 'unsigned' instead of 'int' for effectLedInterval, effectLedIntervalMax, and effectLedIntervalMin.

johnwasser:
The second one can be fixed by using 'unsigned' instead of 'int' for effectLedInterval, effectLedIntervalMax, and effectLedIntervalMin.

Correction: by using 'unsigned long' instead of 'int' !

Erik_Baas:
Correction: by using 'unsigned long' instead of 'int' !

The min and max values are 30 and 100 respectively. It is not at all necessary to use 32 bits. In fact, uint8_t/byte/unsigned char is large enough and will work just as well as unsigned or unsigned long.

johnwasser:
will work just as well as unsigned

But... unsigned what ??

Erik_Baas:
But... unsigned what ??

'unsigned' means 'unsigned int', just like 'unsigned long' means 'unsigned long int'.

Aha! I knew about 'unsigned long' but not about 'unsigned'. Was almost sure it was a typo... :wink: Thank you!

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