Use millis function instead of delay

Hello!
We have created an irrigation system that once in a while reads the sensors and if necessary starts the irrigation, if not, waits until the next reading.
After installing and running several tests, I noticed that after about a month the program crashed.
From the information found on the internet I understood that it may be from the dealy function.
I understand that if I use the millis function, I will no longer have these problems.
I've been searching, but I don't know how to enter this function in my code, can you help me?

{
  digitalWrite(13, HIGH);
  
     { 
     lcd.setCursor(1,0);
     lcd.print("STOP!");
     delay(5000);
     lcd.clear();
     lcd.setCursor(6,2);
     lcd.print((procent_s_1+procent_s_2)/2);           
     lcd.print ("%");     
     lcd.setCursor(3,0);
     lcd.print("Umiditate:");
     **delay(7200000);**
     lcd.clear();
     }
     
    
      
    
    
}
else
{
  digitalWrite(13, LOW); 
     { 
     lcd.setCursor(1,0);
     lcd.print("START!");
     delay(5000);
     lcd.clear();
     lcd.setCursor(6,2);
     lcd.print((procent_s_1+procent_s_2)/2);           
     lcd.print ("%");     
     lcd.setCursor(3,0);
     lcd.print("Umiditate:");
     **delay(900000 );**
     lcd.clear();
     }
      
   
}
}

Hello,
Welcome to the forum.
Please share your complete code.
For the moment, i do not see problems with your code apart from the fact that I am not sure that **delay() even compiles.

I think the OP was only trying to help us find the delay() call in question.

Short answer: not your problem. That is a perfectly valid use of delay() and is not going to cause periodic problems.

Long answer: full code, schematic, power supply &c. Your problem is elsewhere.

As far as anyone could tell from your snippet.

a7

1 Like

"**" are for easier identification

#include <Wire.h>                // folosire librarie pentru protocol i2c
#include <LiquidCrystal_I2C.h>   // folosire librarie pentru afisaj lcd1602 pe i2c

LiquidCrystal_I2C lcd(0x27,20,4);
  
  


void setup() {               
   lcd.init();
   lcd.begin(16, 2);
   lcd.backlight();
   lcd.setCursor(1,0);
   lcd.print("Citire date...");
   delay(5000);
   lcd.clear();

   Serial.begin(9600);

   pinMode(13, OUTPUT);  
}



void loop() {
    

     
     
     int senzor2 = analogRead(A1);
     int procent_s_2 = map(senzor2, 1023, 0, 0, 100);   
     
     // afisare in COM3
     Serial.print(procent_s_2);
     Serial.print ("senzor2 %");
     
     int senzor1 = analogRead(A0);
     int procent_s_1 = map(senzor1, 1023, 0, 0, 100);   
     
     // afisare in COM3
     Serial.print(procent_s_1);
     Serial.print ("senzor1 %");


     
     if ((procent_s_1+procent_s_2)/2 > 45)

     
     
{
  digitalWrite(13, HIGH);
  
     { 
     lcd.setCursor(1,0);
     lcd.print("STOP");
     delay(5000);
     lcd.clear();
     lcd.setCursor(6,2);
     lcd.print((procent_s_1+procent_s_2)/2);           
     lcd.print ("%");     
     lcd.setCursor(3,0);
     lcd.print("Umiditate:");
     delay(7200000);
     lcd.clear();
     }
     
    
      
    
    
}
else
{
  digitalWrite(13, LOW); 
     { 
     lcd.setCursor(1,0);
     lcd.print("START");
     delay(5000);
     lcd.clear();
     lcd.setCursor(6,2);
     lcd.print((procent_s_1+procent_s_2)/2);           
     lcd.print ("%");     
     lcd.setCursor(3,0);
     lcd.print("Umiditate:");
     delay(900000 );
     lcd.clear();
     }
      
   
}
}

This is full code. On the schemathic and power supply side, I'm sure I have no problems. The current is filtered, the solders were properly.

