 //------------------------------------------------------------------------------------------------------
 
 void MPPT_state_machine(void) {
-  
-  static int old_sol_watts = 0;
-  static int off_count = OFF_NUM;
-  static int delta = PWM_INC;                   // variable used to modify pwm duty cycle for the ppt algorithm
-
-  int pwm = power_status.pwm_duty;
+
+  // MPPT Skip: only run every MPPT_SKIP_COUNT
+  static unsigned int mppt_skip_counter = 0;
+  mppt_skip_counter++;
+  if (mppt_skip_counter < MPPT_SKIP_COUNT) return;
+  mppt_skip_counter = 0;
+
+  static int off_count = MPPT_OFF_COUNT;       // Off counter
+
   int sol_watts = power_status.sol_watts;
   int bat_volts = power_status.bat_volts;
   int sol_volts = power_status.sol_volts;

}		

//------------------------------------------------------------------------------------------------------
// This routine is the charger state machine. It has four states on, off, bulk and float.
// It's called once each time through the main loop to see what state the charger should be in.
// The battery charger can be in one of the following four states:
// 
//  On State - this is charger state for MIN_SOL_WATTS < solar watts < LOW_SOL_WATTS. This state is probably
//      happening at dawn and dusk when the solar watts input is too low for the bulk charging state but not
//      low enough to go into the off state. In this state we just set the pwm = 100% to get the most of low
//      amount of power available.
//  Bulk State - this is charger state for solar watts > MIN_SOL_WATTS. This is where we do the bulk of the battery
//      charging and where we run the Peak Power Tracking alogorithm. In this state we try and run the maximum amount
//      of current that the solar panels are generating into the battery.
//  Float State - As the battery charges it's voltage rises. When it gets to the MAX_BAT_VOLTS we are done with the 
//      bulk battery charging and enter the battery float state. In this state we try and keep the battery voltage
//      at MAX_BAT_VOLTS by adjusting the pwm value. If we get to pwm = 100% it means we can't keep the battery 
//      voltage at MAX_BAT_VOLTS which probably means the battery is being drawn down by some load so we need to back
//      into the bulk charging mode.
//  Off State - This is state that the charger enters when solar watts < MIN_SOL_WATTS. The charger goes into this
//      state when it gets dark and there is no more power being generated by the solar panels. The MOSFETs are turned
//      off in this state so that power from the battery doesn't leak back into the solar panel. When the charger off
//      state is first entered all it does is decrement off_count for OFF_NUM times. This is done because if the battery
//      is disconnected (or battery fuse is blown) it takes some time before the battery voltage changes enough so we can tell
//      that the battery is no longer connected. This off_count gives some time for battery voltage to change so we can
//      tell this.
//------------------------------------------------------------------------------------------------------

