LCD code affecting inverter oscillator code due to LCD delay();

Good day please i need help. i extracted this code out of the main code. The main issue am having is lines 197 to 213 . If i remove the LCD code the PWM which servers as oscillator for the power inverter works fine . With the LCD code the delay(); function for the LCD affects the PWM nt to be unstable.
I have tried previousMillis interval but not working. PLEASE ANY HELP WILL BE APPRECIATED . THANKS

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

//unsigned long previousMillis = 0;
//unsigned long interval = 4000; //10 seconds

// inverter  

int MOSFET1  =         9;           // MOSFET Bank1
int MOSFET2  =        10;           // MOSFET Bank2
int EnablePower =      2;           // HV power on switch
int AC_RELAY    =       4;          //AC RELAY PIN


//ac and dc meter
int  DCInput = A0;    // DC SENSE PIN
int  ACInput = A1;    // AC SENSE PIN
float vout0 = 0.0;
float vout1 = 0.0;
float vin0 = 0.0;
float vin1 = 0.0;
float R1 = 100000.0; // resistance  of  R1 (100K) -see text!
float R2 = 10000.0; //  resistance  of  R2 (10K) - see text!
float R3 = 100000.0; // resistance  of  R3 (100K) -see text!
float R4 = 10000.0; //  resistance  of  R4 (10K) - see text!
int value0 = 0;
int value1 = 0;



void setup(){
  // INVERTER
  pinMode(MOSFET1, OUTPUT);     // MOSFET 1
  pinMode(MOSFET2, OUTPUT);     // MOSFET 2
  pinMode(AC_RELAY, OUTPUT);
  pinMode(EnablePower, INPUT);

  // VOLTMETER
  pinMode(DCInput, INPUT);
  pinMode(ACInput, INPUT);

  lcd.begin(16, 2);

  // Turn off the display:
  lcd.setCursor(0, 0);
  lcd.print("DAVID OLAOLU");
  lcd.setCursor(7, 1);
  lcd.print("DAVID OLAOLU");
  lcd.noDisplay();
  delay(500);
  // Turn on the display:
  lcd.display();
  delay(500);
  lcd.clear();

  // Turn off the display:
  lcd.setCursor(1, 0);
  lcd.print("DAVID OLAOLU");
  lcd.setCursor(6, 1);
  lcd.print("DAVID OLAOLU");
  lcd.noDisplay();
  delay(1000);
  // Turn on the display:
  lcd.display();
  delay(1000);
  lcd.clear();


}

