Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Weird problem in interfacing between Arduino Uno and SD card

Status
Not open for further replies.

mamech

Full Member level 3
Full Member level 3
Joined
Nov 9, 2010
Messages
176
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,296
Activity points
3,135
hello everyone

I have a weird problem. I am trying to interface an Arduino Uno with an SD card. The code is made to produce a new file every timer interval. The logic seemed simple to me, but the results are not logical at all!
this is the code:
Code:
#include <SPI.h>  //SPI communication protocol library
#include <SD.h>  //SD card interface library

double File_name_counter=1;
String Fixed_File_Name = "ex";
String Variable_File_Name = "";
String Extension = ".txt";
char temp[9] = "ex.txt";
int i=0;
int soft_counter =0;
void setup()
{
  // Open serial communications:
  Serial.begin(9600);
  
  Serial.print("Initializing SD card...");
  pinMode(10, OUTPUT);  // Comment from Arduino page: This can be the hardware SS pin - pin 10 (on most Arduino boards) or 
                        // pin 53 (on the Mega) - or another pin specified in the call to SD.begin(). Note that
                        // even if you don't use the hardware SS pin, it must be left as an output or the SD library won't work.

  if (!SD.begin(4))  // begin SD proces, and make the SS/CS pin to be on pin 4, and check if it returns true or false
  {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  delay(3000);

}


void loop()
{
  
  File myFile;
  myFile = SD.open(temp, FILE_WRITE);
 if (myFile) {
    Serial.println("Writing to the file");
    i++;
    myFile.println(i);
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening file");
  }
  delay(1000);      //delay
  
  soft_counter++;
  if(soft_counter>30)
  {
    Variable_File_Name= Fixed_File_Name+File_name_counter+Extension;  // construct new file name
    Variable_File_Name.toCharArray(temp,9) ;   // convert file name from String to CharArray

    File_name_counter++;
    soft_counter=0;
  }
}


This code writes only one file with incrementing numbers in it, and when the part after delay function begins , the serial monitor gives several times :
error opening file
error opening file
error opening file


can any one tells me what is the problem in this code? I made numerous modifications nut no logical response.
 

hi m,
I would add a file name check/debug after this code in order to ensure a correct name construct.

Variable_File_Name= Fixed_File_Name+File_name_counter+Extension; // construct new file name
Variable_File_Name.toCharArray(temp,9) ; // convert file name from String to CharArray
Serial.println(Variable_File_Name);

E
 

thanks for advice, but before I made this equation, I have already made this step for checking to be sure that the output is really a char array that contains the correct file name that increment with incrementing of variable "File_name_counter"
 

hi m,
I have programmed my working Arduino Mega and SD Shield data logger with your Sketch.
For some reason it reports 'error opening file', I will relook at the cause, let you know what I find.

I have reduced the software counter in order to get the constructed FileName more often.

Note when the FileNameCounter is Double , the FileName has 1.00 as the Index which means two 'DPs!' in the FileName.
Changing the Double Long gives an acceptable FileName construct.
Ref the attached images.

E

- - - Updated - - -

hi m,
The above 'Arduino Mega and SD Shield data logger', should read Arduino UNO and SD Shield data logger.
I will change over to the MEGA later.
E

- - - Updated - - -

hi,
The attached EDA_SD1.txt file works OK on a UNO, I have verified that the data is being written to the SD Card.
Change the file extension to '.ino'.
Attached is a clip of the Serial data.
E
 

Attachments

  • A002.gif
    A002.gif
    3.7 KB · Views: 157
  • A003.gif
    A003.gif
    3.2 KB · Views: 161
  • EDA_SD1.txt
    1.4 KB · Views: 145
  • A006.gif
    A006.gif
    3.7 KB · Views: 151
  • Like
Reactions: mamech

    mamech

    Points: 2
    Helpful Answer Positive Rating
it works now, thank you very much! I was about to be mad because I did not think that data type can cause all of this mess.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top