Using milli () delay instead of delay

#include <Animately.h>
#include <Core/Timeline.h>

#include <Adafruit_Sensor.h>

#include <LiquidCrystal_I2C.h>

#include <Servo.h> // servo library
#include <DHT.h> 

LiquidCrystal_I2C lcd(0x27, 16, 2); // A5 to SCL , A4 to SDA

#define LED1 2// 
#define LED2 3 //
#define LED3 4 // MOTOR LED 
#define LED4 5
#define RELAY 6 // motor relay high = off , low = on 
#define DHTPIN1 7 
#define BUZZER 8 
#define SERVOMOTOR 9
#define TRIG 10 
#define ECHO 11 
#define SOILMOIST1 A0  
#define RAINFALL A1  

DHT HT (DHTPIN1, DHT11); // creating the object 


Servo myservo;  

//define global varibles used for timing 
const long eventInterval_LED1 = 1000; 
const long eventInterval_LED2 = 1000;  
const long eventInterval_LED3 = 1000; 
const long eventInterval_LED4 = 1000; 

unsigned long previousTime_1 =0;  
unsigned long previousTime_2 = 0; 
unsigned long previousTime_3 =0; 
unsigned long previousTime_4 =0;



// define gloabl varibales for system parameters 
float tankLvlCM;
float duration_us; 
float soilMos1;  
float tempC; 
float rainfallVal; 

int motorpos = 0; 
int minwatertankLvl= 16; // THE HEIGHT OF THE TANK IS 20 CM SO and dont want water to get below 4 cm so when the sensor see that is more tha n 16 cm measure water level to low 
int rainfallThreashhold= 50; 
int soilMoisturemin = 25; // when measured mositure is above this value then action 



void setup() {
  // put your setup code here, to run once:
Serial.begin(115200); // Initialise Serial debug output
  // put your setup code here, to run once: 
HT.begin(); //Initialise the DHT sensor , starting the object 
//delay(500);

pinMode(LED1, OUTPUT); 
pinMode(LED2, OUTPUT); 
pinMode(LED3, OUTPUT); 
pinMode(LED4, OUTPUT);
pinMode(RELAY,OUTPUT); 
pinMode(BUZZER,OUTPUT);  
pinMode(RAINFALL,INPUT);      
pinMode(TRIG, OUTPUT);                  // Relay Module PIN D8
pinMode(ECHO, INPUT);
                             
myservo.attach(SERVOMOTOR);                                   //  Servo PIN D9

digitalWrite(RELAY, LOW);                           // Relay Normally High for OFF condition
digitalWrite(TRIG, LOW);                          // trig normally low 
digitalWrite (BUZZER, LOW);      // buzzer pin normally low 
             

lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("SMART IRRAGATION SYSTEM ");
lcd.setCursor(0, 1);
lcd.print("IS ACTIVATED!");
lcd.clear();

}

void loop() {
  // put your main code here, to run repeatedly: 
  unsigned long currentTime= millis(); 
  measureDistance() ; 
  indiLED1();
  measureTemperature (); 
  indiLED2();
  measureSoilmositure(); 
  indiLED3();
  measureRainfall (); 
  indiLED4();

if (tankLvlCM < minwatertankLvl ) 
{ 
  if (rainfallVal < rainfallThreashhold ) 
    { 
      if (soilMos1< soilMoisturemin) { 
        
        if (tempC >= 4.4 && tempC < 21) {
        waterpumplowtime(); 
        } 

        else if ( tempC >= 21) {
        waterpumphightime();
        } 

        else {
          return; // return back to the start of void loop to get new measurement values 
        }

      }
      else 
      {
        return; // return back to the start of void loop to get new measurement values 
      }
    }
  else 
    {
      return; // return back to the start of void loop to get new measurement values  
    }

}  

else {   
 lowWaterlevelnotify();   
} 

return; 
} 

