 }
 
 /* MOSFET STATES */
-void state0(double pwmfreq)
+void state0(double pwmfreq) //P2N3
 {
   PORT->Group[PORTA].OUTSET.reg = (1<<20);
   PORT->Group[PORTA].OUTSET.reg = (1<<19);
-//  PORT->Group[PORTA].OUTSET.reg = 110000000000000000000; //P2N3
-}
-
-void state1(double pwmfreq)
+}
+
+void state1(double pwmfreq) //P1N3
 {
   PORT->Group[PORTA].OUTSET.reg = (1<<15);
   PORT->Group[PORTA].OUTSET.reg = (1<<19);
-//  PORT->Group[PORTA].OUTSET.reg = 10001000000000000000; //P1N3
-}
-
-void state2(double pwmfreq)
+}
+
+void state2(double pwmfreq) //P1N2
 {
   PORT->Group[PORTA].OUTSET.reg = (1<<15);
   PORT->Group[PORTA].OUTSET.reg = (1<<16);
-//  PORT->Group[PORTA].OUTSET.reg = 11000000000000000; //P1N2
-}
-
-void state3(double pwmfreq)
+}
+
+void state3(double pwmfreq) //P3N2
 {
   PORT->Group[PORTA].OUTSET.reg = (1<<7);
-  PORT->Group[PORTA].OUTSET.reg = (1<<16);
-//  PORT->Group[PORTA].OUTSET.reg = 10000000010000000; //P3N2
-}
-
-void state4(double pwmfreq)
+  PORT->Group[PORTA].OUTSET.reg = (1<<16); 
+}
+
+void state4(double pwmfreq) //P3N1
 {
   PORT->Group[PORTA].OUTSET.reg = (1<<7);
   PORT->Group[PORTA].OUTSET.reg = (1<<18);
-//  PORT->Group[PORTA].OUTSET.reg = 1000000000010000000; //P3N1
-}
-
-void state5(double pwmfreq)
+}
+
+void state5(double pwmfreq) //P2N1
 {
   PORT->Group[PORTA].OUTSET.reg = (1<<20);
   PORT->Group[PORTA].OUTSET.reg = (1<<18);
-//  PORT->Group[PORTA].OUTSET.reg = 101000000000000000000; //P2N1
+}
+
+void stateGND() //N1N2N3
+{
+  PORT->Group[PORTA].OUTSET.reg = (1<<16);
+  PORT->Group[PORTA].OUTSET.reg = (1<<18);
+  PORT->Group[PORTA].OUTSET.reg = (1<<19);
 }
 

//    analogWrite(motorN1, pwmfreq);
//}

int readCurrent() //read current for active mosfet
{
  int coilCurrent = 0;
  if ((state == 0) || (state == 1))
    coilCurrent = analogRead(current0);
  if ((state == 2) || (state == 3))

  
  else //otherwise if same coil being used, check for overcurrent. If over, disable. If under, update pwm
  {
    uint16_t coilCurrent = readCurrent(); //read current is very slow because analog read sucks
    if ((coilCurrent > currentThresh) && (!coilGndFlag)) //if current over thresh and previously below, ground all coils to eliminate current flow
    {
      disableMotor();
      stateGND();
      coilGndFlag = 1;
    }
    else if (coilGndFlag) //if curret below thresh and was previously above, turn back on and reset flag
    {
      setPWM(255);
      coilGndFlag = 0;
    }
  }
}

void disableMotor() //turns off all mosfets using registers
{
//  PORT->Group[PORTA].OUTCLR.reg = 111011000000010000000; //not currently working but should be faster
  PORT->Group[PORTA].OUTCLR.reg = (1<<15);
  PORT->Group[PORTA].OUTCLR.reg = (1<<20);
  PORT->Group[PORTA].OUTCLR.reg = (1<<7);
