Sim800l sending sms constantly terminating the programe loop

hi every one there
i have made a code for smart irrigation system using mega and sim800l. the problem i faced is that the module constantly sends sms. the thing i actually want is that the module sends an sms only once a condition is detected and affter sending the sms the program works properly

#include<Sim800L.h>
#include<LiquidCrystal.h>
#include<SoftwareSerial.h>
String str = "";
Sim800L Sim800L;
SoftwareSerial myserial(50, 51);
LiquidCrystal lcd(10, 11, 7, 6, 5, 4);
const int SoMS1 = 22;
const int SoMS2 = 23;
const int SoMS3 = 24;
const int SoMS4 = 25;
const int RDpS = 26;
const int mtr1 = 36;
const int mtr2 = 37;
const int mtr3 = 38;
const int mtr4 = 39;
const int led1 = 40;
const int led2 = 41;
const int led3 = 42;
const int led4 = 43;

void setup()
{
  myserial.begin(9600);
  Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.print("Water Irrigaton");
  lcd.setCursor(2, 1);
  delay(2000);
  lcd.clear();
  lcd.print("UCET");
  lcd.setCursor(0, 1);
  lcd.print("Welcomes You");
  delay(2000);
  gsmInit();
  lcd.clear();
  lcd.print("System Ready");
  delay(1000);
  lcd.clear();
  lcd.print("SMART IRRIGATION");
  lcd.setCursor(2, 1);
  lcd.print("...SYSTEM...");
  delay(2000);
  lcd.clear();
  lcd.print("PRESENTED BY");
  lcd.setCursor(2, 1);
  lcd.print("BSC ELECTRICAL");
  delay(2000);
  lcd.clear();
  lcd.print("GROUP MEMBERS");
  lcd.setCursor(2, 1);
  lcd.print("---------------");
  delay(2000);
  lcd.clear();
  lcd.print("IKRAM UL HAQ");
  lcd.setCursor(2, 1);
  lcd.print("BEEF15M003");
  delay(2000);
  lcd.clear();
  lcd.print("BEHZAD ASGHAR");
  lcd.setCursor(2, 1);
  lcd.print("BEEF15M026");
  delay(2000);
  lcd.clear();
  lcd.print("TALHA HAIDER");
  lcd.setCursor(2, 1);
  lcd.print("BEEF15M039");
  delay(2000);
  lcd.clear();
  pinMode(SoMS1, INPUT_PULLUP);
  pinMode(SoMS2, INPUT_PULLUP);
  pinMode(SoMS3, INPUT_PULLUP);
  pinMode(SoMS4, INPUT_PULLUP);;
  pinMode(RDpS, INPUT_PULLUP);
  pinMode(mtr1, OUTPUT);
  pinMode(mtr2, OUTPUT);
  pinMode(mtr3, OUTPUT);
  pinMode(mtr4, OUTPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  digitalWrite(mtr1, LOW);
  digitalWrite(mtr2, LOW);
  digitalWrite(mtr3, LOW);
  digitalWrite(mtr4, LOW);
}
void loop()
{
  if (digitalRead(SoMS1)== LOW)
  { digitalWrite(led1, HIGH);
    digitalWrite(mtr1, HIGH);
    lcd.print("LOW MOISTURE1");
    lcd.setCursor(2,1);
    lcd.print("MOTOR1 ON");
    delay(1000);
    lcd.clear();
    sendSMS("LOW SOIL MOISTURE1 MOTOR 1 ON");
    
  }
  if (digitalRead(SoMS1)== HIGH)
  {
    digitalWrite(led1, LOW);
    digitalWrite(mtr1, LOW);
    lcd.print("HIGH MOISTURE1");
    lcd.setCursor(2,1);
    lcd.print("MOTOR1 OFF");
    delay(1000);
    lcd.clear();
    sendSMS("NORMAL MOISTURE MOTOR1 OFF");
  }
  if (digitalRead(SoMS2) ==LOW)
  { digitalWrite(led2, HIGH);
    digitalWrite(mtr2, HIGH);
    lcd.print("LOW MOISTURE2");
    lcd.setCursor(2,1);
    lcd.print("MOTOR2 ON");
    delay(1000);
    lcd.clear();
    sendSMS("LOW SOIL MOISTURE2 MOTOR 2 ON");
  }
  if (digitalRead(SoMS2)== HIGH)
  {
    digitalWrite(led2, LOW);
    digitalWrite(mtr2, LOW);
    lcd.print("HIGH MOISTURE2");
    lcd.setCursor(2,1);
    lcd.print("MOTOR2 OFF");
    delay(1000);
    lcd.clear();
    sendSMS("NORMAL SOIL MOISTURE2 MOTOR 2 OFF");
  }
   if (digitalRead(SoMS3)== LOW)
  { digitalWrite(led3, HIGH);
    digitalWrite(mtr3, HIGH);
    lcd.print("LOW MOISTURE3");
    lcd.setCursor(2,1);
    lcd.print("MOTOR3 ON");
    delay(1000);
    lcd.clear();
    sendSMS("LOW SOIL MOISTURE3 MOTOR 3 ON");
  }
  if (digitalRead(SoMS3)== HIGH)
  {
    digitalWrite(led3, LOW);
    digitalWrite(mtr3, LOW);
    lcd.print("HIGH MOISTURE3");
    lcd.setCursor(2,1);
    lcd.print("MOTOR3 OFF");
    delay(1000);
    lcd.clear();
    sendSMS("NORMAL SOIL MOISTURE3 MOTOR 3 OFF");
  }
   if (digitalRead(SoMS4)== LOW)
  { digitalWrite(led4, HIGH);
    digitalWrite(mtr4, HIGH);
    lcd.print("LOW MOISTURE4");
    lcd.setCursor(2,1);
    lcd.print("MOTOR4 ON");
    delay(1000);
    lcd.clear();
    sendSMS("LOW SOIL MOISTURE4 MOTOR 4 ON");
  }
   if (digitalRead(SoMS4)== HIGH)
  {
    digitalWrite(led4, LOW);
    digitalWrite(mtr4, LOW);
    lcd.print("HIGH MOISTURE4");
    lcd.setCursor(2,1);
    lcd.print("MOTOR4 OFF");
    delay(1000);
    lcd.clear();
    sendSMS("NORMAL SOIL MOISTURE4 MOTOR 4 OFF");
  }
   if (digitalRead(RDpS)== HIGH)
  { digitalWrite(led1, LOW);
    digitalWrite(mtr1, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(mtr2, LOW);
    digitalWrite(led3, LOW);
    digitalWrite(mtr3, LOW);
    digitalWrite(led4, LOW);
    digitalWrite(mtr4, LOW);
 
  }
  else
    { digitalWrite(led1, LOW);
    digitalWrite(mtr1, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(mtr2, LOW);
    digitalWrite(led3, LOW);
    digitalWrite(mtr3, LOW);
    digitalWrite(led4, LOW);
    digitalWrite(mtr4, LOW);
  }
}
void sendSMS(String msg)
{
lcd.clear();
lcd.print("Sending SMS");
myserial.println("AT+CMGF=1");
delay(500);
myserial.print("AT+CMGS=");
myserial.print('"');
myserial.print("+923411046117");// number
myserial.print('"');
myserial.println();
delay(500);
myserial.println(msg);
delay(500);
myserial.write(26);
delay(1000);
lcd.clear();
lcd.print("SMS Sent");
delay(1000);
lcd.begin(16, 2);
}
void gsmInit()
{
lcd.clear();
lcd.print("Finding Module..");
boolean at_flag = 1;
while (at_flag)
{
myserial.println("AT");
while (myserial.available() > 0)
{
if (myserial.find("OK"))
at_flag = 0;
}
delay(1000);
}
myserial.println("ATE0");
lcd.clear();
lcd.print("Finding Network..");
boolean net_flag = 1;
while (net_flag)
{
myserial.println("AT+CPIN?");
while (myserial.available() > 0)
{
if (myserial.find("READY"))
net_flag = 0;
break; 
}
delay(1000); 
}
myserial.println("AT+CNMI=2,2,0,0,0");
delay(1000);
myserial.println("AT+CMGF=1");
delay(1000);
myserial.println("AT+CSMP=17,167,0,0");
lcd.clear();
myserial.flush();
}

try this

#include<Sim800L.h>
#include<LiquidCrystal.h>
#include<SoftwareSerial.h>
String str = "";
Sim800L Sim800L;
SoftwareSerial myserial(50, 51);
LiquidCrystal lcd(10, 11, 7, 6, 5, 4);
const int SoMS1 = 22;
const int SoMS2 = 23;
const int SoMS3 = 24;
const int SoMS4 = 25;
const int RDpS = 26;
const int mtr1 = 36;
const int mtr2 = 37;
const int mtr3 = 38;
const int mtr4 = 39;
const int led1 = 40;
const int led2 = 41;
const int led3 = 42;
const int led4 = 43;
bool a=1,b=1,c=1,d=1,e=1,f=1,g=1,h=1;

void setup()
{
  myserial.begin(9600);
  Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.print("Water Irrigaton");
  lcd.setCursor(2, 1);
  delay(2000);
  lcd.clear();
  lcd.print("UCET");
  lcd.setCursor(0, 1);
  lcd.print("Welcomes You");
  delay(2000);
  gsmInit();
  lcd.clear();
  lcd.print("System Ready");
  delay(1000);
  lcd.clear();
  lcd.print("SMART IRRIGATION");
  lcd.setCursor(2, 1);
  lcd.print("...SYSTEM...");
  delay(2000);
  lcd.clear();
  lcd.print("PRESENTED BY");
  lcd.setCursor(2, 1);
  lcd.print("BSC ELECTRICAL");
  delay(2000);
  lcd.clear();
  lcd.print("GROUP MEMBERS");
  lcd.setCursor(2, 1);
  lcd.print("---------------");
  delay(2000);
  lcd.clear();
  lcd.print("IKRAM UL HAQ");
  lcd.setCursor(2, 1);
  lcd.print("BEEF15M003");
  delay(2000);
  lcd.clear();
  lcd.print("BEHZAD ASGHAR");
  lcd.setCursor(2, 1);
  lcd.print("BEEF15M026");
  delay(2000);
  lcd.clear();
  lcd.print("TALHA HAIDER");
  lcd.setCursor(2, 1);
  lcd.print("BEEF15M039");
  delay(2000);
  lcd.clear();
  pinMode(SoMS1, INPUT_PULLUP);
  pinMode(SoMS2, INPUT_PULLUP);
  pinMode(SoMS3, INPUT_PULLUP);
  pinMode(SoMS4, INPUT_PULLUP);;
  pinMode(RDpS, INPUT_PULLUP);
  pinMode(mtr1, OUTPUT);
  pinMode(mtr2, OUTPUT);
  pinMode(mtr3, OUTPUT);
  pinMode(mtr4, OUTPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  digitalWrite(mtr1, LOW);
  digitalWrite(mtr2, LOW);
  digitalWrite(mtr3, LOW);
  digitalWrite(mtr4, LOW);
}
void loop()
{
  if (digitalRead(SoMS1)== LOW)
  { digitalWrite(led1, HIGH);
    digitalWrite(mtr1, HIGH);
    lcd.print("LOW MOISTURE1");
    lcd.setCursor(2,1);
    lcd.print("MOTOR1 ON");
    delay(1000);
    lcd.clear();
    sendSMS("LOW SOIL MOISTURE1 MOTOR 1 ON"); a=0 ;b=1;
   
  }
  if (digitalRead(SoMS1)== HIGH)
  {
    digitalWrite(led1, LOW); a = 1;
    digitalWrite(mtr1, LOW);
    lcd.print("HIGH MOISTURE1");
    lcd.setCursor(2,1);
    lcd.print("MOTOR1 OFF");
    delay(1000);
    lcd.clear();
    sendSMS("NORMAL MOISTURE MOTOR1 OFF"); a=1;b=0;
  }
  if (digitalRead(SoMS2) ==LOW)
  { digitalWrite(led2, HIGH);
    digitalWrite(mtr2, HIGH);
    lcd.print("LOW MOISTURE2");
    lcd.setCursor(2,1);
    lcd.print("MOTOR2 ON");
    delay(1000);
    lcd.clear();
    sendSMS("LOW SOIL MOISTURE2 MOTOR 2 ON");c=0;d=1;
  }
  if (digitalRead(SoMS2)== HIGH)
  {
    digitalWrite(led2, LOW);
    digitalWrite(mtr2, LOW);
    lcd.print("HIGH MOISTURE2");
    lcd.setCursor(2,1);
    lcd.print("MOTOR2 OFF");
    delay(1000);
    lcd.clear();
    sendSMS("NORMAL SOIL MOISTURE2 MOTOR 2 OFF");c=1;d=0;
  }
   if (digitalRead(SoMS3)== LOW)
  { digitalWrite(led3, HIGH);
    digitalWrite(mtr3, HIGH);
    lcd.print("LOW MOISTURE3");
    lcd.setCursor(2,1);
    lcd.print("MOTOR3 ON");
    delay(1000);
    lcd.clear();
    sendSMS("LOW SOIL MOISTURE3 MOTOR 3 ON");e=0;f=1;
  }
  if (digitalRead(SoMS3)== HIGH)
  {
    digitalWrite(led3, LOW);
    digitalWrite(mtr3, LOW);
    lcd.print("HIGH MOISTURE3");
    lcd.setCursor(2,1);
    lcd.print("MOTOR3 OFF");
    delay(1000);
    lcd.clear();
    sendSMS("NORMAL SOIL MOISTURE3 MOTOR 3 OFF");e=1;f=0;
  }
   if (digitalRead(SoMS4)== LOW)
  { digitalWrite(led4, HIGH);
    digitalWrite(mtr4, HIGH);
    lcd.print("LOW MOISTURE4");
    lcd.setCursor(2,1);
    lcd.print("MOTOR4 ON");
    delay(1000);
    lcd.clear();
    sendSMS("LOW SOIL MOISTURE4 MOTOR 4 ON");g=0;h=1;
  }
   if (digitalRead(SoMS4)== HIGH)
  {
    digitalWrite(led4, LOW);
    digitalWrite(mtr4, LOW);
    lcd.print("HIGH MOISTURE4");
    lcd.setCursor(2,1);
    lcd.print("MOTOR4 OFF");
    delay(1000);
    lcd.clear();
    sendSMS("NORMAL SOIL MOISTURE4 MOTOR 4 OFF");g=1;h=0;
  }
   if (digitalRead(RDpS)== HIGH)
  { digitalWrite(led1, LOW);
    digitalWrite(mtr1, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(mtr2, LOW);
    digitalWrite(led3, LOW);
    digitalWrite(mtr3, LOW);
    digitalWrite(led4, LOW);
    digitalWrite(mtr4, LOW);
 
  }
  else
    { digitalWrite(led1, LOW);
    digitalWrite(mtr1, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(mtr2, LOW);
    digitalWrite(led3, LOW);
    digitalWrite(mtr3, LOW);
    digitalWrite(led4, LOW);
    digitalWrite(mtr4, LOW);
  }
}

void sendSMS(String msg)
{
if ( msg.indexOf("1") >= 1 && msg.indexOf("ON") >= 1 && a == 1   )   {  sending(msg); }
if ( msg.indexOf("1") >= 1 && msg.indexOf("OFF") >= 1 &&  b == 1  )   {  sending(msg); }

if ( msg.indexOf("2") >= 1 && msg.indexOf("ON") >= 1 && c == 1   )   {  sending(msg); }
if ( msg.indexOf("2") >= 1 && msg.indexOf("OFF") >= 1 &&  d == 1  )   {  sending(msg); }

if ( msg.indexOf("3") >= 1 && msg.indexOf("ON") >= 1 && e == 1   )   {  sending(msg); }
if ( msg.indexOf("3") >= 1 && msg.indexOf("OFF") >= 1 &&  f == 1  )   {  sending(msg); }

if ( msg.indexOf("4") >= 1 && msg.indexOf("ON") >= 1 && g == 1   )   {  sending(msg); }
if ( msg.indexOf("4") >= 1 && msg.indexOf("OFF") >= 1 &&  h == 1  )   {  sending(msg); }

}

void sending(String msg)
{
lcd.clear();
lcd.print("Sending SMS");
myserial.println("AT+CMGF=1");
delay(500);
myserial.print("AT+CMGS=");
myserial.print('"');
myserial.print("+923411046117");// number
myserial.print('"');
myserial.println();
delay(500);
myserial.println(msg);
delay(500);
myserial.write(26);
delay(1000);
lcd.clear();
lcd.print("SMS Sent");
delay(1000);
lcd.begin(16, 2);
}
void gsmInit()
{
lcd.clear();
lcd.print("Finding Module..");
boolean at_flag = 1;
while (at_flag)
{
myserial.println("AT");
while (myserial.available() > 0)
{
if (myserial.find("OK"))
at_flag = 0;
}
delay(1000);
}
myserial.println("ATE0");
lcd.clear();
lcd.print("Finding Network..");
boolean net_flag = 1;
while (net_flag)
{
myserial.println("AT+CPIN?");
while (myserial.available() > 0)
{
if (myserial.find("READY"))
net_flag = 0;
break;
}
delay(1000);
}
myserial.println("AT+CNMI=2,2,0,0,0");
delay(1000);
myserial.println("AT+CMGF=1");
delay(1000);
myserial.println("AT+CSMP=17,167,0,0");
lcd.clear();
myserial.flush();
}

hope you can see flags i used to limit to 1 sms sending/detection

hope you can see flags i used to limit to 1 sms sending/detection

THANKS a lot @KASSIMSAMJI
Happy to see your replay
it really works with my project.
another thing i want to know that if i use dht11 humidity sensor then how to display the sensor values on lcd and also send as sms.

itx-opps:
THANKS a lot @KASSIMSAMJI
Happy to see your replay
it really works with my project.
another thing i want to know that if i use dht11 humidity sensor then how to display the sensor values on lcd and also send as sms.

did you create that sketch above or you copied it from somewhere?

if you did create it then what you are asking should not be a problem at all but if C&P is all you did then you have to 'study/understand' it , then you will be able to add your dht11 in it, displays it and send sms

did you create that sketch above or you copied it from somewhere?

i write this code my self except the part of sending sms which is the main i am new to programing and have not enough skills to create a competent program. I am trying my best by using google and also try to under stand the arduino programing completely. in programing the problems i face are the basics of declaring some variables and using the float values also did not know how to use the high level gadgets used with arduino such as sim modules and gps modules. your kind help is very important to me.
thanks @KASSIMSAMJI