SD CARD "Card failed, or not present"

i’m trying to make door access system with arduino mega and adfruit SD card attached to it’s TFT3.5" touch screen as a key pad for entering the users passcode
I stored userd data in binary file on the sd card and after user enter his passcode system will check for it in the file then print matched on the screen then open the door
but the problem is :
when i open the file for first time to read it works fine but when i try to open it for reading after that it gives me this message

“Card failed, or not present”
i don’t know the reason for this but i hope if any body can help me for this

this it the code i’m useing

#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_HX8357.h>
#include <TouchScreen.h>
#include <SD.h>
const int chipSelect = 4;//for sd card
// These are the four touchscreen analog pins
#define YP A14  // must be an analog pin, use "An" notation!
#define XM  A15 // must be an analog pin, use "An" notation!
#define YM  48  // can be a digital pin
#define XP  49  // can be a digital pin

// This is calibration data for the raw touch data to the screen coordinates
#define TS_MINX 150
#define TS_MINY 120
#define TS_MAXX 920
#define TS_MAXY 940

#define MINPRESSURE 10
#define MAXPRESSURE 1000

// These are 'flexible' lines that can be changed
#define TFT_CS 10
#define TFT_DC 9
#define TFT_RST -1 // RST can be set to -1 if you tie it to Arduino's reset

Adafruit_HX8357 tft = Adafruit_HX8357(TFT_CS, TFT_DC, TFT_RST);
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
#define PENRADIUS 3
int oldcolor, currentcolor;
unsigned long int enteredvalue;
int currentpage;

/////for reading users file from sd card
struct datastore {
 long int id_code;
 String id_name;
 long int tel_number;
 long int kepad_code;

};
File dataFile;

TSPoint p;


void setup() {
  SPI.begin();  
 Serial.begin(9600);
 Serial.println("HX8357D Test!");
 pinMode(53, OUTPUT);
 digitalWrite(53, HIGH);
 tft.begin(HX8357D);
 tft.setRotation(0);
 currentpage = homescreendraw();
 pinMode(relay1,OUTPUT);  
digitalWrite(relay1,HIGH); 

}

void loop() {
////when USER writ his passcode and pressed enter

if (p.x > 15 && p.x < 280 && p.y > 425 && p.y < 475) 
     {
            if (!SD.begin(chipSelect)) {
             Serial.println("Card failed, or not present");
              return;
             }
          Serial.println("card initialized.");
         dataFile = SD.open("datalog.dat", FILE_READ);
  while (dataFile.available())
   {
     struct datastore myData;
     dataFile.read((uint8_t *)&myData, sizeof(myData));
     if (myData.kepad_code == enteredvalue)
     {
        
        tft.setCursor(20, 75);
       tft.setTextColor(HX8357_BLACK);
       tft.setTextSize(4);
       tft.fillRect(20, 65, 290, 50, HX8357_GREEN);
       tft.println("matched");
       delay(1000);
       
       dataFile.close();
       }
    }

Before others say it, wrap your code in a code block. In the editor, it’s the < /> button. It’s also the [ code ] [ /code ] tags.

Also, move the following lines to your setup() function:

 if (!SD.begin(chipSelect)) {
      Serial.println("Card failed, or not present");
      return;
 }

Something isn’t right about the way you’re closing your file. It seems like it’s not in the correct block.

Hi, you can try initialising SD card only once in the setup loop itself, it doesn't need to be initialised every 1000 mS. Another thing I would suggest is that, you don't want to close the data file in every iteration, your job can be accomplished by keeping the file open and just every iteration.

And also as daleykd said, please put the code in the code block for the ease of understanding your code.

thanks for your note and your replay , I'm sorry it's just my first post and i didn't notice the rule for using the forum which explain how to send the code .

so , i did what you said for initialization and file closing I MOVED initialization to setup area and deleted the closing line but i still have the same problem is this problem has any thing related to SPI ?

Hello,

you say, you have a Adafruit SD. Adafruit use chipselect 10 you use 4 in your sketch.

regards ThomasD