   if (!SD.begin(CHIP_SELECT)) { //Checks the SD card to make sure it responds. On success, it initializes it.
     if (debug) Serial.println("Card failed, or not present");
     CARD_PRESENT = digitalRead(CARD_DETECT);  //read CD pin
+    //this delays the program while there is no card inserted.
     while (CARD_PRESENT == 0) { //The CD pin is 0 if there is no card inserted
       CARD_PRESENT = digitalRead(CARD_DETECT); //read it the value for every loop, or we will never exit the While Loop
-      Blink(LED, 50); //blink!
-    }
-    if (!SD.begin(CHIP_SELECT))
-      Blink(LED, 50);
-    if (debug) Serial.println("SD card initialized"); //when the SD card is re-inserted, intiliaze it!
+      Blink(LED, 50); //blink to show an error.
+    }
+    delay(10); //wait for card to stablize from insertion before initializing
+
+    if (!SD.begin(CHIP_SELECT)) //when the SD card is re-inserted, intiliaze it!
+      Blink(LED, 50); //if it fails again....well, you're probably out of luck.
+    if (debug) Serial.println("SD card initialized");
   }
   if (debug) Serial.println("SD card initialized"); ///Victory
 

}

int badPacket(long utm) {
  //ID, time, sensor, temp, excite volts, battery volts, number of attempts
  CARD_PRESENT = digitalRead(CARD_DETECT);
  if (CARD_PRESENT == 0) {
    Blink(LED, 25);
    Blink(LED, 25);
    if (debug) Serial.println("Card Not Present");
    while (CARD_PRESENT == 0) {
      CARD_PRESENT = digitalRead(CARD_DETECT);
      Blink(LED, 100);
    }
    if (!SD.begin(CHIP_SELECT))  Blink(LED, 25); //Serial.println("SD card initialized");
    else Blink(LED, 25); Blink(LED, 25); //Serial.println("SD Card Failed to Initialize");
  }
  //Write data to card
  String fileName = String("log.txt");
  char __fileName[fileName.length() + 1];
  fileName.toCharArray(__fileName, sizeof(__fileName));
  File dataFile = SD.open(__fileName, FILE_WRITE); //Opens file (creates if it doesn't exist) and Appends data to the end of it
  if (dataFile) {
    dataFile.print("Bad Packet Recieved at: ");
    dataFile.print(utm);
    dataFile.println();
    dataFile.close();
    return 1; //returns a 1, Success
  }
  else {
    if (debug) {
      Serial.print("Error opening: ");
      Serial.println(fileName);
    }
    Blink(LED, 100);
    Blink(LED, 100);
    return 0; //returns a 0, failure
  }
}



















#include <RFM69.h>
#include <SPI.h>
#include "RTClib.h"
#include <SdFat.h>

#define NODE_ID     0 //Network Node ID. All Senders must send to this ID #
#define NETWORK_ID  101 //Network ID. All Nodes must be on the same Network
#define FREQUENCY   RF69_433MHZ //Match this to the frequency of your radio
#define KEY         "p6ZNvTmGfdY2hUXb" //has to be same 16 characters/bytes on all nodes.
#define LED         3 //LED Pin Number
#define debug       0 //debug false/true

/*==============|| DS3231_RTC ||==============*/
RTC_DS3231 rtc; //Initialize the Real Time Clock
DateTime now; //initialize a DateTime Object



/*==============|| SD ||==============*/
SdFat SD; //This is the SD Card
SdFile dataFile; //this is the DatFile
const int CHIP_SELECT = 4; //SPI SS pin for SD card
const int CARD_DETECT = 5; //Card Detect Pin
int CARD_PRESENT; //var for Card Detect sensor reading
