using millis in lcd

hello everyone in this code i’m trying to use the millis function instead of the delay in the LCD. The code will display 6 different reading from the sensors, but when i used the milis it just display 1 and 2 only.

#include <LiquidCrystal.h>
long time1,time2,time3;
const int  PUSH= 7;
int state;
int i,j;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  pinMode( PUSH, INPUT);
  Serial.begin(9600);
  time1 = millis();
  time2 = millis();
  time3 = millis();
 
}

void loop() {
  int i;
 state=digitalRead(PUSH);
    
     
     
     if (state==HIGH){
    
       if (millis()-time1 >= 2000){
     Serial.print(state);
        lcd.clear();
        lcd.setCursor(1,0);
        lcd.print ("Soil Mositure");
        lcd.setCursor(1,1);
        lcd.print ("Information");
        //delay(2000);
      time1=millis();
       }
  
       //else if  (millis()-time2 >= 2500){
        for(j=0; j<5; j++){
        if  (millis()-time2 >= 4000){
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print ("Soil ");// Sets the location at which subsequent text written to the LCD will be displayed
        lcd.print(j+1); // Prints the soil mositure  value from the sensor
        lcd.print(" :");
        //lcd.print(sesnor[j]);   
        j++;
        lcd.setCursor(0,1);
        lcd.print ("Soil ");
        lcd.print(j+1);
        lcd.print(" :");
        //lcd.print(sesnor[j]);   
        //delay(2500);
       time2=millis();
        
        }
     
}
     }
  else  {
  if (millis()-time3 >= 1000){
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("****Welcome*****");
    lcd.setCursor(6, 1);
    lcd.print("^__^");
     //delay(1000);
      time3=millis();
   }

}
     }

How have you wired the button? With a pull down resistor ?

6v6gt: How have you wired the button? With a pull down resistor ?

yes

Just looking at the posted code, STATE == HIGH is the preventer of the Else statement from being ran. Perhaps looking into how the STATE button is wired up, such as with a pullup could solve the issue?

The soil sensors all use the same timer. If the button is down, it looks at the timer five times and if the timer has expired, displays that one number and resets the timer. That means 99% of the time it will only show “Soil 1” and then skip the other four because the timer has been reset.

I think you need a classic state machine:

#include <LiquidCrystal.h>


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


const int  PUSH = 7;


enum States {DrawTitle, HoldTitle, Soil1, Soil2, Soil3, Soil4, Soil5} State = DrawTitle;
unsigned long StateStartTime = 0;


void setup()
{
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  pinMode( PUSH, INPUT);
  Serial.begin(9600);
}


void DisplaySoilSensor(int j)
{
  // Then show the Soil1 page:
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print ("Soil ");// Sets the location at which subsequent text written to the LCD will be displayed
  lcd.print(j + 1); // Prints the soil mositure  value from the sensor
  lcd.print(" :");
  //lcd.print(sesnor[j]);
  j++;
  lcd.setCursor(0, 1);
  lcd.print ("Soil ");
  lcd.print(j + 1);
  lcd.print(" :");
  //lcd.print(sesnor[j]);
}


void loop()
{
  unsigned long currentTime = millis();


  switch (State)
  {
    case DrawTitle:
      lcd.clear();
      lcd.setCursor(1, 0);
      lcd.print ("Soil Mositure");
      lcd.setCursor(1, 1);
      lcd.print ("Information");
      StateStartTime = currentTime;
      State = HoldTitle;
      break;


    case HoldTitle:
      // Keep the title page displaying for two seconds
      if (currentTime - StateStartTime >= 2000)
      {
        // Then show the Soil1 page:
        DisplaySoilSensor(1);


        StateStartTime = currentTime;
        State = Soil1;
      }
      break;


    case Soil1:
      // Keep the Soil1 page displaying for four seconds
      if (currentTime - StateStartTime >= 4000)
      {
        DisplaySoilSensor(2);


        StateStartTime = currentTime;
        State = Soil2;
      }
      break;




    case Soil2:
      // Keep the Soil2 page displaying for four seconds
      if (currentTime - StateStartTime >= 4000)
      {
        DisplaySoilSensor(3);


        StateStartTime = currentTime;
        State = Soil3;
      }
      break;
  }
}/code]