 }		
 
 //------------------------------------------------------------------------------------------------------
-// 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) {
+// MPPT Algorithm
+//------------------------------------------------------------------------------------------------------
+
+void MPPT_PWM_Adjust(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;
+  static int delta =  PWM_MPPT_INC;         // variable used to modify pwm duty cycle for the ppt algorithm
+
   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
-// 
+
+  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
+  }
+  
+  int pwm = power_status.pwm_duty + delta;  // add delta to change PWM duty cycle for PPT algorithm 
+  
+  if (pwm < PWM_MPPT_MIN) {                 // Keep within MPPT min PWM duty cycle limit
+    pwm = PWM_MPPT_MIN;
+  }
+
+  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
+}
+
+//------------------------------------------------------------------------------------------------------
+// MPPT State Machine.
+//
+//  To adjust for the LPF, the machine will only switch states  
+//
 //  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 LOW_SOL_WATTS       500           // value of solar watts scaled by 100 so this is 5.00 watts
#define MIN_SOL_WATTS       100           // value of solar watts scaled by 100 so this is 1.00 watts
#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
#define BUFF_MAX            16            // Maximum buffer size for receiving from serial


#define ADC_BAT_VOLTS_CHAN  2             // the adc channel to read battery volts

#define PWM_FULL            1023          // the actual value used by the Timer1 routines for 100% pwm duty cycle
#define PWM_MAX             0.9*PWM_FULL  // the max value for pwm duty cyle 0-100.0% (Resolution in 
#define PWM_MIN             0.1*PWM_FULL  // the min value for pwm duty cyle 0-100.0%
#define PWM_START           0.1*PWM_FULL  // the starting value for pwm duty cyle 0-100.0%

#define PWM_MPPT_MIN        0.5*PWM_FULL  // the min value for pwm duty cyle 0-100.0% in MPPT mode
#define PWM_MPPT_START      0.7*PWM_FULL  // the starting value for MPPT mode pwm duty cyle 0-100.0%

#define PWM_MPPT_INC        16             // the value the increment to the pwm value for the ppt algorithm
#define MIN_SOL_WATTS       100           // value of solar watts scaled by 100 so this is 0.50 watts
#define MIN_SOL_VOLTS       1200          // The minimum voltage of the solar panel to keep MPPT on
#define MPPT_SKIP_COUNT     32            // number of iterations to skip updating MPPT state machine
#define MPPT_OFF_COUNT      32            // number of iterations of off charger state

#define AVG_NUM             8             // number of iterations of the adc routine to average the adc readings
#define SOL_AMPS_SCALE      0.5           // the scaling value for raw adc reading to get solar amps scaled by 100 [(1/(0.005*(3.3k/25))*(5/1023)*100]