void MPPT_original_state_machine(void) {
  
  static int old_sol_watts = 0;
  static int off_count = OFF_NUM;
  static int delta = PWM_INC;                   // variable used to modify pwm duty cycle for the ppt algorithm

  int pwm = power_status.pwm_duty;
  int sol_watts = power_status.sol_watts;
  int bat_volts = power_status.bat_volts;
  int sol_volts = power_status.sol_volts;
  
  switch (power_status.mode) {
    case MODE_MPPT_ON:                                        
      if (sol_watts < MIN_SOL_WATTS) {          //if watts input from the solar panel is less than
        power_status.mode = MODE_MPPT_OFF;      //the minimum solar watts then it is getting dark so
        off_count = OFF_NUM;                    //go to the charger off state
        TURN_OFF_MOSFETS; 
      }
      else if (bat_volts > MAX_BAT_VOLTS) {       //else if the battery voltage has gotten above the float
        power_status.mode = MODE_MPPT_BAT_FLOAT;  //battery float voltage go to the charger battery float state
      }
      else if (sol_watts < LOW_SOL_WATTS) {     //else if the solar input watts is less than low solar watts
        pwm = PWM_MAX;                          //it means there is not much power being generated by the solar panel
        set_pwm_duty(pwm);			                //so we just set the pwm = 100% so we can get as much of this power as possible
      }                                         //and stay in the charger on state
      else {                                          
        pwm = ((bat_volts * 10) / (sol_volts / 10)) + 5;  //else if we are making more power than low solar watts figure out what the pwm
        power_status.mode = MODE_MPPT_BULK;               //value should be and change the charger to bulk state 
      }
      break;
    case MODE_MPPT_BULK:
      if (sol_watts < MIN_SOL_WATTS) {         //if watts input from the solar panel is less than
        power_status.mode = MODE_MPPT_OFF;     //the minimum solar watts then it is getting dark so
        off_count = OFF_NUM;                   //go to the charger off state
        TURN_OFF_MOSFETS; 
      }
      else if (bat_volts > MAX_BAT_VOLTS) {      //else if the battery voltage has gotten above the float
        power_status.mode = MODE_MPPT_BAT_FLOAT; //battery float voltage go to the charger battery float state
      }
      else if (sol_watts < LOW_SOL_WATTS) {    //else if the solar input watts is less than low solar watts
        power_status.mode = MODE_MPPT_ON;      //it means there is not much power being generated by the solar panel
        TURN_ON_MOSFETS;                       //so go to charger on state
      }
      else {                                   // this is where we do the Peak Power Tracking ro Maximum Power Point algorithm
        if (old_sol_watts >= sol_watts) {      // if previous watts are greater change the value of
          delta = -delta;			                 // delta to make pwm increase or decrease to maximize watts
        }
        pwm += delta;                          // add delta to change PWM duty cycle for PPT algorythm 
        old_sol_watts = sol_watts;             // load old_watts with current watts value for next time
        set_pwm_duty(pwm);			               // set pwm duty cycle to pwm value
      }
      break;
    case MODE_MPPT_BAT_FLOAT:
      if (sol_watts < MIN_SOL_WATTS) {         //if watts input from the solar panel is less than
        power_status.mode = MODE_MPPT_OFF;     //the minimum solar watts then it is getting dark so
        off_count = OFF_NUM;                   //go to the charger off state
        set_pwm_duty(pwm);					
        TURN_OFF_MOSFETS; 
      }
      else if (bat_volts > MAX_BAT_VOLTS) {    //since we're in the battery float state if the battery voltage
        pwm -= 1;                              //is above the float voltage back off the pwm to lower it   
        set_pwm_duty(pwm);					
      }
      else if (bat_volts < MAX_BAT_VOLTS) {    //else if the battery voltage is less than the float voltage
        pwm += 1;                              //increment the pwm to get it back up to the float voltage
        set_pwm_duty(pwm);					
        if (pwm >= 100) {                      //if pwm gets up to 100 it means we can't keep the battery at
          power_status.mode = MODE_MPPT_BULK;  //float voltage so jump to charger bulk state to charge the battery
        }
      }
      break;
    case MODE_MPPT_OFF:                           //when we jump into the charger off state, off_count is set with OFF_NUM
      if (off_count > 0) {                        //this means that we run through the off state OFF_NUM of times with out doing
        off_count--;                              //anything, this is to allow the battery voltage to settle down to see if the  
      }                                           //battery has been disconnected
      else if ((bat_volts > HIGH_BAT_VOLTS) && (bat_volts < MAX_BAT_VOLTS) && (sol_volts > bat_volts)) {
        power_status.mode = MODE_MPPT_BAT_FLOAT;  //if battery voltage is still high and solar volts are high
        set_pwm_duty(pwm);		                    //change charger state to battery float			
        TURN_ON_MOSFETS; 
      }
      else if ((bat_volts > MIN_BAT_VOLTS) && (bat_volts < MAX_BAT_VOLTS) && (sol_volts > bat_volts)) {
        pwm = PWM_START;                        // if battery volts aren't quite so high but we have solar volts
        set_pwm_duty(pwm);			                // greater than battery volts showing it is day light then	
        power_status.mode = MODE_MPPT_ON;       // change charger state to on so we start charging
        TURN_ON_MOSFETS; 
      }
      break;
    default:
      TURN_OFF_MOSFETS;                         //else stay in the off state
      break;
  }
}

//------------------------------------------------------------------------------------------------------
// Modified MPPT code. The code for handling battery or low power is removed
// 
//  On State - this is charger state for solar watts > MIN_SOL_WATTS. This is where we run the 
//      Peak Power Tracking alogorithm. In this state we try and run the maximum amount
//      of current that the solar panels are generating into the other side.

#define BAT_VOLTS_SCALE     2.7           // the scaling value for raw adc reading to get battery volts scaled by 100 [((10+2.2)/2.2)*(5/1023)*100]

#define ONE_SECOND          50000         // count for number of interrupt in 1 second on interrupt period of 20us

#define PIN_LED             13            // LED connected to digital pin 13
#define BUFF_MAX            16            // Maximum buffer size for receiving from serial

