Want to send a msg only once a condition detected without terminating the program loop

sim800l

#1

Hi.
I am new to programing and i am trying to make a project of irrigation system using arduino mega and sim800l Gsm module. the think i actually want to do is that if once a soil sensor detects a condition it sends sms only once. but the code i have written is constantly sending the sms terminating the other functions of program

#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();
}


#2

could you please help me @RaviPujar


#3

Your code logic should be something like this,

int x;
void setup()
{
    x=0;
}
void loop()
{
if(condition)
{
    if(x==0)
    {
        Print("Executed once");
        x=1;
    }
}
}

This would print only once since after printing once x would be 1;


#4

i think you are asking to int some flags to the gsm module


#5

Yes use some flags in code to save the message sent state.