void loop(){


  //$$$$$$$$$$$$$ SENSE  DC $$$$$$$$$$$$$$$ 
  value0 = analogRead(DCInput);
  vout0 = (value0 * 5.0) / 1024.0; // see text
  vin0 = vout0 / (R2/(R1+R2)); 
  if (vin0<0.09) {
    vin0=0.0;//statement to quash undesired reading !
  }
  //$$$$$$$$$$$$$ SENSE  AC $$$$$$$$$$$$$$$ 

  value1 = analogRead(ACInput);
  vout1 = (value1 * 72.0) / 1024.0; // see text
  vin1 = vout1 / (R4/(R3+R4)); 
  if (vin1<0.09) {
    vin1=0.0;//statement to quash undesired reading !
  }



  {
    if ((vin1 >140)||(vin1>240))
    {
      {
        digitalWrite(AC_RELAY, HIGH);  //   
        digitalWrite(MOSFET1, LOW);   
        digitalWrite(MOSFET2, LOW);  
        delay(7);    
        digitalWrite(MOSFET1, LOW);   
        digitalWrite(MOSFET2, LOW);    
        delay(7);

        { 
          if (digitalRead (EnablePower) == HIGH)
          {
            lcd.setCursor(0, 0);
            //lcd.write(6);
            lcd.setCursor(2, 0);
            lcd.print(" STANDBY ON ");
            lcd.setCursor(0, 1);
            lcd.print("MAINS  ON");
            delay(2000);
            lcd.clear(); 

            lcd.setCursor(0, 0);
            //lcd.write(6);
            lcd.setCursor(2, 0);
            lcd.print( vin1);
            lcd.print(" V ");
            lcd.setCursor(0, 1);
            lcd.print("MAINS VOLTAGE");
            delay(2000);
            lcd.clear();  
          } 

          else  
          {
            lcd.setCursor(0, 0);
            //lcd.write(6);
            lcd.setCursor(2, 0);
            lcd.print(" STANDBY OFF ");
            lcd.setCursor(0, 1);
            lcd.print("MAINS  ON");
            delay(2000);
            lcd.clear(); 

            lcd.setCursor(0, 0);
            //lcd.write(6);
            lcd.setCursor(2, 0);
            lcd.print( vin1);
            lcd.print(" V ");
            lcd.setCursor(0, 1);
            lcd.print("MAINS VOLTAGE");
            delay(2000);
            lcd.clear();  

          }
        }
      }
    }


    else
    {
      digitalWrite(AC_RELAY, LOW);     

      if ((vin0 < 8)&& (digitalRead (EnablePower) == HIGH))

      {   
        digitalWrite(MOSFET1, LOW);   
        digitalWrite(MOSFET2, LOW);  
        delay(7);    
        digitalWrite(MOSFET1, LOW);   
        digitalWrite(MOSFET2, LOW);    
        delay(7);
        lcd.setCursor(0, 0);
        lcd.print("LOW BATTERY ");
        lcd.setCursor(0, 1);
        lcd.print("INPUT V= ");
        lcd.print(vin0);
        delay(1000); 
        lcd.clear();
      }
      if ((vin0 > 8)&& (digitalRead (EnablePower) == HIGH))

      {          
        digitalWrite(MOSFET1, HIGH);   
        digitalWrite(MOSFET2, LOW);  
        delay(7);    
        digitalWrite(MOSFET1, LOW);   
        digitalWrite(MOSFET2, HIGH);    
        delay(7);
        lcd.setCursor(0, 0);
        lcd.print("INVERTER ON ");
        lcd.setCursor(0, 1);
        lcd.print("INPUT V= ");
        lcd.print(vin0);
        delay(1000); 
        lcd.clear();
      } 

      if   (digitalRead (EnablePower) == LOW)
      {
        digitalWrite(MOSFET1, LOW);   
        digitalWrite(MOSFET2, LOW);  
        delay(7);    
        digitalWrite(MOSFET1, LOW);   
        digitalWrite(MOSFET2, LOW);    
        delay(7);
        lcd.setCursor(2, 0);
        lcd.print(" INVERTER OFF  ");
        lcd.setCursor(1, 1);
        lcd.print("INPUT V= ");
        lcd.print(vin0);
        delay (1000);
        lcd.clear();
      }
    }
  }
}

Thanks

With the LCD code the delay(); function for the LCD affects the PWM nt to be unstable.

Why do you need delay()? Add a switch so the user can cycle through the options with regards to what he wants to see. Stop clearing the screen after showing a value.

Thanks Puals for respond. i i have 3 messages to display without the clear LCD the last message will appear in the new message. please do you meas switch case? Thanks. how can i rewrite the code without using delay(); for the LCD?

this compiles but probably has lots of problems as its cut and paste code. It should show you one way to deal with the screens

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

// inverter

int MOSFET1  =         9;           // MOSFET Bank1
int MOSFET2  =        10;           // MOSFET Bank2
int EnablePower =      2;           // HV power on switch
int AC_RELAY    =       4;          //AC RELAY PIN


//ac and dc meter
int  DCInput = A0;    // DC SENSE PIN
int  ACInput = A1;    // AC SENSE PIN
float vout0 = 0.0;
float vout1 = 0.0;
float vin0 = 0.0;
float vin1 = 0.0;
float R1 = 100000.0; // resistance  of  R1 (100K) -see text!
float R2 = 10000.0; //  resistance  of  R2 (10K) - see text!
float R3 = 100000.0; // resistance  of  R3 (100K) -see text!
float R4 = 10000.0; //  resistance  of  R4 (10K) - see text!
int value0 = 0;
int value1 = 0;
unsigned long previousMillis = 0;
byte screen = 0;
byte prevScreen = 0;
int counter = 0;


