 int MaxRunTime = 0;//60*60*10;// number of seconds the program is allowed to run be
 int SpndlPWM;
 int newdutycycle = 0;
-//int range = 800; //the number Substeps the Mark/Space ratio can be divided into
-//int Maxdutycycle =  630;
-//int Mindutycycle = 23;
 int dutycycle = 30; // percentage of time clock is high
-int StrtDC = 0; // the calculated Start dutycycle needed to hit the target rpm $
-//int TooHighCnt = 0;
-//int TooLowCnt = 0;
-//int MaxChngLmt = 0;
-int LastRPM = 0;
-int AltRPM = 0;
-int SpeedChange = 0;
-int SuspectRdCnt = 0;
-bool increaseSpeed = true;
+//int StrtDC = 0; // the calculated Start dutycycle needed to hit the target rpm $
+
+//int LastRPM = 0;
+//int AltRPM = 0;
+//int SpeedChange = 0;
+//int SuspectRdCnt = 0;
+//bool increaseSpeed = true;
 bool NewTrgtVal = true; //used to determine if display needs updating
 bool ExitNow = false;
 bool NuInput = true;
-char *intFlgStat;
-int ThrtlLpCnt= 0;
-int Startup = 0; // Locks out pid calcs on startup to give the default throttle setting a chance to take effect before correcting it
-int CalcIntrvl = 0;
+//char *intFlgStat;
+//int ThrtlLpCnt= 0;
+//int Startup = 0; // Locks out pid calcs on startup to give the default throttle setting a chance to take effect before correcting it
+//int CalcIntrvl = 0;
 int BackLight = 6;  // set Display BackGround color to 1=Red; 2=Green; 4=Blue
 int Mode = 7;
 

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

// SpindleTachInterrupt:  called every time an event occurs

void SpindleTachInterrupt(void)
{
   int ThisRPM = 0;
   double pulseperiod = 0;
//   if (Startup>0) // PID Lock out counter
//   {
//     LoopCounter = 1;
//     Startup -= 1;
//     return;
//   }
   if (eventCounter == 0)
     {
      Start = micros();//gettimeofday(&Start, NULL);
     }
   if (eventCounter == 10)
     {
      Stop = micros();//gettimeofday(&Stop, NULL);
     }
   eventCounter +=1;//eventCounter++;
///////////////////////////////////////////////////////////////////////////////
    if (eventCounter < 11)// Let's see if there's enough samples to calc the current RPM
     {
      if (eventCounter < 3)
       {
        return; //No, there isn't enough samples
       }
      else
       {
        // yes there is, but does it seem like a reasonable value 
        Stop = micros(); //gettimeofday(&CalcStrt, NULL);
        //pulseperiod = ((Stop.tv_sec-Start.tv_sec)*1000000ULL+(Stop.tv_usec-Start.tv_usec));
        pulseperiod = (Stop-Start);
        ThisRPM  = (60000000*(eventCounter-1))/pulseperiod;
        if (ThisRPM <1200 || ThisRPM > 4200)
         {
          return; // its too slow, or its going fast enough that we need to go for a full ten  
                  // samples before deciding what to do with it
         }
       }
     }
   if (eventCounter >= 11 || ThisRPM >0 )// There's enough samples to calc the current RPM
     {
      if (eventCounter >= 11)
       {
        pulseperiod = (Stop-Start);
        calcrpm = 600000000/pulseperiod;// calculate rpm based on having measured the period of 10 revolutions
       }
       else
       {
        calcrpm = ThisRPM;
       }

      SpeedChange =  calcrpm -  LastRPM;
      //LastRPM = curspeed;
      LastRPM = calcrpm; 
      eventCounter = 0;
      LoopCounter = 1;
      //return; // temp stop for testing just the Tachometer portion of the code
     
      if (pulseperiod<0 )//|| pulseperiod > 600000
       {
         sprintf (buf, "Pulse Period Err");
         //Serial1.println(buf); 
        return; // disregard this result; it appears to be bogus
       }
      
      SampleTime = MstrSampleTime; 
      SetTunings(Kp, Ki, Kd);
      SpndlPWM = CalcMtrPID(calcrpm);//Max+CalcMtrPID(calcrpm);
      analogWrite(PWMpin, SpndlPWM);
      dutycycle = (int)((SpndlPWM*100)/(Max));//(int)((SpndlPWM*100)/(2*Max));// convert PWM signal to a precentage
     }

   }
// ================================================================
// ===               END INTERRUPT DETECTION ROUTINE            ===
// ================================================================

      }
     else
      {
       strncpy(SgndErStr, " ", 8);
       strcat(SgndErStr, SpdErStr);
      }
