   pinMode(yZeropin, INPUT);                  //Y SWITCH
   pinMode(zZeropin, INPUT);                  //Z SWITCH
 
-
-  stepper1.setMaxSpeed(maxmotorSpeed);
-  stepper2.setMaxSpeed(maxmotorSpeed);                         
-  stepper4.setMaxSpeed(maxmotorSpeed);                         
-  stepper5.setMaxSpeed(maxmotorSpeed);      
-
-  stepper1.setSpeed(motorSpeed);
-  stepper2.setSpeed(motorSpeed);
-  stepper4.setSpeed(motorSpeed);
-  stepper5.setSpeed(motorSpeed);
-  stepper1.setAcceleration(motorAccel);  
-  stepper2.setAcceleration(motorAccel);  
-  stepper4.setAcceleration(motorAccel);  
-  stepper5.setAcceleration(motorAccel);    
-
-  zeroaxis();                                //ZERO AXIS
-  
-  stepper1.setSpeed(motorSpeed);
-  stepper2.setSpeed(motorSpeed);
-  stepper4.setSpeed(motorSpeed);
-  stepper5.setSpeed(motorSpeed);
-  stepper1.setAcceleration(motorAccel);  
-  stepper2.setAcceleration(motorAccel);  
-  stepper4.setAcceleration(motorAccel);  
-  stepper5.setAcceleration(motorAccel);    
-  
-  //testaxis();                                //(optional) RUNS TO EXTREMES AND STOPS AT (X0,Y0,Z0,E0)
+  //Thermo
+  pinMode(thermo, INPUT);              //Analogue temperature measure
+  pinMode(thermoswitch, OUTPUT);       //Heater mosfet switch
+  digitalWrite(thermoswitch, LOW);     //Defaults to OFF/LOW
+  
+  pinMode(motorpinXstep, OUTPUT);
+  pinMode(motorpinXdir, OUTPUT);  
+  pinMode(motorpinYstep, OUTPUT);
+  pinMode(motorpinYdir, OUTPUT);    
+  pinMode(motorpinZstep, OUTPUT);
+  pinMode(motorpinZdir, OUTPUT);    
+  pinMode(motorpinEstep, OUTPUT);
+  pinMode(motorpinEdir, OUTPUT);    
+
+  thermistor();
+  zeroaxis();                                
+  thermistor();
+  
+  
   Serial.print("{ \"log\" : \"Success\"}");
   Serial.print("{ \"version\" : \""+version+"\"}");  
-  //rapid(75000,100000, 10000, 0);
-
-  line(tablexdistance/2, tableydistance/2, 0, 0);  //CENTER
-
+  pinMode(13, OUTPUT);
+  Serial.print("{ \"status\" : \"waiting\"}"); 
 }
 
 aJsonStream serial_stream(&Serial);
 
+double lastcmd = 0;
+double tempcounter = 0;
+
 void loop() {
-  //line(10000,10000,-20000,0);
-  //line(150000,127052,-30000,20000);
-  //gotoxyze(0,0,0,0);
-  //testsurface();  //(optional) this runs around on the table so you can test height consistancy
-  
-  //gotoxyze(150000,100000,0,0);  
-  //circle(50000,50000,0,0,20000);
-  //
-  //
-      if (serial_stream.available()) {
-        /* First, skip any accidental whitespace like newlines. */
-        serial_stream.skip();
-      }
-    
-      if (serial_stream.available()) {
-        /* Something real on input, let's take a look. */
+    double lastloop = millis();
+    while (serial_stream.available()) {
         aJsonObject *msg = aJson.parse(&serial_stream);
         processMessage(msg);
-        aJson.deleteItem(msg);        
-      }   
-  Serial.print("{ \"status\" : \"waiting\"}");  
+        lastcmd = millis();
+        aJson.deleteItem(msg);                
+
+        tempcounter++;
+        if (tempcounter > 5) {
+            thermistor();
+            tempcounter = 0;
+        }
+
+        Serial.print("{ \"status\" : \"waiting\"}"); 
+    } 
+    
+    
+    if (!serial_stream.available()) {
+      
+    }    
+
+    
+    if (lastloop > lastcmd+1000) {
+        pinMode(13, HIGH);
+        delay(250);        
+        pinMode(13, LOW);                
+        delay(250);                
+        pinMode(13, HIGH);
+        delay(250);        
+        pinMode(13, LOW);                
+        delay(250);                
+        Serial.print("{ \"status\" : \"waiting\"}"); 
+        pinMode(13, HIGH);
+        delay(250);        
+        pinMode(13, LOW);                
+        delay(250);                
+        pinMode(13, HIGH);
+        delay(250);        
+        pinMode(13, LOW);                
+        delay(250);        
+    }
+
+}
+//=======================================================================================
+
+void thermistor() {
+   Serial.print("{ \"thermistor\" : \"");
+   Serial.print(analogRead(thermo));
+   Serial.print("\"}");   
 }
 
 //=======================================================================================

 *  =======================================================================================
 */

