How I replace delay() for the millis()?

Good Afternoon, I need help modifying my code. I'm doing an automatic house with arduino, with some sensors and leds.

I made the system work with only one sensor, when I put more sensors they were not working properly because of the delay () function that delays the whole system, delaying the other sensors too, I need all sensors to always work.

My question is how do I replace delay () for the millis () function?

All the help is welcome, I'm starting on the Arduino.

My code:

#include <LiquidCrystal.h> 
#include <DHT.h> 
#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h> 

#define DHTPIN A5
#define DHTTYPE DHT11
#define SS_PIN 53
#define RST_PIN 9

#include <SPI.h>
#include <MFRC522.h>

MFRC522 mfrc522(SS_PIN, RST_PIN);  
DHT dht(DHTPIN, DHTTYPE);
Servo microservo9g; 
Servo microservo8g; 
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

//Array simbolo grau
byte grau[8] ={ B00001100,
               B00010010,
               B00010010,
               B00001100,
               B00000000,
               B00000000,
               B00000000,
               B00000000,};

int entrada = A2; 

int led = 8;
int leitura = 0;
int alarme = 100; 

int ledPin1 = 42;
int ledPin2 = 22;
int ldrPin = 0; 
int ldrValor = 0; 

int led_liberado = 7;
int led_negado = 6;

char st[20];
int Sensor = 10;
int Buzzer = 26; 
int Var = 0;
int pinotrig = 49;
int pinoEcho = 47;
int distancia;

void setup()
{
Serial.begin(9600);
lcd.begin(16,2); 
lcd.clear(); 
lcd.createChar(0, grau);
pinMode(led,OUTPUT);  
Serial.begin(9600);
pinMode(ledPin1,OUTPUT);
pinMode(ledPin2,OUTPUT);  
Serial.begin(9600); 
pinMode(led_liberado, OUTPUT);
 pinMode(led_negado, OUTPUT);
 microservo9g.attach(24);
 microservo9g.write(90);
 Serial.begin(9600);
 SPI.begin();
 mfrc522.PCD_Init(); 
 Serial.println("Aproxime o seu cartao do leitor...");
 Serial.println();
pinMode(Buzzer, OUTPUT);
pinMode(Sensor, INPUT);
Serial.begin(9600);
pinMode(pinotrig,OUTPUT);
pinMode(pinoEcho,INPUT);
microservo8g.attach(28);
microservo8g.write(90);
Serial.begin(9600);
}

void loop()
{
float h = dht.readHumidity();
float t = dht.readTemperature(); 
lcd.setCursor(0,0);
lcd.print("Temp : ");
lcd.print(" ");
lcd.setCursor(7,0);
lcd.print(t,1);
lcd.setCursor(12,0);
lcd.write((byte)0);
lcd.setCursor(0,1);
lcd.print("Umid : ");
lcd.print(" ");
lcd.setCursor(7,1);
lcd.print(h,1);
lcd.setCursor(12,1);
lcd.print("%");
delay(100);
leitura = analogRead(entrada); 
 if (leitura > alarme) {
   digitalWrite(led,HIGH);
 }
 else {
   digitalWrite(led,LOW);
 }
 Serial.println(leitura); 
 delay(100);
 ldrValor = analogRead(ldrPin); 
if (ldrValor>= 700) 
digitalWrite(ledPin1,HIGH);
if (ldrValor>= 700) 
digitalWrite(ledPin2,HIGH); 
if (ldrValor< 700) 
digitalWrite(ledPin1,LOW); 
if (ldrValor< 700) 
digitalWrite(ledPin2,LOW); 
delay(100);
 if ( ! mfrc522.PICC_IsNewCardPresent()) 
 {
   return;
 }
 if ( ! mfrc522.PICC_ReadCardSerial()) 
 {
   return;
 }
 Serial.print("UID da tag :");
 String conteudo= "";
 byte letra;
 for (byte i = 0; i < mfrc522.uid.size; i++) 
 {
    Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
    Serial.print(mfrc522.uid.uidByte[i], HEX);
    conteudo.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
    conteudo.concat(String(mfrc522.uid.uidByte[i], HEX));
 }
 Serial.println();
 Serial.print("Mensagem : ");
 conteudo.toUpperCase();
 
 if (conteudo.substring(1) == "30 C4 AF 56")
 {
   microservo9g.write(-90);
   digitalWrite(led_liberado, HIGH);
   Serial.println("Cartao1 - Acesso liberado !");
   Serial.println();
   delay(4000);
   microservo9g.write(90);
   digitalWrite(led_liberado, LOW);
   }
 if (conteudo.substring(1) == "C8 16 B4 79")

 {
   Serial.println("Cartao2 - Acesso negado !!");
   Serial.println();
   for (int i= 1; i<5 ; i++)
   {
     digitalWrite(led_negado, HIGH);
     delay(200);
     digitalWrite(led_negado, LOW);
     delay(200);
   }
}    
delay(100);
 Var=digitalRead(Sensor);
Serial.print(Var);
if(Var<1) 
{
digitalWrite(Buzzer, HIGH); 
delay(100);
digitalWrite(Buzzer, LOW);
delay(20);
} 
 digitalWrite(pinotrig,HIGH);
  delayMicroseconds(11);
  digitalWrite(pinotrig,LOW);
  distancia = pulseIn(pinoEcho, HIGH);
  distancia = distancia/29/2;
  
 if ((distancia < 0) && (distancia <= 5))
 {
   
   microservo8g.write(-90);
   delay(3000);
   microservo8g.write(90);
   
   }
}

My question is how do I replace delay () for the millis () function?