void setup() {
  // INVERTER
  pinMode(MOSFET1, OUTPUT);     // MOSFET 1
  pinMode(MOSFET2, OUTPUT);     // MOSFET 2
  pinMode(AC_RELAY, OUTPUT);
  pinMode(EnablePower, INPUT);

  // VOLTMETER
  pinMode(DCInput, INPUT);
  pinMode(ACInput, INPUT);

  lcd.begin(16, 2);

  // Turn off the display:
  lcd.setCursor(0, 0);
  lcd.print("DAVID OLAOLU");
  lcd.setCursor(7, 1);
  lcd.print("DAVID OLAOLU");
  lcd.noDisplay();
  delay(500);
  // Turn on the display:
  lcd.display();
  delay(500);
  lcd.clear();

  // Turn off the display:
  lcd.setCursor(1, 0);
  lcd.print("DAVID OLAOLU");
  lcd.setCursor(6, 1);
  lcd.print("DAVID OLAOLU");
  lcd.noDisplay();
  delay(1000);
  // Turn on the display:
  lcd.display();
  delay(1000);
  lcd.clear();


}

void loop() {

  unsigned long currentMillis = millis();
  //$$$$$$$$$$$$$ SENSE  DC $$$$$$$$$$$$$$$
  value0 = analogRead(DCInput);
  vout0 = (value0 * 5.0) / 1024.0; // see text
  vin0 = vout0 / (R2 / (R1 + R2));
  if (vin0 < 0.09) {
    vin0 = 0.0; //statement to quash undesired reading !
  }
  //$$$$$$$$$$$$$ SENSE  AC $$$$$$$$$$$$$$$

  value1 = analogRead(ACInput);
  vout1 = (value1 * 72.0) / 1024.0; // see text
  vin1 = vout1 / (R4 / (R3 + R4));
  if (vin1 < 0.09) {
    vin1 = 0.0; //statement to quash undesired reading !
  }
  if ((vin1 > 140) || (vin1 > 240))
  {
    digitalWrite(AC_RELAY, HIGH);  //
    digitalWrite(MOSFET1, LOW);
    digitalWrite(MOSFET2, LOW);
    delay(7);
    digitalWrite(MOSFET1, LOW);//theses lines are repeated????????????
    digitalWrite(MOSFET2, LOW);
    delay(7);


    if (digitalRead (EnablePower) == HIGH)
    {
      screen = 4;
    }
    else {//(EnablePower) == LOW)
      screen = 6;
    }
  }
  else {//((vin1 > 140) || (vin1 > 240))failed

    digitalWrite(AC_RELAY, LOW);

    if ((vin0 < 8) && (digitalRead (EnablePower) == HIGH))
    {
      digitalWrite(MOSFET1, LOW);
      digitalWrite(MOSFET2, LOW);
      delay(7);
      digitalWrite(MOSFET1, LOW);
      digitalWrite(MOSFET2, LOW);
      delay(7);
      screen = 1;

    }

    if ((vin0 > 8) && (digitalRead (EnablePower) == HIGH))
    {
      digitalWrite(MOSFET1, HIGH);
      digitalWrite(MOSFET2, LOW);
      delay(7);
      digitalWrite(MOSFET1, LOW);
      digitalWrite(MOSFET2, HIGH);
      delay(7);
      screen = 2;

    }

    if   (digitalRead (EnablePower) == LOW)
    {
      digitalWrite(MOSFET1, LOW);
      digitalWrite(MOSFET2, LOW);
      delay(7);
      digitalWrite(MOSFET1, LOW);
      digitalWrite(MOSFET2, LOW);
      delay(7);
      screen = 3;

    }

  }

  if (screen != prevScreen) {//screen has changed clear lcd
    lcd.setCursor(0, 0);
    lcd.clear();
    counter = 0;//reset timer in case screen changed before it went to screen 0
  }

  switch (screen) {

    case 0:
      previousMillis = currentMillis;//stop timer
      counter = 0;//stop counter
      lcd.clear();//clear lcd
      break;

    case 1:
      lcd.print("LOW BATTERY ");
      lcd.setCursor(0, 1);
      lcd.print("INPUT V= ");
      lcd.print(vin0);
      if (counter >= 10) {
        screen = 0;//display for 1 second then clear screen
      }
      break;

    case 2:
      lcd.print("INVERTER ON ");
      lcd.setCursor(0, 1);
      lcd.print("INPUT V= ");
      lcd.print(vin0);
      if (counter >= 10) {
        screen = 0;
      }
      break;

    case 3:
      lcd.print(" INVERTER OFF  ");
      lcd.setCursor(1, 1);
      lcd.print("INPUT V= ");
      lcd.print(vin0);
      if (counter >= 10) {
        screen = 0;
      }
      break;

    case 4:
      //lcd.write(6);
      lcd.setCursor(2, 0);
      lcd.print(" STANDBY ON ");
      lcd.setCursor(0, 1);
      lcd.print("MAINS  ON");
      if (counter >= 20) {//display 2 seconds then move to next screen
        lcd.clear();
        screen = 5;
        counter = 0;
      }
      break;

    case 5:
      //lcd.write(6);
      lcd.setCursor(2, 0);
      lcd.print( vin1);
      lcd.print(" V ");
      lcd.setCursor(0, 1);
      lcd.print("MAINS VOLTAGE");
      if (counter >= 20) {
        screen = 0;
      }
      break;

    case 6:
      //lcd.write(6);
      lcd.setCursor(2, 0);
      lcd.print(" STANDBY OFF ");
      lcd.setCursor(0, 1);
      lcd.print("MAINS  ON");
      if (counter >= 20) {
        lcd.clear();
        screen = 7;
        counter = 0;
      }
      break;

    case 7:
      //lcd.write(6);
      lcd.setCursor(2, 0);
      lcd.print( vin1);
      lcd.print(" V ");
      lcd.setCursor(0, 1);
      lcd.print("MAINS VOLTAGE");
      if (counter >= 20) {//display 2 seconds then move to next screen
        screen = 0;
      }
      break;
  }

  if (currentMillis - previousMillis > 100) {
    counter++;//counter increases every 10th of second
    previousMillis = currentMillis;
  }
  if (counter > 100) {//stop counter roll over
    counter = 100;
  }
  prevScreen = screen;


}