//physical movement scale (bigger == more);
double tablexsteps    = 13000;     
double tableysteps    = 13000;
double tablezsteps    = 100000;    //257142 (100mm) from top (STEPS FROM TABLE SURFACE TO TOP) 0,0,0 is FAR LEFT BOTTOM
double tableesteps  = -3600;

//measured (1000 = 1mm)
double tablexdistance = 250000;    //in microns (158mm )
double tableydistance = 250000;    //in microns (200mm)
double tablezdistance = 96000;    //in microns (96mm)
double tableedistance = 1000;    //in microns (96mm)

double maxmotorSpeed = 12600;
double motorSpeed = 12600;                                           //default 9600
double motorAccel = 160000; //steps/second/second to accelerate      //default 80000

// Motor pins
int motorpinXstep = 7;
int motorpinXdir  = 6;
int motorpinYstep = 9;
int motorpinYdir  = 8;
int motorpinZstep = 13;
int motorpinZdir  = 12;
int motorpinEstep = 11;
int motorpinEdir  = 10;

// Motor Zero switch pins
int xZeropin = 5;        //INPUT pin
int yZeropin = 4;        //INPUT pin
int zZeropin = 3;        //INPUT pin

int xZeroNC  =  1;        //Normally closed/open
int yZeroNC  =  1;          //Normally closed/open
int zZeroNC  =  0;          //Normally closed/open

// =========================================================================================

//latest position in microns (1000 = 1mm)
double lastx = tablexdistance/2;
double lasty = tableydistance/2;
double lastz = 0;
double laste = 0;




// =========================================================================================

AccelStepper stepper5(1, motorpinXstep, motorpinXdir);          //CROSSBEAM      X AXIS
AccelStepper stepper4(1, motorpinYstep, motorpinYdir);          //TABLE IN/OUT   Y AXIS
AccelStepper stepper1(1, motorpinZstep, motorpinZdir);          //UP DOWN        Z AXIS
AccelStepper stepper2(1, motorpinEstep, motorpinEdir);          //EXSTRUDER FEED E AXIS

