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. ![]()
#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();
}