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();
}
}