Arduino Watering System

Hi people,

I hope that you are doing well. Throught this topic, I would like to ask you if you can give me some help. I’ve made a small irregation system using Arduino Uno Rev3, GSM Module Sim 900, Relay 220Vac and some other electronic components. I also made a code in C++, but I have an issue with this code, and I think that someone here maybe can help me.
My system works like this:

If there is no water in the ground, the soil moisture sensor sends this information to the Arduino board, and Arduino Board turns on relay in order to power up the pump. But before turning on the Relay, Arduino communicates throught serial port with GSM Module Sim 900, and sends the message to the user, to tell him that the pump will be turned on soon. But the problem is that if there is no water in the ground, and Arduino sends the first message to the user and turns on the pump, the user recieves messages constantly even if the pump has started to work.

What I want to do is: I want the Arduino to send just one message to the user, and to turn on the pump. Not to keep sendin sms messages while there is no watter in the ground.

Here is the code that I’ve wrote:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(9, 10);
int Relay = 13;
int sensor = 8;
int val;
int i;
int j;
void setup()
{
  pinMode(13, OUTPUT);
  pinMode(8, INPUT);
  Serial.begin(9600);
  mySerial.begin(9600);  

}

void loop()
{
  val = digitalRead(8);
  if(Serial.available()==0)
  {
    if(val == LOW)
    {
      
      delay(2000);
      digitalWrite(13, LOW);
    }else
    {
      delay(2000);
      digitalWrite(13, HIGH);
     for(j=0; j<1; j++)
     {
        SendMessage2(); 
     }
      
    }
  }
  if (mySerial.available()>0)
  Serial.write(mySerial.read());
}


void SendMessage1()
{
  mySerial.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
  delay(1000);  // Delay of 1000 milli seconds or 1 second
  mySerial.println("AT+CMGS=\"+38344319085\"\r"); // Replace x with mobile number
  delay(1000);
  mySerial.println("Procesi i ujitjes ka filluar");// The SMS text you want to send
  delay(1000);
  mySerial.println((char)26);// ASCII code of CTRL+Z
  delay(1000);
}

void SendMessage2()
{
  mySerial.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
  delay(1000);  // Delay of 1000 milli seconds or 1 second
  mySerial.println("AT+CMGS=\"+38344319085\"\r"); // Replace x with mobile number
  delay(1000);
  mySerial.println("Ka perfunduar procesi i ujitjes");// The SMS text you want to send
  delay(1000);
  mySerial.println((char)26);// ASCII code of CTRL+Z
  delay(1000);
}

void RecieveMessage()
{
  mySerial.println("AT+CNMI=2,2,0,0,0"); // AT Command to receive a live SMS
  delay(1000);
}

When you turn the pump on (and send the message) set a flag (pumpTurnedOn) and, as loop() loops, check the flag before sending the message. If the flag is set, skip sending the message. When you turn the pump off, clear the flag so that the next time the pump turns on you can send the message (and set the flag).

Can you write to me an example of how to set a flag?

Thank you in advance.