Close

Improvement in LCD Display Function

A project log for ARDUINO MPPT SOLAR CHARGE CONTROLLER

An Arduino based Solar MPPT charge controller.

Debasish DuttaDebasish Dutta 08/11/2015 at 15:571 Comment

The LCD display functionality is improved.The above video shows the simulation to test the modified software.

These are the improvements

1. Dynamic battery status in battery icon.Earlier it was always showing about half full .Now it changes according to the battery SOC, just like in cell phone.

2. Removing the long if else statement for displaying the battery SOC. Now used a math function to do the job.

3. Adding a spinner icon to show the charger is running.It stops when program stuck up.

Code before Modification :

void lcd_display()
{
  back_light_pin_State = digitalRead(BACK_LIGHT_PIN);
  if (back_light_pin_State == HIGH)
  {
    time = millis();                        // If any of the buttons are pressed, save the time in millis to "time"
  }
 
 lcd.setCursor(0, 0);
 lcd.print("SOL");
 lcd.setCursor(4, 0);
 lcd.write(1);
 lcd.setCursor(0, 1);
 lcd.print(sol_volts);
 lcd.print("V"); 
 lcd.setCursor(0, 2);
 lcd.print(sol_amps);
 lcd.print("A");  
 lcd.setCursor(0, 3);
 lcd.print(sol_watts);
 lcd.print("W "); 
 lcd.setCursor(8, 0);
 lcd.print("BAT");
 lcd.setCursor(12, 0);
 lcd.write(2);
 lcd.setCursor(8, 1);
 lcd.print(bat_volts);
 lcd.setCursor(8,2);
 
 if (charger_state == on) 
 lcd.print("on");
 else if (charger_state == off)
 lcd.print("off");
 else if (charger_state == bulk)
 lcd.print("bulk");
 else if (charger_state == bat_float)
 lcd.print("float");

 
 //-----------------------------------------------------------
 //--------------------Battery State Of Charge ---------------
 //-----------------------------------------------------------
 lcd.setCursor(8,3);
 if ( bat_volts >= 12.7)
 lcd.print( "100%");
 else if (bat_volts >= 12.5 && bat_volts < 12.7)
 lcd.print( "90%");
 else if (bat_volts >= 12.42 && bat_volts < 12.5)
 lcd.print( "80%");
 else if (bat_volts >= 12.32 && bat_volts < 12.42)
 lcd.print( "70%");
 else if (bat_volts >= 12.2 && bat_volts < 12.32)
 lcd.print( "60%");
 else if (bat_volts >= 12.06 && bat_volts < 12.2)
 lcd.print( "50%");
 else if (bat_volts >= 11.90 && bat_volts < 12.06)
 lcd.print( "40%");
 else if (bat_volts >= 11.75 && bat_volts < 11.90)
 lcd.print( "30%");
 else if (bat_volts >= 11.58 && bat_volts < 11.75)
 lcd.print( "20%");
 else if (bat_volts >= 11.31 && bat_volts < 11.58)
 lcd.print( "10%");
 else if (bat_volts < 11.3)
 lcd.print( "0%");
 
//--------------------------------------------------------------------- 
//------------------Duty Cycle-----------------------------------------
//---------------------------------------------------------------------
 lcd.setCursor(15,0);
 lcd.print("PWM");
 lcd.setCursor(19,0);
 lcd.write(3);
 lcd.setCursor(15,1);
 lcd.print(pwm); 
 lcd.print("%");
 //----------------------------------------------------------------------
 //------------------------Load Status-----------------------------------
 //----------------------------------------------------------------------
 lcd.setCursor(15,2);
 lcd.print("Load");
 lcd.setCursor(15,3);
 if (load_status == 1)
 {
    lcd.print("On");
 }
 else
 {
   lcd.print("Off");
 }
 backLight_timer();                      // call the backlight timer function in every loop 
}

void backLight_timer(){
  if((millis() - time) <= 15000) // if it's been less than the 15 secs, turn the backlight on
      lcd.backlight();           // finish with backlight on  
  else 
      lcd.noBacklight();         // if it's been more than 15 secs, turn the backlight off
}

Code After Modification :

void lcd_display()
{
  static bool current_backlight_state = -1;
  back_light_pin_State = digitalRead(BACK_LIGHT_PIN);
  if (current_backlight_state != back_light_pin_State) {
    current_backlight_state = back_light_pin_State;
    if (back_light_pin_State == HIGH)
      lcd.backlight();// finish with backlight on
    else
      lcd.noBacklight();
  }

  if (back_light_pin_State == HIGH)
  {
    time = millis();                        // If any of the buttons are pressed, save the time in millis to "time"
  }
 
 lcd.setCursor(0, 1);
 lcd.print(sol_volts);
 lcd.print("V ");
 lcd.setCursor(0, 2);
 lcd.print(sol_amps);
 lcd.print("A");  
 lcd.setCursor(0, 3);
 lcd.print(sol_watts);
 lcd.print("W "); 
 lcd.setCursor(8, 1);
 lcd.print(bat_volts);
 lcd.setCursor(8,2);

 if (charger_state == on) 
 lcd.print("on   ");
 else if (charger_state == off)
 lcd.print("off  ");
 else if (charger_state == bulk)
 lcd.print("bulk ");
 else if (charger_state == bat_float)
 {
 lcd.print("     ");
 lcd.setCursor(8,2);
 lcd.print("float");
 }
 
 //-----------------------------------------------------------
 //--------------------Battery State Of Charge ---------------
 //-----------------------------------------------------------
 int pct = 100.0*(bat_volts - 11.3)/(12.7 - 11.3);
 if (pct < 0)
     pct = 0;
 else if (pct > 100)
     pct = 100;

 lcd.setCursor(12,0);
 lcd.print((char)(pct*5/100));

 lcd.setCursor(8,3);
 pct = pct - (pct%10);
 lcd.print(pct);
 lcd.print("%  ");
 
//--------------------------------------------------------------------- 
//------------------Duty Cycle-----------------------------------------
//---------------------------------------------------------------------
 lcd.setCursor(15,0);
 lcd.print("PWM");
 lcd.setCursor(19,0);
 lcd.write(PWM_ICON);
 lcd.setCursor(15,1);
 lcd.print("   ");
 lcd.setCursor(15,1);
 if( charger_state == off)
 lcd.print(0);
 else
 lcd.print(pwm); 
 lcd.print("% ");
 //----------------------------------------------------------------------
 //------------------------Load Status-----------------------------------
 //----------------------------------------------------------------------
 lcd.setCursor(15,2);
 lcd.print("Load");
 lcd.setCursor(15,3);
 if (load_status)
 {
    lcd.print("On  ");
 }
 else
 {
   lcd.print("Off ");
 } 
 spinner();
 backLight_timer();                      // call the backlight timer function in every loop 
}

void backLight_timer(){
  if((millis() - time) <= 15000)         // if it's been less than the 15 secs, turn the backlight on
      lcd.backlight();                   // finish with backlight on  
  else 
      lcd.noBacklight();                 // if it's been more than 15 secs, turn the backlight off
}
void spinner(void) {
  static int cspinner;
  static char spinner_chars[] = { '*','*', '*', ' ', ' '};
  cspinner++;
  lcd.print(spinner_chars[cspinner%sizeof(spinner_chars)]);
}

Discussions

vitalijbojko wrote 03/19/2017 at 23:38 point

Excellent scheme and works in a beautiful way to change the code so that it works on 12 -24 volts. Thank you

  Are you sure? yes | no