Thanks very much gpop1 I have check the new code you sent and I have been working on it. Am trying to adapt the code to what I Was looking for since you've given me the clue of what to do.

The counter worked but 1 affect the frequency of the PWM from 50hz to 26hz. 2 I want to circle the information like 1showing my name 2. Showing the switch status 3 showing the voltage. Am still working on this. I got it working in delay function but not suitable for the purpose.
If am unable to get it I will post the full cold and a short video of what am desiring to achieve with the lcd. Thanks for your help

are you trying to make a square wave ac output using the loop for timing?

"are you trying to make a square wave ac output using the loop for timing?" YES initially i was looking for sine wave 50hz with 2 outputs but when i don't have the idea of how to use the sine table i resulted to square wave (PWM).
Thanks for the example you gave me i have been trying to flip between 2 screens in case 4 but is mixing up. this is what am trying 1 show STANDBY ON and MAINS ON wait for 3 sec then show the mains voltage (case 4 and 7 to satisfy this condition if ((vin1 > 40) || (vin1 > 240 )) )
This was what i tried .There was a mix-up on the screen.Am reading more about counter to get the concept very well . your help was very helpful boss thanks

//lcd.write(6);
    lcd.setCursor(2, 0);
    lcd.print(" STANDBY ON ");
    lcd.setCursor(0, 1);
    lcd.print("MAINS  ON");
    if (counter >= 40) {//display 2 seconds then move to next screen
      lcd.clear();
      screen = 0;
      counter = 0;
    }

    lcd.setCursor(2, 0);
    lcd.print( vin1);
    lcd.print(" V ");
    lcd.setCursor(0, 1);
    lcd.print("MAINS VOLTAGE");
    if (counter >= 50) {//display 2 seconds then move to next screen
      lcd.clear();
      screen = 0;
      counter = 0;
    }

    break;

