problem with lcd display and using millis

Hi everyone, i am a begginer in arduino and i am going to create an incubator using arduino. Тhe problems I have are: I want the lcd display to show me a screen with temperature and humidity information for 5 seconds then clear and show me the remaining days and how long the system works as I want to use millis instead of delay but it doesnt work and i dont know how can deal with it..this is the code I have developed and hope someone to help me with this one. thanks in advance. and sorry if my English is bad,basic knowledges. :slight_smile:

 #include <DHT.h>
#define DHTPIN 2
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define DHTTYPE DHT22
#include <Servo.h>
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
Servo servo;
int pinOut7 = 7;
int pinOut9 = 9;
int pinOut10 = 10;
int pinOut11 = 11; 

float Temp = 22.7;    
float deltaTemp = 0.1; 
float Humidity = 70;
float HumidityDelta = 5; 
int angle;
unsigned long startMillis;  //some global variables available anywhere in the program
unsigned long currentMillis;
const unsigned long period = 10000;
unsigned long swimMillis = 20000; 
unsigned long LSDMillis, LSDMillis1, LSDMillis2;
unsigned long RelayMillis;
void setup()
{
  startMillis = millis();
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  dht.begin();
  lcd.begin(16, 2);
  lcd.clear();
  lcd.print("T & H controller");
  delay(1000);
  lcd.clear();
  servo.attach(8);
  servo.write(120);
  
  LSDMillis1 = millis() + 30000;
  LSDMillis2 = millis() + 6000;
}

void motor()
{
  
if(angle>56)
  {
    for(angle = 146; angle > 56; angle--)    
  {                                
      servo.write(angle);           
      delay(25);       
  }
  }
    else
  {
      for(angle = 56; angle < 146; angle++)  
  {                                  
      servo.write(angle);               
      delay(25);                   
  }
  }

  
}

void display()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  float f = dht.readTemperature(true);
  int is, im, ih, id, ida; // variables for time
  float time, s1, m1, h1, d1; // Set up variables to calculate time
  time = millis(); // Get time in milliseconds since tunit turn on
  // до тук
  s1 = time / 1000; // Convert time to seconds, minutes, hours, days
  m1 = s1 / 60;
  h1 = m1 / 60;
  d1 = h1 / 24;
  id = int(d1); // Strip out remainder to leave Days:Hours:Minutes:Seconds
  ih = int((d1 - int(d1)) * 24);
  im = int((h1 - int(h1)) * 60);
  is = int((m1 - int(m1)) * 60);
  ida = 21 - id;
  if (isnan(h)  || isnan(t) || isnan(f) ) {
    lcd.print("DHT Sensor fail");
    return;
  }
  float hif = dht.computeHeatIndex(f, h);
  float hic = dht.computeHeatIndex(t, h, false);
  lcd.setCursor(0, 0);
  lcd.print("Humidity: ");
  lcd.print(h);
  lcd.print("%");
  lcd.setCursor(0, 1);
  lcd.print("Temp:     ");
  lcd.print(t);
  lcd.print("C");
  if (currentMillis - LSDMillis1 < 0)
  {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Time");
    lcd.print(id);
    lcd.print("d ");
    lcd.print(ih);
    lcd.print("h ");
    lcd.print(im);
    lcd.print("m ");
    lcd.print(is);
    lcd.print("s ");
    lcd.setCursor(0, 1);
    lcd.print("Days left:");
    lcd.print(21 - id);
    LSDMillis1 = LSDMillis1 + 30000;
   

  }

  if (currentMillis - LSDMillis2 < 0)  
  {
    lcd.clear();
    LSDMillis2 = LSDMillis2 + 30000;
  }
}

void releys(float Temp, float Humidity ) 
{
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  if (t <24.3){
    digitalWrite(pinOut9, LOW);
  }
 if(t>24.4) 
   {
    digitalWrite(pinOut9, HIGH);
  }
 if (h <50){
    digitalWrite(pinOut10, LOW);
    digitalWrite(pinOut11, LOW);
  }
  if(h>60)
  {
  
  digitalWrite(pinOut10, HIGH);
  digitalWrite(pinOut11, LOW);

  }
}
void loop()
{
  currentMillis = millis();
  if (currentMillis - startMillis >= swimMillis) 

  {
    motor();
    startMillis = currentMillis;
  }
  releys(Temp, Humidity ); 

  display();
}

Please define "doesn't work". :confused:

Well okay. The lcd display only the first screen with the temperature and humidity and update them but never show the second one with the time and the days left. its work fine with delay but in that case all cycle is stopped for this 5 seconds.

Just to clarify, do you mean display the H&T for 5 seconds from start up, and then switch to the other data for ever more?- ie only show the H&T once and never again?

The T&H controller is displayed once, it is in the setup section. i want to display the values of the temp and humidity for 5 seconds then clear and display the time and days left. and again 1st screen with values of temp and humidity. 2 displays switching for 5 seconds. i used millis for the servo motor and i sucess but here i cant :confused: i know my code is mess and my english too, but i hope you can understand me and help me :slight_smile: i keep trying to make it work. maybe is veeeery easy to be done but i think my head is already mess to and cant figured out.

The code below does what you want, but I wrote it as a stand-alone sketch to prove the concept; you will need to build it into your code.

It uses Blink Without Delay thinking to toggle a boolean variable displayScreenOne every time interval expires. (The ! means not, so it sets the boolean to the opposite of what it is at the moment.) I just read 4 variables from 4 analog pins to give it something to print. If displayScreenOne is true, it prints variables 1 and 2; when it's false it prints variables 3 and 4.

// reads 4 analog values
// displays 2 of them on top and bottom line of lcd
// delay()-lessly toggles to show the other 2 instead
// repeat

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);

unsigned long previousMillis;
int interval = 2000;
bool displayScreenOne = false;

byte val1;
byte val2;
byte val3;
byte val4;

void setup()
{
  // initialize the LCD
  lcd.begin();

  // Turn on the blacklight and print a message.
  lcd.backlight();
  lcd.print("Hello, world!");
  delay(1000);
  lcd.clear();

}

void loop()
{
  readValues();
  displayStuff();
}

void readValues()
{
  val1 = analogRead(1);
  val2 = analogRead(2);
  val3 = analogRead(3);
  val4 = analogRead(4);
}

void displayStuff()
{
  if (millis() - previousMillis >= interval)
  {
    previousMillis = millis();

    displayScreenOne = !displayScreenOne;
    if (displayScreenOne)
    {
      lcd.setCursor(0, 0);
      lcd.print("value1: "); lcd.print(val1);
      lcd.setCursor(0, 1);
      lcd.print("value2: "); lcd.print(val2);
    }
    else
    {
      lcd.setCursor(0, 0);
      lcd.print("value3: "); lcd.print(val3);
      lcd.setCursor(0, 1);
      lcd.print("value4: "); lcd.print(val4);
    }
  }
}

Thank you very much i will insert it in my code and test it. i will write again if it works or no :slight_smile:

i think it works great now. huge thaaaanks :slight_smile: :slight_smile: