SD writing/reading. (String header not working)

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

float refresh_rate = 0.0;
int val = analogRead(A0);
long id = 1;

void setup() {
  Serial.begin(9600);
  Serial.println("Initializing Card");
  pinMode(4, OUTPUT);

  if(!SD.begin(4))
  {
    Serial.println("Card Failed!");
    return;
  }
  else
  Serial.println("Card Ready.");
  delay(5000);
  
  File commandFile = SD.open("COMMAND.txt");
  if (commandFile)
  {
    Serial.println("Reading Command File");

    float decade = pow(10, (commandFile.available() - 1));
    while(commandFile.available())
    {
      float temp = (commandFile.read() - '0');
      refresh_rate = temp*decade+refresh_rate;
      decade = decade/10;
    }
    Serial.print("Refresh Rate = ");
    Serial.print(refresh_rate);
    Serial.println("ms");
  }
 
  else
  {
    Serial.println("Couldn't read command file.");
    return;
  }
  delay(5000);
   
  File logFile = SD.open("Voltage & Current.csv", FILE_WRITE);
  if (logFile)
  {
    logFile.println(", ,");
    String header = "ID, Voltage, Current";
    logFile.println(header);
    logFile.close();
    Serial.println(header);
  }
  else
  {
    Serial.println("Couldn't open log file");
    return;
  }  
  delay(10000);
  return;
}
return;

void loop() {
  int Voltage = (val*(5/1024));
  int Current = ((val*(5/1024))/15);
  
 String dataString = String(id) + ", " + String(Voltage) + ", " + String(Current);

 File dataFile = SD.open("Voltage & Current.csv", FILE_WRITE);
  if(dataFile)
  {
    dataFile.println(dataString);
    dataFile.close();
    Serial.println(dataString);
  }
  else
  {
    Serial.println("Couldn't create datalog");
    return;
  }
  id++;

  delay (refresh_rate);
}

This is how far i’ve gotten after some time.
Everything should work according to tutorials and earlier topics.
Yet something goes wrong with creating the datalog.
After the part where it defines the header it jumps to end programme and keeps repeating (couldn’t create datalog!).
I am working with a Arduino MEGA2560 and the Arduino Ethernet Shield 2.

Once it tries to create the datalog it fails. What could this be the cause? :slightly_frowning_face:

I found this, maybe it will help.

Forum discussion of Ethernet shield problems

My own experience with the MEGA and SD is that the pins used have to be set up correctly

G

return;

Why is this after setup()?

  int Voltage = (val*(5/1024));
  int Current = ((val*(5/1024))/15);

5 / 1024 is 0. You might just as well assign 0 to Voltage and Current as waste your time calculating 0. (5.0 / 1024.0 is NOT 0).

 File dataFile = SD.open("Voltage & Current.csv", FILE_WRITE);

Is there some part of the 8.3 file name format that you think doesn't apply to you? It does.

    dataFile.println(dataString);

Pissing away resources on the String class so you can avoid doing:

dataFile.print(id);
dataFile.print(", ");
dataFile.print(Voltage);
dataFile.print(", ");
dataFile.println(Current);

is just too lame for words.

Zardof:
My own experience with the MEGA and SD is that the pins used have to be set up correctly

G

Just tried it, problem remains the same.
I can acces the SD card, and the busses are now the right lengt.
But it still cant write to the SD card. I checked for possible locks on it but cant find anything.

PaulS:

return;

Why is this after setup()?

I tried to return every process since I kept getting (Couldn't create datalog!) at 9600

  int Voltage = (val*(5/1024));

int Current = ((val*(5/1024))/15);



5 / 1024 is 0. You might just as well assign 0 to Voltage and Current as waste your time calculating 0. (5.0 / 1024.0 is NOT 0).

The reason why i need to calculate it is because its not a constand. It is a variable input to A0



File dataFile = SD.open("Voltage & Current.csv", FILE_WRITE);



Is there some part of the 8.3 file name format that you think doesn't apply to you? It does.]

Checked it and indeed i didn't realize that in the first place

The reason why i need to calculate it is because its not a constand. It is a variable input to A0

Nonsense. 5/1024 is a constant, with a value of 0. Multiplying a non-constant value by a constant value of 0 will result in a value of 0, pretty consistently.

The formula is still not the main problem. I can change that instantly. My problem remains that before it sais (can't create datalog) it sais (Couldn't open log file). Wich means after editing the filename should be a problem with the header function. Because that's where the programme stops working. After that it goes to (Can't create datalog!).

Atleast, that's what should be logical in my opinion.

It appears to me that the sketch can't create the file to write the header, and can't open the file to write data.

It appears to me that the problem is that the SD class only supports 8.3 format names, and that your name does not fit that format.

After you change the code to use a supported name, if you still have problems, post the new code.

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


float refresh_rate = 0.0;

long id = 1;

void setup() {
  Serial.begin(9600);
  Serial.println("Initializing Card");
  pinMode(4, OUTPUT);

  if(!SD.begin(4))
  {
    Serial.println("Card Failed!");
    return;
  }
  else
  Serial.println("Card Ready.");
  delay(5000);
  
  File commandFile = SD.open("COMMAND.txt");
  if (commandFile)
  {
    Serial.println("Reading Command File");

    float decade = pow(10, (commandFile.available() - 1));
    while(commandFile.available())
    {
      float temp = (commandFile.read() - '0');
      refresh_rate = temp*decade+refresh_rate;
      decade = decade/10;
    }
    Serial.print("Refresh Rate = ");
    Serial.print(refresh_rate);
    Serial.println("ms");
  }
 
  else
  {
    Serial.println("Couldn't read command file.");
    return;
  }
  delay(5000);
   
  File logFile = SD.open("LOG.csv", FILE_WRITE);
  if (logFile)
  {
    logFile.println(", ,");
    String header = "ID, Voltage, Current, Display";
    logFile.println(header);
    logFile.close();
    Serial.println(header);
  }
  else
  {
    Serial.println("Couldn't open log file");
    return;
  }  
  delay(10000);
}


void loop() {
  int val = analogRead(A10);
  float Voltage = val*(5.0/1023.0);
  float Current = ((val*(5.0/1023.0))/15);
  float Display = ((val*(5.0/1023.0))*3);
 String dataString = String(id) + ", " + String(Voltage) + ", " + String(Current) + ", " +String(Display);

 File dataFile = SD.open("LOG.csv", FILE_WRITE);
  if(dataFile)
  {
    dataFile.print(id);
    dataFile.print(", ");
    dataFile.print(Voltage);
    dataFile.print(", ");
    dataFile.print(Current);
    dataFile.print(", ");
    dataFile.print(Display);
    dataFile.close();
    Serial.println(dataString);
  }
  else
  {
    Serial.println("Couldn't create datalog");
    return;
  }
  id++;

  delay (refresh_rate);
}

Fixed it. Now everything runs as it is supposed to be!
Thanks for the help guys!

Dr