thanks very much i will upload the full code and the LCD screen short video to see what i have done initially and see where my problem lies thanks very much

    if ((vin1 >140)||(vin1>240))

If vin1 is greater than 240, then surely it is greater than 140?

the program I wrote was based on cause and affect.

If something happens then display this.

I think it can be modified to do what you want by changing case 0 and adding some lines to other cases that use to be in case 0. Try this to see what happens

never tested using break; in a “if” statement to exit the switch but it compiled so it may be expectable (if not you will need to code so the first “if” in case 0 doesn’t make the second “if” true.)

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

// inverter

int MOSFET1  =         9;           // MOSFET Bank1
int MOSFET2  =        10;           // MOSFET Bank2
int EnablePower =      2;           // HV power on switch
int AC_RELAY    =       4;          //AC RELAY PIN


//ac and dc meter
int  DCInput = A0;    // DC SENSE PIN
int  ACInput = A1;    // AC SENSE PIN
float vout0 = 0.0;
float vout1 = 0.0;
float vin0 = 0.0;
float vin1 = 0.0;
float R1 = 100000.0; // resistance  of  R1 (100K) -see text!
float R2 = 10000.0; //  resistance  of  R2 (10K) - see text!
float R3 = 100000.0; // resistance  of  R3 (100K) -see text!
float R4 = 10000.0; //  resistance  of  R4 (10K) - see text!
int value0 = 0;
int value1 = 0;
unsigned long previousMillis = 0;
byte screen = 0;
byte prevScreen = 0;
int counter = 0;
byte display = 0;

void setup() {
  // INVERTER
  pinMode(MOSFET1, OUTPUT);     // MOSFET 1
  pinMode(MOSFET2, OUTPUT);     // MOSFET 2
  pinMode(AC_RELAY, OUTPUT);
  pinMode(EnablePower, INPUT);

  // VOLTMETER
  pinMode(DCInput, INPUT);
  pinMode(ACInput, INPUT);

  lcd.begin(16, 2);

  // Turn off the display:
  lcd.setCursor(0, 0);
  lcd.print("DAVID OLAOLU");
  lcd.setCursor(7, 1);
  lcd.print("DAVID OLAOLU");
  lcd.noDisplay();
  delay(500);
  // Turn on the display:
  lcd.display();
  delay(500);
  lcd.clear();

  // Turn off the display:
  lcd.setCursor(1, 0);
  lcd.print("DAVID OLAOLU");
  lcd.setCursor(6, 1);
  lcd.print("DAVID OLAOLU");
  lcd.noDisplay();
  delay(1000);
  // Turn on the display:
  lcd.display();
  delay(1000);
  lcd.clear();


}

