CCCPaste
#include 
#include 
#include 
File dataFile;

byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
//10.71.148.177 home 
IPAddress ip(10, 71, 148, 177);
//IPAddress ip(10, 1, 0, 177);
EthernetServer server(80);
//Function defs
void rotorSense(EthernetClient client);
void rotorA(EthernetClient client);
void rotorB(EthernetClient client);
void startPage(EthernetClient client);
void endPage(EthernetClient client);
void svgHeader(EthernetClient client);
void svgFooter(EthernetClient client);
void polyLineBegin(EthernetClient client);
void polyLineEnd(EthernetClient client);
void pageWrite(EthernetClient client);
void listenClient(EthernetClient client);
void background(EthernetClient client); //only want to do this one once, maybe I can run an Iframe or something
void sensors();//pots
/**Pot Vars**/ 
int potX = A0;    
int potY = A1;    
 
int sensorValX = 0;  
int sensorValY = 0;  
 
int oldX = 0;
int oldY = 0;

/**Rotor Vars**/
// Rotary Encoder Input efintions
#define CLKA 2
#define DTA 3
#define SWA 4

#define CLKB 5
#define DTB 6
#define SWB 7

int Acounter = 0;
int Bcounter = 0;

int AcurrentStateCLK;
int BcurrentStateCLK;

int AlastStateCLK;
int BlastStateCLK;

String AcurrentDir ="";  //just easier than cleverly combining this string for seprate IO
String BcurrentDir ="";

unsigned long AlastButtonPress = 0;
unsigned long BlastButtonPress = 0;
//

   
void setup() {

  delay(5000);  //you want this delay. reason: tl;dr
  /** Set encoder pins as inputs **/
  pinMode(CLKA,INPUT);
  pinMode(DTA,INPUT);
  pinMode(SWA, INPUT_PULLUP);
  pinMode(CLKB,INPUT);
  pinMode(DTB,INPUT);
  pinMode(SWB, INPUT_PULLUP);

  // Read the initial state of CLK
  AlastStateCLK = digitalRead(CLKA);
  BlastStateCLK = digitalRead(CLKB);
  /***End Rotary Encoder vars****/
  
  Serial.begin(9600); 

  Ethernet.begin(mac, ip);

if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("Ethernet cable is not connected.");
  }

  // start the server
  server.begin();
  delay(100);
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
  Serial.print("Initializing SD card...");

  if (!SD.begin(BUILTIN_SDCARD)) {
    Serial.println("SD Card init failed!");
    while (1); 
    }
/****SD CARD and Ethernet intialized*****/  
//if data file (on SD Card) Doesn't exist, create it. 
//if data file (on SD Card) does exist errase it and create a new one
  if (SD.exists("datalog.txt")) {
    Serial.println("datalog.txt exists: \n Removing it.");
    SD.remove("datalog.txt");
  } else {
    Serial.println("datalog.txt doesn't exist.");
         }

  // open a new file and immediately close it:
  Serial.println("Creating datalog.txt...");
  dataFile = SD.open("datalog.txt", FILE_WRITE);
  dataFile.close();

  // Check to see if the file exists:
  if (SD.exists("datalog.txt")) {
    Serial.println("datalog.txt exists.");
  } else {
    Serial.println("datalog.txt wasn't created.");
         }
} //end setup