You probably don't need to do that.To call attention when you think we might no see something, just

 // PUT A SHOUTING COMMENT ON THE LINE

This is more important if you expect us to be able to compile and perhaps even execute your code, otherwise as you can see, it just gives a person something to wonder about.

a7

Hello
Do you can provide comments to these times used?

        Line 15:      delay(5000);
	Line 56:      delay(5000);
	Line 63:      delay(7200000);
	Line 78:      delay(5000);
	Line 85:      delay(900000 );

Why are you printing to Serial, do you have this system constantly hooked to IDE monitor?

The signature of delay is void delay(unsigned int)
Int is normally 16 bit, that implies limited to 65535 so
delay (7200000) does not make much sense.

Not in the document I found right away, viz:

 ms: the number of milliseconds to pause. Allowed data types: unsigned long

https://www.arduino.cc/reference/en/language/functions/time/delay/

a7

If you say so.

a7

What are the two analog signals for?

I have simulated your code in wokwi, and it works fine as far as I can tell, but it would help to know what the two signals you are reading represent.

a7

the two signals are from soil moisture sensors.
Yes, the code works exactly the way I want, only it crashed after about a month. And I think the dealy is a bit big, I would like to replace the dealy function with millis

No, remained during the tests

If all you are doing is delay-ing, what's the point?

If you have extra functionality to address, then restructuring your program to use a millis() orientated approach makes sense.

But your program crashes after some time.

It makes no sense to strat changing the code until you have exorcised whatever demons are causing your problems.

The cause is not the code, and you have ruled out everything else. Therefor I at least have nothing more to offer.

a7

How many times has it failed?

Do you know more precisely how long it took to crash?

Hello
Did you watert your living room caused by the virtuell "crashed" sketch? :wink:

Hello
I have made some mods inside your sketch. I have replaced the long-time delays by adding state logic and a timer.
Try and check.

#include <Wire.h>                // folosire librarie pentru protocol i2c
#include <LiquidCrystal_I2C.h>   // folosire librarie pentru afisaj lcd1602 pe i2c
LiquidCrystal_I2C lcd(0x27, 20, 4);
int senzor2;
int procent_s_2;
int senzor1;
int procent_s_1;
int procent_s;
void setup() {
  lcd.init();
  lcd.begin(16, 2);
  lcd.backlight();
  lcd.setCursor(1, 0);
  lcd.print("Citire date...");
  delay(5000);
  lcd.clear();
  Serial.begin(9600);
  pinMode(13, OUTPUT);
}
void loop() {
  // make timer to read sensors
  static unsigned long sensorMillis;
  const unsigned long sensorDuration {1000};
  if (millis() - sensorMillis >= sensorDuration) {
    sensorMillis = millis();
    senzor2 = analogRead(A1);
    int procent_s_2 = map(senzor2, 1023, 0, 0, 100);
    // afisare in COM3
    Serial.print ("senzor2 %");
    Serial.println(procent_s_2);
    int senzor1 = analogRead(A0);
    int procent_s_1 = map(senzor1, 1023, 0, 0, 100);
    // afisare in COM3
    Serial.print ("senzor1 %");
    Serial.println (procent_s_1);
    procent_s = (procent_s_1 + procent_s_2) / 2;
    Serial.println(procent_s);
    lcd.setCursor(6, 2);
    lcd.print(procent_s);
    lcd.print ("%");
    lcd.setCursor(3, 0);
    lcd.print("Umiditate:");
  }
  if (procent_s > 45) {
    if (!digitalRead(13)) {
    digitalWrite(13, HIGH);
      lcd.clear();
      lcd.setCursor(1, 0);
      lcd.print("STOP");
      delay(5000);
      lcd.clear();
    }
    //    delay(7200000);
  }
  else
  {
    if (digitalRead(13)) {
    digitalWrite(13, LOW);
      lcd.clear();
      lcd.setCursor(1, 0);
      lcd.print("START");
      delay(5000);
      lcd.clear();
    }
    //    delay(900000 );
  }
}