void setup() {
  Serial.begin(115200);
  Serial.print("{ \"log\" : \"Connecting...\"}");
  //ZERO SWITCHES

      Runs each motor towards the zero switch and resets its position. Must be run at start.
  */

    ////////////////////////////////////////////////////////ZERO Z - UPDOWN
    Serial.print("{ \"log\" : \"Attempting to zero Z axis\"}");

    while (digitalRead(zZeropin) != zZeroNC) {             
      stepperZdestPosition = stepperZdestPosition + 16;
      while (stepperZdistanceToGo() != 0) {
        stepperZrun();
      }
    }

    stepperZcurrPosition = tablezsteps + 6400;
 
    //moves a little off switch
    stepperZdestPosition = tablezsteps -30000;        
    while (stepperZdistanceToGo() != 0) {
      stepperZrun();
    }   
    /////////////////////////////////////////////////////////// X LEFT RIGHT
    Serial.print("{ \"log\" : \"Attempting to zero X axis\"}");    
    while (digitalRead(xZeropin) != xZeroNC) {   
      stepperXdestPosition = stepperXdestPosition - 32;
      while (stepperXdistanceToGo() != 0) {
        stepperXrun();
      }
    }
    
    stepperXcurrPosition = -1000;
    //moves a little off switch (0)
    stepperXdestPosition = tablexsteps;
    while (stepperXdistanceToGo() != 0) {
      stepperXrun();
    }
    Serial.print("{ \"log\" : \"X axis zero success\"}");
    ////////////////////////////////////////////////////////////// Y TABLE FRONT BACK
    Serial.print("{ \"log\" : \"Attempting to zero Y axis\"}");   
    while (digitalRead(yZeropin) != yZeroNC) {   
      stepperYdestPosition = stepperYdestPosition + 32;
      while (stepperYdistanceToGo() != 0) {
        stepperYrun();
      }      
    }
    //moves to 0
    stepperYcurrPosition = tableysteps+1000;
    stepperYdestPosition = 0;
    while (stepperYdistanceToGo() != 0) {
      stepperYrun();
    }      
    ////////////////////////////////////////////////////////////// E zero
    Serial.print("{ \"log\" : \"Attempting to test E axis\"}");   
    stepperEdestPosition = 6400;  //1 rotation
    while (stepperEdistanceToGo() != 0) {
        stepperErun();
      }      
    //moves to 0
    stepperEcurrPosition = 0;
    stepperEdestPosition = 0;
    Serial.print("{ \"log\" : \"Extruder stepper axis zero success\"}");  
    
}
// END ZEROAXIS()
// ==============================================================================================================================================================================
// START LINE()
void line(double x, double y, double z, double e) {

  //DISTANCESTEPS
  
   double deltax = lastx - x;
   double deltay = lasty - y;
   double deltaz = lastz - z;
   double deltae = laste - e;   

   double distance = pow(deltax,2) + pow(deltay,2) + pow(deltaz, 2) + pow(deltae, 2);
   distance = sqrt(distance);
  
  ///
  
//   double steps = 25;
    double steps = round(distance)*5;
   
        //calculate new step positions from micron position
   double nx = tablexsteps*(x/tablexdistance);
   double ny = tableysteps*(y/tableydistance);  
   double nz = tablezsteps*(z/tablezdistance);      
   double ne = tableesteps*(e/tableedistance);                

   double cx = float(stepperXcurrPosition);
   double cy = float(stepperYcurrPosition);   
   double cz = float(stepperZcurrPosition);      
   double ce = float(stepperEcurrPosition);         

  
   double dx = ((nx-cx)/steps);
   double dy = ((ny-cy)/steps);   
   double dz = ((nz-cz)/steps);      
   double de = ((ne-ce)/steps);         

   
   //distance
   /*
   double deltax = lastx - x;
   double deltay = lasty - y;
   double deltaz = lastz - z;
   double deltae = laste - e;   
   Serial.print("{ \"deltax\" : \"");
   Serial.print(deltax);
   Serial.print("\", \"deltay\" : \"");   
   Serial.print(deltay);   
   Serial.print("\", \"deltaz\" : \"");   
   Serial.print(deltaz);   
   Serial.print("\", \"deltae\" : \"");   
   Serial.print(deltae);      
   Serial.println("\"}");
   */
   //
  
   lastx = x;
   lasty = y;
   lastz = z;
   laste = e;

   for (int i = 0; i < steps; i++) {   
     double newx = cx + (dx*i);
     double newy = cy + (dy*i);     
     double newz = cz + (dz*i);          
     double newe = ce + (de*i);     
      
     stepperXdestPosition = newx;
     stepperYdestPosition = newy;
     stepperZdestPosition = newz;
     stepperEdestPosition = newe;     
     
     
     while ((stepperXdistanceToGo() != 0) || (stepperYdistanceToGo() != 0) || (stepperZdistanceToGo() != 0) || (stepperEdistanceToGo() != 0)) {
       ///GET THESE INTO THE MAIN LOOP
       // NEED TO REFACTOR EVENT LOOP
       stepperXrun();
       stepperYrun();
       stepperZrun();
       stepperErun();
       } 
   }
  
}
// END LINE();
// ==============================================================================================================================================================================