//declaring indicating led functions 
void indiLED1(){ 
// using millis to create a delay of 8 seconds 
  digitalWrite (LED1,HIGH ); 
  Serial.print("distance: ");
  Serial.print(tankLvlCM);
  Serial.println("cm"); 
  lcd.setCursor(0,0); 
  lcd.print("Current Water"); 
  lcd.setCursor(0,1); 
  lcd.print("Level: "); 
  lcd.setCursor(8,1); 
  lcd.print("tankLvlCM"); 
  delay(2000);
  lcd.clear();
  digitalWrite (LED1,LOW );

} 
void indiLED2(){ 
  digitalWrite (LED2,HIGH );
  lcd.setCursor(0,0); 
  lcd.print("Temp: ");
  lcd.print(tempC);
  lcd.print((char)223);
  lcd.print("C"); 
  Serial.println("temperature:");
  Serial.println(tempC); 
  delay(2000); // here i want to delay the system so that the lcd stays on for 5 seconds and the text printed on the lcd stays on for 5 seconds ,
  lcd.clear(); 
  digitalWrite (LED2,LOW ); 
} 
void indiLED3(){ 
  digitalWrite (LED3,HIGH );
  lcd.setCursor(0,0); 
  lcd.print("Soil Moisture:"); 
  lcd.setCursor(0,1); 
  lcd.print(soilMos1); 
  //lcd.print("%");
  Serial.println(soilMos1); 
  delay(2000); // here i want to delay the system so that the lcd stays on for 5 seconds and the text printed on the lcd stays on for 5 seconds ,
  lcd.clear(); 
  digitalWrite (LED3,LOW); 
} 
void indiLED4(){ 
  digitalWrite (LED4,HIGH);
  lcd.setCursor(0,0); 
  lcd.print("Rainfall Level: "); 
  lcd.setCursor(0,1);
  lcd.print(rainfallVal);
  Serial.println(rainfallVal);
  delay(2000); // here i want to delay the system so that the lcd stays on for 5 seconds and the text printed on the lcd stays on for 5 seconds ,
  lcd.clear();
  digitalWrite (LED4,LOW);
} 

void measureDistance() {
  // this first measurment is getting distance of the water level in the water tank
  digitalWrite (LED1, HIGH );
  digitalWrite ( TRIG, HIGH ); // getting distance of the sensor
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  duration_us = pulseIn(ECHO, HIGH);
  tankLvlCM == 0.017 * duration_us;
} 
//declaring 
void measureTemperature () {
tempC= HT.readTemperature(DHTPIN1); 
} 

void measureSoilmositure () {
  soilMos1= analogRead(SOILMOIST1); 
  soilMos1 = map(soilMos1,0,1024,0,100);
} 

void measureRainfall () { 
  rainfallVal = analogRead(RAINFALL); 
  rainfallVal = map(rainfallVal, 0, 1023, 0, 100);
} 

void lowWaterlevelnotify(){ 
  do {
    digitalWrite (BUZZER, HIGH); 
    lcd.setCursor(0,0);
    lcd.print("Water Tank Level to low ! "); 
  }while (tankLvlCM >= minwatertankLvl); 
} 

void waterpumplowtime(){
  digitalWrite(RELAY, HIGH); //relay goes high so that the water pump turns on 
          lcd.setCursor(0,0); 
          lcd.print("MOTOR STATUS:ON"); 
          // add in a timer herefor 15 sec 
            for (motorpos = 0; motorpos <= 180; motorpos += 1) 
            { // goes from 0 degrees to 180 degrees // in steps of 1 degree
            myservo.write(motorpos);              // tell servo to go to position in variable 'pos'
            delay(15);                       // waits 15ms for the servo to reach the position
            } 
            for (motorpos = 180; motorpos >= 0; motorpos -= 1) { // goes from 180 degrees to 0 degrees
            myservo.write(motorpos);              // tell servo to go to position in variable 'pos'
            delay(15);                       // waits 15ms for the servo to reach the position
            }
         
            digitalWrite(RELAY, LOW);
}

