 #define MIN_BAT_VOLTS       1100          // value of battery voltage scaled by 100 so this is 11.00 volts          
 #define MAX_BAT_VOLTS       1410          // value of battery voltage scaled by 100 so this is 14.10 volts  
 #define HIGH_BAT_VOLTS      1300          // value of battery voltage scaled by 100 so this is 13.00 volts  
+#define SOL_BAT_VOLT_DIFF   200           // For modified MPPT to start, solar voltage must be this much higher than battery side
 #define OFF_NUM             9             // number of iterations of off charger state
   
 #define PIN_LED             13            // LED connected to digital pin 13

//  power_status.bat_volts = BAT_VOLTS_SCALE * read_adc(ADC_BAT_VOLTS_CHAN); // ((read_adc(ADC_BAT_VOLTS_CHAN) * BAT_VOLTS_SCALE) + 5) / 10;   //input of battery volts result scaled by 100
//  power_status.sol_watts = (int)((((long)power_status.sol_amps * (long)power_status.sol_volts) + 50) / 100);    //calculations of solar watts scaled by 10000 divide by 100 to get scaled by 100                 
}
//------------------------------------------------------------------------------------------------------
// 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.

}

//------------------------------------------------------------------------------------------------------
// 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.
//  Off State - This is state that the device enters when solar watts < MIN_SOL_WATTS. The device 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_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 {                                   // this is where we do the Peak Power Tracking 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_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 (sol_volts > (bat_volts + SOL_BAT_VOLT_DIFF)) {
        pwm = PWM_START;                        // if we have solar volts more than SOL_BAT_VOLT_DIFF volts higher than battery
        power_status.mode = MODE_MPPT_ON;       // start pumping the power
        set_pwm_duty(pwm);                      
        TURN_ON_MOSFETS; 
      }
      break;
    default:
      TURN_OFF_MOSFETS;                         //else stay in the off state
      break;
  }
}

//------------------------------------------------------------------------------------------------------
// PWM Adjustments for constant voltage and constant power modes.
// If "target_difference" > 0, it increases PWM
// If "target_difference" < 0, it decreases PWM