Have you read Using millis() for timing. A beginners guide, Several things at the same time and looked at the BlinkWithoutDelay example in the IDE.

Please edit your post and use code tags around the code unless some of it is intended to be in italics

See read this before posting a programming question

These might be of interest to you:

https://forum.arduino.cc/index.php?topic=525240.0

https://forum.arduino.cc/index.php?topic=526696.0

Larryd, I after reading these posts about millis () I tried to edit my code with millis () but it did not work out, you can send me an example of a code with two sensors using the millis function so I have an idea ?

The links that UKHeliBob and the ones I gave, show good examples on how to manage nonblocking delays.

What exactly are you having trouble understanding?

I can not explain exactly where I have difficulties. I just can not understand millis (), it's very complex for me, I've read many posts about millis () and I have not been able to replace the delay () in my code with millis ().
If you could help me replace I would be very grateful.

You have used delay() 10 times in your program and you cannot simply use millis() to replace them. I am afraid that you will need to restructure your program. You must break your program into a number of steps that need timing then use the BlinkWithoutDelay method to do the timing. Put the program into the required state, such as positioning the servo and save the millis() value as the start time. Then each time through loop() check whether the current time minus the start time exceeds the required waiting period. If not then go round loop() again and again until it does and you can move on to the next action.

Incidentally

   microservo9g.write(-90);

will not work. The servo expects to be commanded to move to an explicit angle between 0 and 180 (possibly less). You cannot use the Servo library to move relative to the current position or to angles outside of the range 0 to 180,

Not going to rewrite your current code, but …

Let’s start with a simple example.

Show us a small bit of sample code that uses two sensors where you would what to incorporate BWD.

For example, this uses the DHT11 sensor and RFID:

#include <LiquidCrystal.h>
#include <DHT.h> 
#include <MFRC522.h>
#include <Servo.h> 

#define DHTPIN A5
#define DHTTYPE DHT11
#define SS_PIN 53
#define RST_PIN 9

 
DHT dht(DHTPIN, DHTTYPE); 
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
MFRC522 mfrc522(SS_PIN, RST_PIN); 
Servo microservo9g; 

byte grau[8] ={ B00001100,
                B00010010,
                B00010010,
                B00001100,
                B00000000,
                B00000000,
                B00000000,
                B00000000,};

unsigned long tempoinicial;

int led_liberado = 7;
int led_negado = 6;
int ledPin1 = 42;
int ledPin2 = 22;
int ldrPin = 0; 
int ldrValor = 0;

char st[20]; 

void setup()
{
  Serial.begin(9600);
  lcd.begin(16,2); 
  lcd.clear(); 
  lcd.createChar(0, grau);
  tempoinicial = millis(); 
 pinMode(led_liberado, OUTPUT);
  pinMode(led_negado, OUTPUT);
  microservo9g.attach(24);
  microservo9g.write(90);
  Serial.begin(9600);
  SPI.begin();
  mfrc522.PCD_Init(); 
  Serial.println("Aproxime o seu cartao do leitor...");
  Serial.println();
  pinMode(ledPin1,OUTPUT);
  pinMode(ledPin2,OUTPUT);  
  Serial.begin(9600); 
}
 
void loop()
{
long tempoatual = millis();
float h = dht.readHumidity();
float t = dht.readTemperature();
lcd.setCursor(0,0);
lcd.print("Temp : ");
lcd.print(" ");
lcd.setCursor(7,0);
lcd.print(t,1);
lcd.setCursor(12,0);
lcd.write((byte)0);
lcd.setCursor(0,1);
lcd.print("Umid : ");
lcd.print(" ");
lcd.setCursor(7,1);
lcd.print(h,1);
lcd.setCursor(12,1);
lcd.print("%");
delay (2000);
  if ( ! mfrc522.PICC_IsNewCardPresent()) 
  {
    return;
  }
  if ( ! mfrc522.PICC_ReadCardSerial()) 
  {
    return;
  }
  Serial.print("UID da tag :");
  String conteudo= "";
  byte letra;
  for (byte i = 0; i < mfrc522.uid.size; i++) 
  {
     Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
     Serial.print(mfrc522.uid.uidByte[i], HEX);
     conteudo.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
     conteudo.concat(String(mfrc522.uid.uidByte[i], HEX));
  }
  Serial.println();
  Serial.print("Mensagem : ");
  conteudo.toUpperCase();
  
  if (conteudo.substring(1) == "30 C4 AF 56")
  {
    microservo9g.write(-90);
    digitalWrite(led_liberado, HIGH);
    Serial.println("Cartao1 - Acesso liberado !");
    Serial.println();
    delay (4000);
    microservo9g.write(90);
    digitalWrite(led_liberado, LOW);
    }
  if (conteudo.substring(1) == "C8 16 B4 79")
 
  {
    Serial.println("Cartao2 - Acesso negado !!");
    Serial.println();
    for (int i= 1; i<5 ; i++)
    {
      digitalWrite(led_negado, HIGH);
      delay (200);
      digitalWrite(led_negado, LOW);
      delay (20);
    }
}

https://forum.arduino.cc/index.php?topic=526696.0

In the link above (which you have read) there are two sketches.

The first sketch is written like yours, i.e. it has delay(this) and delay(that).

In the second sketch, the delay()s have been replaced by a shared timer (written with BWD in mind).

Suggest you compare the two sketches to see what the differences are.

BTW
You are aware using 'return' in your loop() code, cuts off any further loop() code execution when 'if' conditions are met. i.e. execution goes back to the start of loop().

I think I'll give up trying to replace delay () with millis () and I'll present my project like this. It's slow but at least it works. Thanks anyway for trying to help me.