Go Down

Topic: Is SD.begin a one-time thing??After removing card, it can't reinitialize... (Read 267 times) previous topic - next topic

phoenix1118

this is the code
Code: [Select]
#include <LiquidCrystal.h>
#include <SD.h>


LiquidCrystal lcd(7,6,5,4,3,2);

const int analogInPin = A3; 
const int lcdLed=13;
const  int runLed=24;
const int chipSelect=53;

int ledState=LOW;
File myFile;
unsigned long number;


int i;
int counter1=0;
int counter2=0;
int mmenu[10];     
const int rxPin=10;
const int txPin=11;
const int byteSize=64;


bool startDataReceived=false;
bool dataAvailable=false;
bool sdInserted=false;


void setup(){
  pinMode(lcdLed,OUTPUT);
  digitalWrite(lcdLed,HIGH);
  pinMode(runLed,OUTPUT);
  pinMode(chipSelect,OUTPUT);
  lcd.begin(16,2);
  Serial.begin(9600);
  mySerial.begin(9600);
  insertCard();
}

void insertCard(){
  while(!SD.begin(chipSelect)){
    lcd.setCursor(2,0);
    lcd.write("Please Insert");
    lcd.setCursor(4,1);
    lcd.write("SD Card...");
    delay(1000);
    lcd.clear();
  }
  lcd.clear();
  lcd.setCursor(1,0);
  lcd.print("Press Blinking");
  lcd.setCursor(4,1);
  lcd.print("Button...");
  while(ledState==LOW){
    ledState=HIGH;
    digitalWrite(runLed,ledState);
    delay(500);
    ledState=LOW;
    digitalWrite(runLed,ledState);
    delay(500);
    Analogkeyinput();
    delay(500);
    if((mmenu[0]<225&&mmenu[0]>205)){
      ledState=HIGH;
    }
  }
  digitalWrite(runLed,LOW);
  lcd.clear();
  lcd.setCursor(4,0);
  lcd.write("SD Card");
  lcd.setCursor(2,1);
  lcd.write("Validating...");
  delay(5000);
  myFile=SD.open("CARDID.txt");
  if(myFile){
    char fileStr[9];
    int j;
    for(j=0;j<10;j++){
      fileStr[j]=myFile.read();
    }
    char result[9];
    uint8_t k = 0;
    while (fileStr[k] != '\0' && k < sizeof(result) - 1){
      result[k] = fileStr[k];
      k++;
    }
    result[k] = '\0';
    number = atol(result);
    digitalWrite(runLed,LOW);
  }
  if(number==18231801){
    myFile.close();
    lcd.clear();
    delay(1000);
    lcd.setCursor(4,0);
    lcd.write("SD Card");
    lcd.setCursor(4,1);
    lcd.write("Valid!");
    delay(1000);
  }
  else{
    digitalWrite(runLed,LOW);
    delay(1000);
    lcd.clear();
    lcd.setCursor(4,0);
    lcd.write("SD Card");
    lcd.setCursor(4,1);
    lcd.write("Invalid!");
    myFile.close();
    delay(500);
  }
}

void Analogkeyinput(){
  int Ak = analogRead(analogInPin);
  int Dk = map(Ak,0,1023,0,255);
  mmenu[0]=Dk;
}
void loop(){
  insertCard();
}

problem is after i remove the card...i can get back to blinking please inserd card...but when i reinsert the card, the please insert the card won't blink and i get stuck with the permanent please insert  card....i can't go back to rereading the file

phoenix1118

i'm sorry i forgot to edit some
Code: [Select]
#include <LiquidCrystal.h>
#include <SD.h>


LiquidCrystal lcd(7,6,5,4,3,2);

const int analogInPin = A3; 
const int lcdLed=13;
const  int runLed=24;
const int chipSelect=53;

int ledState=LOW;
File myFile;
unsigned long number;


int i;
int counter1=0;
int counter2=0;
int mmenu[10];     
const int rxPin=10;
const int txPin=11;
const int byteSize=64;


bool startDataReceived=false;
bool dataAvailable=false;
bool sdInserted=false;


void setup(){
  pinMode(lcdLed,OUTPUT);
  digitalWrite(lcdLed,HIGH);
  pinMode(runLed,OUTPUT);
  pinMode(chipSelect,OUTPUT);
  lcd.begin(16,2);
  Serial.begin(9600);
  mySerial.begin(9600);
}

void insertCard(){
  while(!SD.begin(chipSelect)){
    lcd.setCursor(2,0);
    lcd.write("Please Insert");
    lcd.setCursor(4,1);
    lcd.write("SD Card...");
    delay(1000);
    lcd.clear();
  }
  lcd.clear();
  lcd.setCursor(1,0);
  lcd.print("Press Blinking");
  lcd.setCursor(4,1);
  lcd.print("Button...");
  while(ledState==LOW){
    ledState=HIGH;
    digitalWrite(runLed,ledState);
    delay(500);
    ledState=LOW;
    digitalWrite(runLed,ledState);
    delay(500);
    Analogkeyinput();
    delay(500);
    if((mmenu[0]<225&&mmenu[0]>205)){
      ledState=HIGH;
    }
  }
  digitalWrite(runLed,LOW);
  lcd.clear();
  lcd.setCursor(4,0);
  lcd.write("SD Card");
  lcd.setCursor(2,1);
  lcd.write("Validating...");
  delay(5000);
  myFile=SD.open("CARDID.txt");
  if(myFile){
    char fileStr[9];
    int j;
    for(j=0;j<10;j++){
      fileStr[j]=myFile.read();
    }
    char result[9];
    uint8_t k = 0;
    while (fileStr[k] != '\0' && k < sizeof(result) - 1){
      result[k] = fileStr[k];
      k++;
    }
    result[k] = '\0';
    number = atol(result);
    digitalWrite(runLed,LOW);
  }
  if(number==18231801){
    myFile.close();
    lcd.clear();
    delay(1000);
    lcd.setCursor(4,0);
    lcd.write("SD Card");
    lcd.setCursor(4,1);
    lcd.write("Valid!");
    delay(1000);
  }
  else{
    digitalWrite(runLed,LOW);
    delay(1000);
    lcd.clear();
    lcd.setCursor(4,0);
    lcd.write("SD Card");
    lcd.setCursor(4,1);
    lcd.write("Invalid!");
    myFile.close();
    delay(500);
  }
}

void Analogkeyinput(){
  int Ak = analogRead(analogInPin);
  int Dk = map(Ak,0,1023,0,255);
  mmenu[0]=Dk;
}
void loop(){
  insertCard();
}

Go Up