 WiFiUDP Udp;
 char time_as_string[] = "12/20/1969 00:00:00";
 
+// LED pins
 const int redLed = 3;
 const int greenLed = 5;
 const int yellowLed = 6;
+
+// SD card
+const int chipSelect = 4;
+File logfile;
 
 void setup() {
   // Initial LEDs

  pinMode(yellowLed, OUTPUT);

  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  flash_3leds(redLed, greenLed, yellowLed, 5, 100);
  while (!Serial) { ; } // wait for serial port to connect.
  flash_3leds(redLed, greenLed, yellowLed, 5, 400);

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    while (true); // stop
  }

  String fv = WiFi.firmwareVersion();
  if ( fv != "1.1.0" )
    Serial.println("Please upgrade the firmware");
  Serial.print("Wifi firmware: ");
  Serial.println(fv);

  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, pass);

    delay(1000);
  }
  Serial.println("Connected to wifi");
  printWifiStatus();

  Serial.println("Connecting to time server.");
  Udp.begin(localPort);

  Serial.println("Getting time.");
  sendNTPpacket(timeServer); // send an NTP packet to a time server
  delay(500);
  if(  Udp.parsePacket() ) get_and_print_time();
}

void loop() {
  // if there are incoming bytes available
  // from the server, read them and print them:
  while (client.available()) {
    Serial.println("Client avaliable.");
    Serial.println("Disconnecting...");
    lastConnectionTime=millis();
    client.stop();
  }

  Udp.endPacket();
}

void get_and_set_time(void) {
    Udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer

    unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
    unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
    unsigned long secsSince1900 = highWord << 16 | lowWord;

    const unsigned long seventyYears = 2208988800UL;
    unsigned long epoch = secsSince1900 - seventyYears;
    setTime(epoch);
}

void time_to_string(unsigned long epoch, char* time_as_string) {
    epoch = now() + TIMEZONE * 3600;
    sprintf(time_as_string, "%02d/%02d/%4d %d:%02d:%02d",
          month(epoch), day(epoch), year(epoch),
          hour(epoch), minute(epoch), second(epoch));
}

void open_file(void) {
  char filename[] = "log_****-11-11_12:59:59.csv";

  sprintf(filename, "log_%4d-%02d-%02d_%02d:%02d:%02d.csv",
          year(), month(), day(), hour(), minute(), second());

  logfile = SD.open(filename, FILE_WRITE);
}
