 int rollingAveragePotent();
 
 
+bool isUsbStickPluggedIn();
+
+// open a filename
+void usbOpenFile4Write(const String& filename);
+// write some data to an open file
+void write2file(const String& data);
+
 // util functions for writing in CSV format
 void writeCsvLabel();
 void writeCsvRow(int rotaryTickCount, int potentTickCount, double x, double y);
 
-
-// hacky because for some reason we can print in the setup() fxn
-bool wroteCsvHeader;
+// read all the input coming back from the usb controller
+String dumpUsbOutput();
+
+// this is just to add some dealy so that the usb device can start up
+// before we do operations on it
+long int started;
+bool usbDeviceWasSetup;
+// Write data to the usb device. If command must have the \r on it!
+void usbDeviceWrite(const String& data);
+// same as usbDeviceWrite but doesn't read all the response input after writing
+// to the device
+void usbDeviceWriteUnbuffered(const String& data);
+
+// setup the connection to the usb device
+void setupUsbDevice();
+void setupUsbStickForWrite(); // open files and add new section delimeter
+
+// alias to make clear that Serial1 is the usb device
+HardwareSerial& usbDev = Serial1;
+
+// whether the current usb stick has been 'prepd' for writing data
+// basically whether the file is open for writing and some other stuff
+bool usbStickIsPrepped = false;
  
 void setup() { 
-    pinMode(pinA,INPUT);
-    pinMode(pinB,INPUT);
+    pinMode(pinA, INPUT);
+    pinMode(pinB,  INPUT);
     /* Read Pin A
     Whatever state it's in will reflect the last position   
     */
     lastRotaryValue = digitalRead(pinA);
     initialPotentiometerReading = analogRead(A0);
-    wroteCsvHeader = false;
     Serial.begin (9600);
     Serial.println("Now running!");
-}
-
- void loop() { 
-  // if they disconnect from reading the output stream, resend the header data on reconnec
-  if (!Serial) {
-    wroteCsvHeader = false;
-  }
-
-  if (!wroteCsvHeader && Serial) // if(Serial) only available on Leonardo: detects if tty connected
-  {
+
+    started = millis();
+    usbDeviceWasSetup = false;
+}
+
+void loop() {
+  dumpUsbOutput();
+  
+  if (millis() - started < 1000) {
+    Serial.println("skipped");
+    return;
+  }
+
+  if (!usbDeviceWasSetup) {
+    usbDeviceWasSetup = true;
+    setupUsbDevice();
+    setupUsbStickForWrite();
     delay(150); // 150ms for the client to prepare for display cause clients were slow & dropping label packets
     writeCsvLabel();
-    wroteCsvHeader = true;
+    dumpUsbOutput();
   }
   
    int newRotaryValue = digitalRead(pinA);

    }
   }

   
    Serial.print("rotary="); Serial.print(rotaryTicks);
    Serial.print(", potent = "); Serial.println(newPotentiometerValue);

    writeCsvRow(rotaryTicks, newPotentiometerValue);


// ############################################
// CONFIG SECTION
bool veryVerboseDebug = true;

int pinA = 3;  // Connected to CLK on KY-040
int pinB = 4;  // Connected to DT on KY-040

int rotaryTicks = 0;
int lastRotaryTicks = 0;

const String usbFileName = "PONDS.PD";

// every n writes we manually close the file and reopen to prevent data
// loss. Lower n = slower perf, but safer
const int USB_WRITE_MANUAL_FLUSH_RATE = 2;

// END CONFIG SECT
// ############################################

int lastRotaryValue; // 1/0 read from rotary

