 }
 
 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
-  }
-}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+    if (!SD.begin(CHIP_SELECT))  Blink(LED, 25);
+    if (debug) Serial.println("SD card initialized");
+  }
+  if (!dataFile.open("log.txt", O_RDWR | O_CREAT | O_AT_END)) {
+    if (debug) Serial.println("Error opening: log.txt");
+    Blink(LED, 100);
+    Blink(LED, 100);
+    return 0;
+  }
+  dataFile.print("Bad Packet Recieved at: ");
+  dataFile.print(utm);
+  dataFile.println();
+  dataFile.close();
+  return 1;
+}
+
+void checkSdCard() {
+  CARD_PRESENT = digitalRead(CARD_DETECT); //Check for card insertion
+  if (CARD_PRESENT == 0) { //if card NOT present....Wait and blink until it is
+    if (debug) Serial.println("Card Not Present");
+    while (CARD_PRESENT == 0) {
+//      CARD_PRESENT = digitalRead(CARD_DETECT);
+      Blink(LED, 50);
+    }
+//    if (!SD.begin(CHIP_SELECT))
+//      Blink(LED, 25);
+//    if (debug) Serial.println("SD card initialized");
+  }
+}
+

#include <RFM69.h>
#include <SPI.h>
#include "RTClib.h"
#include <SD.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       1 //debug false/true

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

/*==============|| SD ||==============*/
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

    Serial.print("RFM69 initialized");
    Serial.print(", key: ");
    Serial.println(KEY);
  }
}

