MPPT program and LCD issue

Hi guys,

I am working on an MPPT controller. There is a 40W solar panel which will be connected to a DC-DC converter which then powers a 12V LED strip at the moment. The DC-Dc converter is controlled via PWM output from the Arduino. The Arduino also senses voltage and currents to calculate power. These will also be displayed on the 16x2 LCD. I am having a few issues.

Issue 1: Only the second row of the LCD updates every 500ms. The first row never updates.

Issue 2: The MPPT code does not work. The duty cycle does not change based on the power. I believe this is a problem with my programming logic.

I have posted the code below. Could someone please help?

// include the library code:
#include <LiquidCrystal.h> // For the LCD 
#include <PWM.h> // For PWN control

int duty = 180; // dutycycle = 0.706*255
int32_t frequency = 490000; // switching frequency
float Po_old = 0.00;
float Po_new = 0.00;
float inc = 1; // duty cycle increment value

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() 
{
  
  InitTimersSafe();
  bool success = SetPinFrequencySafe(9, frequency);
  if (success) //If success, then light up LED on pin 13
  {
    pinMode (13, OUTPUT);
    digitalWrite(13, HIGH);
  }
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
}

void loop() 
{
  
  pwmWrite(9, duty); // PWM output through pin 9

  // Read values from the sensors
  
  float VinValue = analogRead(A0); 
  float CinValue = analogRead(A1);
  float VoValue = analogRead(A2);
  float CoValue = analogRead(A3);

  float Vin = VinValue*25/1023;
  float Cin = CinValue*5/1023;
  float Vo = VoValue*25/1023;
  float Co = CoValue*5/1023;

  // Calculate powers
  
  float Pin = Vin*Cin;
  float Po_new = Vo*Co;

  // MPPT algorithm
  
  if(Po_new >= Po_old) 
  {
    duty = duty + inc;
    }
  else
  {
    duty = duty - inc;
    }

  Po_old = Po_new;

  // Print values

  lcd.print(Vin);
  lcd.print(" ");
  lcd.print(Cin);
  lcd.print(" ");
  lcd.print(Pin);

 
 // set the cursor to column 0, line 1
 // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  lcd.print(Vo);
  lcd.print(" ");
  lcd.print(Co);
  lcd.print(" ");
  lcd.print(Po_new);
  delay(500);
}

See if this changes things:

  float Vin = VinValue *25.0 / 1023.0;
  float Cin = CinValue *5.0  / 1023.0;
  float Vo = VoValue   *25.0 / 1023.0;
  float Co = CoValue   *5.0  / 1023.0;

The compiler defaults numeric constants to int. Also, while this won't make any difference since the compiler will generate the same code, the comments show alternatives that would likely be used:

  if(Po_new >= Po_old) 
  {
    duty = duty + inc;         // Often you'll see:  duty += inc;
    }
  else
  {
    duty = duty - inc;         // or...   duty -= inc;
    }

They don't make any difference. But thanks for the reply.

the last instruction before printing you sent the lcd was lcd.setCursor(0, 1); if you don't tell it to change lines then it will loop the display. I don't know the default for the 2x16 but im guessing

lcd.begin set cursor at (0,0) then after first print

lcd.print(Vin);
  lcd.print(" ");
  lcd.print(Cin);
  lcd.print(" ");
  lcd.print(Pin);

you set cursor to
lcd.setCursor(0, 1);

so now everything will be written to this line. Once it over runs 16 characters it will either display nothing or loop back onto line 2 and carry on writing over the text

add
lcd.setCursor(0, 0);

before first lcd.print