void waterpumphightime() {
    digitalWrite(RELAY, HIGH); //relay goes high so that the water pump turns on 
          lcd.setCursor(0,0); 
          lcd.print("MOTOR STATUS:ON"); 
          // add in a timer here for 30 seconds 
            for (motorpos = 0; motorpos <= 180; motorpos += 1) 
            { // goes from 0 degrees to 180 degrees // in steps of 1 degree
            myservo.write(motorpos);              // tell servo to go to position in variable 'pos'
            delay(15);                       // waits 15ms for the servo to reach the position
            } 
            for (motorpos = 180; motorpos >= 0; motorpos -= 1) { // goes from 180 degrees to 0 degrees
            myservo.write(motorpos);              // tell servo to go to position in variable 'pos'
            delay(15);                       // waits 15ms for the servo to reach the position
            }
         
            digitalWrite(RELAY, LOW);
}


in my code in each indicating led function i call i use a large delay which i think is messing up my system , i want to try to use the millis () function and see if that would help but im having trouble trying to implement it iinto my code

Look at File|Examples|02.Digital|BlinkWithoutDelay to use millis() for timing instead of delay().

#include <Animately.h>
#include <Core/Timeline.h>

#include <Adafruit_Sensor.h>

#include <LiquidCrystal_I2C.h>

#include <Servo.h> // servo library
#include <DHT.h> 

LiquidCrystal_I2C lcd(0x27, 16, 2); // A5 to SCL , A4 to SDA

#define LED1 2// 
#define LED2 3 //
#define LED3 4 // MOTOR LED 
#define LED4 5
#define RELAY 6 // motor relay high = off , low = on 
#define DHTPIN1 7 
#define BUZZER 8 
#define SERVOMOTOR 9
#define TRIG 10 
#define ECHO 11 
#define SOILMOIST1 A0  
#define RAINFALL A1  

DHT HT (DHTPIN1, DHT11); // creating the object 


Servo myservo;  

//define global varibles used for timing 
const long eventInterval_LED1 = 1000; 
const long eventInterval_LED2 = 1000;  
const long eventInterval_LED3 = 1000; 
const long eventInterval_LED4 = 1000; 

unsigned long previousTime_1 =0;  
unsigned long previousTime_2 = 0; 
unsigned long previousTime_3 =0; 
unsigned long previousTime_4 =0;



// define gloabl varibales for system parameters 
float tankLvlCM;
float duration_us; 
float soilMos1;  
float tempC; 
float rainfallVal; 

int motorpos = 0; 
int minwatertankLvl= 16; // THE HEIGHT OF THE TANK IS 20 CM SO and dont want water to get below 4 cm so when the sensor see that is more tha n 16 cm measure water level to low 
int rainfallThreashhold= 50; 
int soilMoisturemin = 25; // when measured mositure is above this value then action 



void setup() {
  // put your setup code here, to run once:
Serial.begin(115200); // Initialise Serial debug output
  // put your setup code here, to run once: 
HT.begin(); //Initialise the DHT sensor , starting the object 
//delay(500);

pinMode(LED1, OUTPUT); 
pinMode(LED2, OUTPUT); 
pinMode(LED3, OUTPUT); 
pinMode(LED4, OUTPUT);
pinMode(RELAY,OUTPUT); 
pinMode(BUZZER,OUTPUT);  
pinMode(RAINFALL,INPUT);      
pinMode(TRIG, OUTPUT);                  // Relay Module PIN D8
pinMode(ECHO, INPUT);
                             
myservo.attach(SERVOMOTOR);                                   //  Servo PIN D9

digitalWrite(RELAY, LOW);                           // Relay Normally High for OFF condition
digitalWrite(TRIG, LOW);                          // trig normally low 
digitalWrite (BUZZER, LOW);      // buzzer pin normally low 
             

lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("SMART IRRAGATION SYSTEM ");
lcd.setCursor(0, 1);
lcd.print("IS ACTIVATED!");
lcd.clear();

}

