user85224: Here data is updated after every 2 seconds. But when the condition is true then the valve and motor process starts and data is updated only after completion of that part (i.e., data is updated after 25 seconds). I want data updation and condition checking simultaneously.
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define ONE_WIRE_BUS 4
#define TEMPERATURE_PRECISION 9

#define motor 6
#define valve 7

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

const float thld = 35;

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

void setup() {
  Serial.begin (115200);
  pinMode(motor, OUTPUT);
  pinMode(valve, OUTPUT);
  delay(10);

    if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }
    delay(2000);
 
  display.setTextColor(WHITE);
}
 void loop() {   
 sensors.requestTemperatures(); 
  float temp = sensors.getTempCByIndex(0);   
  Serial.println(temp);
  Serial.println((char)176);
  Serial.println("C  |  ");
    if ( temp > thld)
  {
  digitalWrite (valve, LOW);
  delay (5000);
  digitalWrite (motor, LOW);
  delay (10000);

  digitalWrite (motor, HIGH);
  delay (6000);
    digitalWrite (valve, HIGH);
  delay (2000);
  }
  else
  {
    digitalWrite (motor, HIGH);
    digitalWrite (valve, HIGH);
  }

   display.clearDisplay();
   display.setCursor(0, 0);
   display.setTextSize(1);
   display.println("Temperature");   
   display.setCursor(0, 30);
   display.println("Soil & Water Engg.");
   display.setTextSize(2);
   display.setCursor(0,10);
   display.println(temp);
   display.setCursor(75, 10);
   display.println("C");
   display.setCursor(0,40);
     display.display();
   
delay (2000);
 }


user4143: You need to think about this with a real-world analogue.
I like to use boiling an egg...
There are three ways you can boil an egg:
Blocking operation

Put the egg in the boiling water
Stand there staring at it for three minutes or so
Take the egg out of the water and eat it

Timer operation

Put the egg in the boiling water
Set a timer for 3 minutes time
Go off and do something else
When the alarm goes off take the egg out and eat it

Time polling

Put the egg in the boiling water and record the current time
Go off and do something else, every so often glancing at your watch and comparing the time to the time you started
When three minutes have passed take the egg out and eat it

Currently you are using the blocking operation where you're stood staring at the egg until it's time to do something.  You need to change your code to use one of the other methods.
Most times the time polling method is good enough. You may get some jitter where whatever else it is you are doing causes a delay before you get to glance at your watch, but that is rarely an issue. For greater precision (better for higher frequency checking) you use a timer which triggers an interrupt so you always do things at more precise intervals.
There is an example in the IDE called BlinkWithoutDelay which implements the time polling in a very simplistic way - record the time, then compare that time and the current time, and when it's elapsed change the LED's state, recording a new time.

85224: <p>Here data is updated after every 2 seconds. But when the condition is true then the valve and motor process starts and data is updated only after completion of that part (i.e., data is updated after 25 seconds). I want data updation and condition checking simultaneously.</p>
<pre><code>#include &lt;Wire.h&gt;
#include &lt;Adafruit_Sensor.h&gt;
#include &lt;OneWire.h&gt;
#include &lt;DallasTemperature.h&gt;
#include &lt;SPI.h&gt;
#include &lt;Adafruit_GFX.h&gt;
#include &lt;Adafruit_SSD1306.h&gt;

#define ONE_WIRE_BUS 4
#define TEMPERATURE_PRECISION 9

#define motor 6
#define valve 7

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&amp;oneWire);

const float thld = 35;

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &amp;Wire, -1);

void setup() {
  Serial.begin (115200);
  pinMode(motor, OUTPUT);
  pinMode(valve, OUTPUT);
  delay(10);

    if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
    Serial.println(F(&quot;SSD1306 allocation failed&quot;));
    for(;;);
  }
    delay(2000);
 
  display.setTextColor(WHITE);
}
 void loop() {   
 sensors.requestTemperatures(); 
  float temp = sensors.getTempCByIndex(0);   
  Serial.println(temp);
  Serial.println((char)176);
  Serial.println(&quot;C  |  &quot;);
    if ( temp &gt; thld)
  {
  digitalWrite (valve, LOW);
  delay (5000);
  digitalWrite (motor, LOW);
  delay (10000);

  digitalWrite (motor, HIGH);
  delay (6000);
    digitalWrite (valve, HIGH);
  delay (2000);
  }
  else
  {
    digitalWrite (motor, HIGH);
    digitalWrite (valve, HIGH);
  }

   display.clearDisplay();
   display.setCursor(0, 0);
   display.setTextSize(1);
   display.println(&quot;Temperature&quot;);   
   display.setCursor(0, 30);
   display.println(&quot;Soil &amp; Water Engg.&quot;);
   display.setTextSize(2);
   display.setCursor(0,10);
   display.println(temp);
   display.setCursor(75, 10);
   display.println(&quot;C&quot;);
   display.setCursor(0,40);
     display.display();
   
delay (2000);
 }
</code></pre>
 4143: <p>You need to think about this with a real-world analogue.</p>
<p>I like to use boiling an egg...</p>
<p>There are three ways you can boil an egg:</p>
<h2>Blocking operation</h2>
<ol>
<li>Put the egg in the boiling water</li>
<li>Stand there staring at it for three minutes or so</li>
<li>Take the egg out of the water and eat it</li>
</ol>
<h2>Timer operation</h2>
<ol>
<li>Put the egg in the boiling water</li>
<li>Set a timer for 3 minutes time</li>
<li>Go off and do something else</li>
<li>When the alarm goes off take the egg out and eat it</li>
</ol>
<h2>Time polling</h2>
<ol>
<li>Put the egg in the boiling water and record the current time</li>
<li>Go off and do something else, every so often glancing at your watch and comparing the time to the time you started</li>
<li>When three minutes have passed take the egg out and eat it</li>
</ol>
<p>Currently you are using the <em>blocking</em> operation where you're stood staring at the egg until it's time to do something.  You need to change your code to use one of the other methods.</p>
<p>Most times the <em>time polling</em> method is good enough. You may get some <em>jitter</em> where whatever else it is you are doing causes a delay before you get to glance at your watch, but that is rarely an issue. For greater precision (better for higher frequency checking) you use a timer which triggers an interrupt so you always do things at more precise intervals.</p>
<p>There is an example in the IDE called <code>BlinkWithoutDelay</code> which implements the time polling in a very simplistic way - record the time, then compare that time and the current time, and when it's elapsed change the LED's state, recording a new time.</p>