void loop() {

  unsigned long currentMillis = millis();
  //$$$$$$$$$$$$$ SENSE  DC $$$$$$$$$$$$$$$
  value0 = analogRead(DCInput);
  vout0 = (value0 * 5.0) / 1024.0; // see text
  vin0 = vout0 / (R2 / (R1 + R2));
  if (vin0 < 0.09) {
    vin0 = 0.0; //statement to quash undesired reading !
  }
  //$$$$$$$$$$$$$ SENSE  AC $$$$$$$$$$$$$$$

  value1 = analogRead(ACInput);
  vout1 = (value1 * 72.0) / 1024.0; // see text
  vin1 = vout1 / (R4 / (R3 + R4));
  if (vin1 < 0.09) {
    vin1 = 0.0; //statement to quash undesired reading !
  }
  if ((vin1 > 140) || (vin1 > 240))
  {
    digitalWrite(AC_RELAY, HIGH);  //
    digitalWrite(MOSFET1, LOW);
    digitalWrite(MOSFET2, LOW);
    delay(7);
    digitalWrite(MOSFET1, LOW);//theses lines are repeated????????????
    digitalWrite(MOSFET2, LOW);
    delay(7);


    if (digitalRead (EnablePower) == HIGH)
    {
      screen = 4;
    }
    else {//(EnablePower) == LOW)
      screen = 6;
    }
  }
  else {//((vin1 > 140) || (vin1 > 240))failed

    digitalWrite(AC_RELAY, LOW);

    if ((vin0 < 8) && (digitalRead (EnablePower) == HIGH))
    {
      digitalWrite(MOSFET1, LOW);
      digitalWrite(MOSFET2, LOW);
      delay(7);
      digitalWrite(MOSFET1, LOW);
      digitalWrite(MOSFET2, LOW);
      delay(7);
      screen = 1;

    }

    if ((vin0 > 8) && (digitalRead (EnablePower) == HIGH))
    {
      digitalWrite(MOSFET1, HIGH);
      digitalWrite(MOSFET2, LOW);
      delay(7);
      digitalWrite(MOSFET1, LOW);
      digitalWrite(MOSFET2, HIGH);
      delay(7);
      screen = 2;

    }

    if   (digitalRead (EnablePower) == LOW)
    {
      digitalWrite(MOSFET1, LOW);
      digitalWrite(MOSFET2, LOW);
      delay(7);
      digitalWrite(MOSFET1, LOW);
      digitalWrite(MOSFET2, LOW);
      delay(7);
      screen = 3;

    }

  }

  if (screen != prevScreen) {//screen has changed clear lcd
    lcd.setCursor(0, 0);
    lcd.clear();
    counter = 0;//reset timer in case screen changed before it went to screen 0
  }

  switch (screen) {

    case 0:
      if (counter >= 30 && display == 1) {
        screen = 7;
        display = 0;
        break;
      }
      if (counter >= 30 && display == 0) {
        screen = 4;
        display = 1;
        break;
      }
      break;

    case 1:
      lcd.print("LOW BATTERY ");
      lcd.setCursor(0, 1);
      lcd.print("INPUT V= ");
      lcd.print(vin0);
      if (counter >= 10) {
        screen = 0;//display for 1 second then clear screen
        counter = 0;//reset counter
      }
      break;

    case 2:
      lcd.print("INVERTER ON ");
      lcd.setCursor(0, 1);
      lcd.print("INPUT V= ");
      lcd.print(vin0);
      if (counter >= 10) {
        screen = 0;
        counter = 0;//reset counter
      }
      break;

    case 3:
      lcd.print(" INVERTER OFF  ");
      lcd.setCursor(1, 1);
      lcd.print("INPUT V= ");
      lcd.print(vin0);
      if (counter >= 10) {
        screen = 0;
        counter = 0;//reset counter
      }
      break;

    case 4:
      //lcd.write(6);
      lcd.setCursor(2, 0);
      lcd.print(" STANDBY ON ");
      lcd.setCursor(0, 1);
      lcd.print("MAINS  ON");
      if (counter >= 20) {//display 2 seconds then move to next screen
        lcd.clear();
        screen = 5;
        counter = 0;
      }
      break;

    case 5:
      //lcd.write(6);
      lcd.setCursor(2, 0);
      lcd.print( vin1);
      lcd.print(" V ");
      lcd.setCursor(0, 1);
      lcd.print("MAINS VOLTAGE");
      if (counter >= 20) {
        screen = 0;
        counter = 0;//reset counter
      }
      break;

    case 6:
      //lcd.write(6);
      lcd.setCursor(2, 0);
      lcd.print(" STANDBY OFF ");
      lcd.setCursor(0, 1);
      lcd.print("MAINS  ON");
      if (counter >= 20) {
        lcd.clear();
        screen = 7;
        counter = 0;
      }
      break;

    case 7:
      //lcd.write(6);
      lcd.setCursor(2, 0);
      lcd.print( vin1);
      lcd.print(" V ");
      lcd.setCursor(0, 1);
      lcd.print("MAINS VOLTAGE");
      if (counter >= 20) {//display 2 seconds then move to next screen
        screen = 0;
        counter = 0;//reset counter
      }
      break;
  }

  if (currentMillis - previousMillis > 100) {
    counter++;//counter increases every 10th of second
    previousMillis = currentMillis;
  }
  if (counter > 100) {//stop counter roll over
    counter = 100;
  }
  prevScreen = screen;


}

Thanks for your response I have been rewriting the whole code I have checked what you sent gpop1 thanks. You are the only helpful person who is there for me loll thanks. I will soon upload the new code.