void loop() {
  // put your main code here, to run repeatedly: 
  unsigned long currentTime= millis(); 
  measureDistance() ; 
  indiLED1();
  measureTemperature (); 
  indiLED2();
  measureSoilmositure(); 
  indiLED3();
  measureRainfall (); 
  indiLED4(); 

if (tankLvlCM >= minwatertankLvl ) 
{ 
  lowWaterlevelnotify() ;
} 

else if (rainfallVal > rainfallThreashhold{
  return ; 
} 

else if (soilMos1 > soilMoisturemin) {
  return ; 
}

else if (tempC >= 4.4 && tempC < 21 &&  ) {
 waterpumplowtime();  
} 

else if ( tempC >= 21) {
  waterpumphightime();
  } 

} 

//declaring indicating led functions 
void indiLED1(){ 
// using millis to create a delay of 8 seconds 
  digitalWrite (LED1,HIGH ); 
  Serial.print("distance: ");
  Serial.print(tankLvlCM);
  Serial.println("cm"); 
  lcd.setCursor(0,0); 
  lcd.print("Current Water"); 
  lcd.setCursor(0,1); 
  lcd.print("Level: "); 
  lcd.setCursor(8,1); 
  lcd.print("tankLvlCM"); 
  delay(2000);
  lcd.clear();
  digitalWrite (LED1,LOW );

} 
void indiLED2(){ 
  digitalWrite (LED2,HIGH );
  lcd.setCursor(0,0); 
  lcd.print("Temp: ");
  lcd.print(tempC);
  lcd.print((char)223);
  lcd.print("C"); 
  Serial.println("temperature:");
  Serial.println(tempC); 
  delay(2000); // here i want to delay the system so that the lcd stays on for 5 seconds and the text printed on the lcd stays on for 5 seconds ,
  lcd.clear(); 
  digitalWrite (LED2,LOW ); 
} 
void indiLED3(){ 
  digitalWrite (LED3,HIGH );
  lcd.setCursor(0,0); 
  lcd.print("Soil Moisture:"); 
  lcd.setCursor(0,1); 
  lcd.print(soilMos1); 
  //lcd.print("%");
  Serial.println(soilMos1); 
  delay(2000); // here i want to delay the system so that the lcd stays on for 5 seconds and the text printed on the lcd stays on for 5 seconds ,
  lcd.clear(); 
  digitalWrite (LED3,LOW); 
} 
void indiLED4(){ 
  digitalWrite (LED4,HIGH);
  lcd.setCursor(0,0); 
  lcd.print("Rainfall Level: "); 
  lcd.setCursor(0,1);
  lcd.print(rainfallVal);
  Serial.println(rainfallVal);
  delay(2000); // here i want to delay the system so that the lcd stays on for 5 seconds and the text printed on the lcd stays on for 5 seconds ,
  lcd.clear();
  digitalWrite (LED4,LOW);
} 

void measureDistance() {
  // this first measurment is getting distance of the water level in the water tank
  digitalWrite (LED1, HIGH );
  digitalWrite ( TRIG, HIGH ); // getting distance of the sensor
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  duration_us = pulseIn(ECHO, HIGH);
  tankLvlCM == 0.017 * duration_us;
} 
//declaring 
void measureTemperature () {
tempC= HT.readTemperature(DHTPIN1); 
} 

void measureSoilmositure () {
  soilMos1= analogRead(SOILMOIST1); 
  soilMos1 = map(soilMos1,0,1024,0,100);
} 

void measureRainfall () { 
  rainfallVal = analogRead(RAINFALL); 
  rainfallVal = map(rainfallVal, 0, 1023, 0, 100);
} 

void lowWaterlevelnotify(){ 
  do {
    digitalWrite (BUZZER, HIGH); 
    lcd.setCursor(0,0);
    lcd.print("Water Tank Level to low ! "); 
  }while (tankLvlCM >= minwatertankLvl); 
} 

void waterpumplowtime(){
  digitalWrite(RELAY, HIGH); //relay goes high so that the water pump turns on 
          lcd.setCursor(0,0); 
          lcd.print("MOTOR STATUS:ON"); 
          // add in a timer herefor 15 sec 
            for (motorpos = 0; motorpos <= 180; motorpos += 1) 
            { // goes from 0 degrees to 180 degrees // in steps of 1 degree
            myservo.write(motorpos);              // tell servo to go to position in variable 'pos'
            delay(15);                       // waits 15ms for the servo to reach the position
            } 
            for (motorpos = 180; motorpos >= 0; motorpos -= 1) { // goes from 180 degrees to 0 degrees
            myservo.write(motorpos);              // tell servo to go to position in variable 'pos'
            delay(15);                       // waits 15ms for the servo to reach the position
            }
         
            digitalWrite(RELAY, LOW);
}

void waterpumphightime() {
    digitalWrite(RELAY, HIGH); //relay goes high so that the water pump turns on 
          lcd.setCursor(0,0); 
          lcd.print("MOTOR STATUS:ON"); 
          // add in a timer here for 30 seconds 
            for (motorpos = 0; motorpos <= 180; motorpos += 1) 
            { // goes from 0 degrees to 180 degrees // in steps of 1 degree
            myservo.write(motorpos);              // tell servo to go to position in variable 'pos'
            delay(15);                       // waits 15ms for the servo to reach the position
            } 
            for (motorpos = 180; motorpos >= 0; motorpos -= 1) { // goes from 180 degrees to 0 degrees
            myservo.write(motorpos);              // tell servo to go to position in variable 'pos'
            delay(15);                       // waits 15ms for the servo to reach the position
            }
         
            digitalWrite(RELAY, LOW);
}

will try the two things at once , i also redesigned the condition statements to make it simplier i know using state machines would be more efficient but would this still work ?

The purpose of state machines is not to make things more efficient. It's to prevent tasks from blocking other tasks.

In neither the first nor subsequent post, did you explain what the problem is exactly, other than an indirect reference to a long delay. That is why you were sent to look at non-blocking code.

If you don't know whether your code works or not, why not test it?

See his other thread.

duplicate post, flagged for moderation
https://forum.arduino.cc/t/jumping-back-to-loop-in-a-loop-of-a-loop/1091259/39

fair okay so my issue is when i run this code my lcd doesnt print anything but my leds blink at the delay time i set and the code compiles but im not sure why the lcd isnt printing anything and i thought it was because of the delays im using even though in theory shouldnt the delay work ?

Sorry, back-of-the-line. Already flagged, but admittedly should have said so.

1 Like

so i got flagged for asking for help twice ?

Yes, but about the same issue.

It's more because you've been pointed at the "how to get the best from this forum", and aren't really paying attention. We try to keep all details of one project in one thread, because that's about the only way to keep the information together. Multiple threads all head off in different directions. Do you really want multiple concurrent questions taking you in different design directions, with likely conflicting code design options and considerations? You're having a hard enough time keeping your head above water in one thread, Jack. Not being cruel, man, but you really need to stay in one box.
Just my 2 cents, toss in the dirt if you want.

When someone is donating their time to help you learn it is very rude to start a new post asking for the same of help.

We do not have to help new users as we volunteer here give you the skills so you can fish for your own food.

okay fair i see where everyone is coming from my bad , thanks for trying to help me .

#include <Adafruit_Sensor.h>

#include <LiquidCrystal_I2C.h>

#include <Servo.h> // servo library
#include <DHT.h>


LiquidCrystal_I2C lcd(0x27, 16, 2); // A5 to SCL , A4 to SDA
// led green red white yellow blue 
//const int LED_PIN = 3;  
#define LED1 2// 
#define LED2 3 //
#define LED3 4 // MOTOR LED 
#define LED4 5
#define RELAY 6 // motor relay high = off , low = on 
#define DHTPIN1 7 //digital 
//#define DHTPIN2 //4 

//#define DHTPIN2 D11
#define SERVOMOTOR 9 
#define BUZZER 8
#define TRIG 10 
#define ECHO 11 


#define SOILMOISTA1 A0
//#define SOILMOIST2 A1   
#define RAINFALL A1  


DHT HT (DHTPIN1, DHT11); // creating the object 
//DHT HT (DHTPIN2, DHT11); // creating the object , second dht 11 
//DHT_1 

Servo myservo;  

float tankLvlCM ;
float duration_us; 


float minimumTanklvl; // = 4 cm 
int rainfallThreashold; 
int soilMoisturemin; //= 25

//int soilMos2; 
//int soilMosTot; // if using two soil sensors
float soilMos1; 
int motorpos; 
float tempC;
float tempF;  
float rainfallVal ; 


void setup()
 { 
Serial.begin(115200); // Initialise Serial debug output
  // put your setup code here, to run once: 
HT.begin(); //Initialise the DHT sensor , starting the object 
delay(500);

pinMode (TRIG,OUTPUT); 
pinMode (ECHO, INPUT); 

pinMode(LED1, OUTPUT); 
pinMode(LED2, OUTPUT); 
pinMode(LED3, OUTPUT); 
pinMode(LED4, OUTPUT);
pinMode(RELAY,OUTPUT); 
pinMode (BUZZER,OUTPUT);  
pinMode(SOILMOISTLED, OUTPUT);


pinMode(moisturePin1, INPUT_PULLUP);           // Soil Moisture Sensor 1 PIN A0
//pinMode(moisturePin2,INPUT_PULLUP);
//pinMode(LIGHTSENSOR,INPUT);    
pinMode(RAINFALL,INPUT);      

pinMode(TRIG, OUTPUT);                  // Relay Module PIN D8
pinMode(ECHO, INPUT);
                             
myservo.attach(9);                                   //  Servo PIN D9

digitalWrite(RELAY, LOW);                           // Relay Normally High for OFF condition
digitalWrite(TRIG, LOW);                          // trig normally low 
digitalwrite (BUZZER, LOW);      // buzzer pin normally low 
digitalWrite(SOILMOISTLED, LOW);             

lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("SMART IRRAGATION SYSTEM ");
lcd.setCursor(0, 1);
lcd.print("IS ACTIVATED!");
delay(5000);
lcd.clear();

}

void loop()  // put your main code here, to run repeatedly:
{ 
int minwatertankLvl= 16 // THE HEIGHT OF THE TANK IS 20 CM SO and dont want water to get below 4 cm so when the sensor see that is more tha n 16 cm measure water level to low 
int rainfallThreashhold= 50 ; 
int soilmoistureThreashold = 25 ; // when measured mositure is above this value then action 
 

// No.	Crop Name	Temperature	Moisture (%)	Humidity(%)
//	Rice	21-37	20-25	60-80
//	Wheat	10-15	14-20	60-70
//	Bajra	20-38	20-25	55-70 
digitalWrite (LED1,HIGH ); 
digitalWrite ( TRIG, HIGH ); // getting distance of the sensor 
delayMicroseconds(10); 
digitalWrite(TRIG, LOW); 
duration_us = pulseIn(echoPin, HIGH); 
tankLvlCM == 0.017 * duration_us; 
Serial.print("distance: ");
Serial.print(distance_cm);
Serial.println(" cm"); 
lcd.setCursor(0,0); 
lcd.print("Current Water"); 
lcd.setCursor(0,1); 
lcd.print("Level: "); 
lcd.setCursor(8,1); 
lcd.print("tankLvlCM"); 
delay(200);  
lcd.clear();
digitalWrite (LED1,LOW );


tempC= HT.readTemperature(DHTPIN); 
  digitalWrite (LED2,HIGH );
  lcd.setCursor(0,0); 
  lcd.print("Temp: ");
  lcd.print(DHT.temperature);
  lcd.print((char)223);
  lcd.print("C"); 
  serial.println("temp C  : ");
  serial.println(tempC);
  delay(2000); 
  lcd.clear(); 
  digitalWrite (LED2,LOW );

tempF=HT.readTemperature(true); 
  serial.println("Temp F: ");
  serial.println(tempF); 
  delay(2000);  
  lcd.clear();

soilMos1= analogRead(SOILMOIST1); 
  soilMos1 = map(output_value,0,1024,0,100); // maps mositure level to a range of 0-100 percent 
  digitalWrite (LED3,HIGH );
  lcd.setCursor(0,0); 
  lcd.print("Soil Moisture: "); 
  lcd.setCursor(0,1); 
  lcd.print(soilMos1); 
  lcd.print("%");
  Serial.println(soilMos1);
  delay(2000); 
  lcd.clear(); 
  digitalWrite (LED3,LOW);

//soilMos2= analogRead(SOILMOIST2);                       // Soil Moisture Sensor 1 PIN A0
//Serial.println(soilMos2);
//delay(100); 
//soilMosTot = ((soilMos1+soilMos2)/2); // if using two soil sensors 
//Serial.println(soilMosTot);
//delay(100); 
rainfallVal = analogRead(rainfall); 
  rainfallVal = map(rainfallVal, 0, 1023, 0, 100); //The function map maps the value of the rain sensor from the output pin, and assigns a value to the variable, ranging from 0 to 225
  digitalWrite (LED4,HIGH );
  lcd.setCursor(0,0); 
  lcd.print("Rainfall Level: "); 
  lcd.setCursor(0,1);
  lcd.print(rainfallVal);
  Serial.println(rainfallVal);
  delay(2000); 
  lcd.clear();
  digitalWrite (LED4,LOW);
  
  

lcd.autoscroll();




// decsion statements 
if (tankLvl < minwatertankLvl ) 
{ 
  if (rainfallVal < rainfallThreashold ) 
    { 
      if (soilMos1< soilMoisturemin) { 
        
        if (tempC >= 4.4 && tempC < 21) {
          pump water , relay pin high 
        } 

        else if ( tempC >= 21) {
          pump water , relay pin high 
        } 

        else {
          return to void loop , return to the start of the program at getting the values 
        }

      }
      else 
      {
        return back to loop 
      }
    }
  else 
    {
      return back to the start of void loop 
    }





}  

else {   
  while (tankLvl >= minwatertankLvl) 
    {
    digitalWrite (BUZZER, HIGH); 
    lcd.setCursor(0,0);
    lcd.print("Water Tank Level to low ! "); 
    } 
    
 return to the start of the mian loop, where we get the measurments  

} 


} // end of main program  
      


hey everyone
im wondering how i would jump back to the start of void loop in my if else statements, would i use : loop(); ?

return;

so if if i put return that would bring me back to the start of void loop where i get my sensor measurements ?

Try it you’ll like it :sunglasses:

However, there are many ways to avoid this with external functions Flags etc.


We have no way what you are trying to do.
:thinking:

1 Like

im basically making an irrigation system , so when the measurements dont meet the conditions in the else if statements then it returns back to the start of void loop which is getting the measurements of the plants environment

ive also attached a flow chart to

I like flow charts.

I haven’t look closely at the sketch but if I were doing similar I would be using State Machines.

BTW
Avoid using delay( ) as it stops other code execution for this time period.

Also, while( ) loops can get you into trouble if they don’t have a way out.