void loop() {
sensors();
 // listen for incoming clients
  EthernetClient client = server.available();
listenClient(client);
}  //end loop  
void listenClient(EthernetClient client){
  
   if (client) {
    boolean currentLineIsBlank = true;
    while (client.connected()) {
       if (client.available()) {
          char c = client.read();
       // Serial.write(c);   //tells about the client connection
        if (c == '\n' && currentLineIsBlank) {
          pageWrite(client);
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }//end avail 
    } //end conect
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
  } //end client
  }
void sensors(){
    int sensorValX = analogRead(8);
  int sensorValY = analogRead(9);
  String dataString = "";
  int threshhold =3;
  int xVariance = abs(sensorValX-oldX);
  int yVariance = abs(sensorValY-oldY);

if (xVariance >=threshhold && yVariance >= threshhold ||xVariance >=threshhold || yVariance >=threshhold){
//if sensor data is greater than threshold, write data to the sd card, then close the file
    dataString += String(sensorValX);
    dataString += ",";
    dataString += String(sensorValY);

   dataFile = SD.open("datalog.txt", FILE_WRITE);
    // if the file is available, write to it:
   if (dataFile) {
      dataFile.println(dataString);
      dataFile.close();
      
      //clear dataString
      dataString="";
  } else {
    // if the file isn't open, pop up an error:
    Serial.println("error opening datalog.txt");
  }
dataFile.close();
  
}else{
  ;
  }

  }

void pageWrite(EthernetClient client){
          startPage(client);   
          svgHeader(client);           
          polyLineBegin(client);
          polyLineEnd(client);
          svgFooter(client);       
          endPage(client);  
  
  }


void startPage(EthernetClient client){
     // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");  // the connection will be closed after completion of the response
          client.println("Refresh: 5");  // refresh the page automatically every 5 sec
          client.println();
          client.println("");
          client.println(""); 
  }

 void endPage(EthernetClient client){
  client.println("");
  client.println("");
  }

  
void polyLineBegin(EthernetClient client){
  client.print("");  //a dirty hack to avoid dealing with 1 last trailing comma.
  }
 
void svgHeader(EthernetClient client){
client.println(""); 
  } 
  
void svgFooter(EthernetClient client){
client.println ("");
  }

void background(EthernetClient client){
//  client.println();

client.println(""
""
""
""
""
""
"");
  }  

 void rotorA(EthernetClient client){
 // Read the current state of CLK
  AcurrentStateCLK = digitalRead(CLKA);

  // If last and current state of CLK are different, then pulse occurred
  // React to only 1 state change to avoid double count
  if (AcurrentStateCLK != AlastStateCLK  && AcurrentStateCLK == 1){

    // If the DT state is different than the CLK state then
    // the encoder is rotating CCW so decrement
    if (digitalRead(DTA) != AcurrentStateCLK) {
      Acounter --;
      AcurrentDir ="CCW";
    } else {
      // Encoder is rotating CW so increment
      Acounter ++;
      AcurrentDir ="CW";
    }
    Serial.print("A Direction: ");
    Serial.print(AcurrentDir);
    Serial.print(" | Counter: ");
    Serial.println(Acounter);
  }
  int AbtnState = digitalRead(SWA);

  //If we detect LOW signal, button is pressed
  if (AbtnState == LOW) {
    //if 50ms have passed since last LOW pulse, it means that the
    //button has been pressed, released and pressed again
    if (millis() - AlastButtonPress > 500) {
      Serial.println("Button A pressed!");
    }
  AlastButtonPress = millis();
  }
  // Remember last CLK state
  AlastStateCLK = AcurrentStateCLK;
//endA


  }
void rotorB(EthernetClient client){
  
  BcurrentStateCLK = digitalRead(CLKB);
  // If last and current state of CLK are different, then pulse occurred
  // React to only 1 state change to avoid double count
  if (BcurrentStateCLK != BlastStateCLK  && BcurrentStateCLK == 1){

    // If the DT state is different than the CLK state then
    // the encoder is rotating CCW so decrement
    if (digitalRead(DTB) != BcurrentStateCLK) {
      Bcounter --;
      BcurrentDir ="CCW";
    } else {
      // Encoder is rotating CW so increment
      Bcounter ++;
      BcurrentDir ="CW";
    }

    Serial.print("B Direction: ");
    Serial.print(BcurrentDir);
    Serial.print(" | Counter: ");
    Serial.println(Bcounter);
  }  
  BlastStateCLK = BcurrentStateCLK;
  // Read the button state
  int BbtnState = digitalRead(SWB);
    if (BbtnState == LOW) {
    //if 50ms have passed since last LOW pulse, it means that the
    //button has been pressed, released and pressed again
    if (millis() - BlastButtonPress > 50) {
      Serial.println("Button B pressed!");
    }
  
 // Remember last button press event
    BlastButtonPress = millis();
  }
//endB
}
void rotorSense(EthernetClient client){
  rotorA(client);
  rotorB(client);
  }