How to make GSM module send text message just once

hello, pls i need help with my code. i'm trying to send text message to my sim using GSM module.
it is working perfectly, but it keeps sending the message repeatedly. pls how can I make it send the SMS just once.
note: I cant use setup because it has to continually read data and send SMS when the sensor value threshold value is LOW.

 // Reads analog pin A0 for moisture value from the sensor
  soilMoistureValue = analogRead(A0); 

  // Uses arduino map function to change value of soil moisture to percentage
  percentage = map(soilMoistureValue, 1023, 585, 0, 100);
  Serial.println(percentage);
 
  // Using IF and ELSE function to OFF and ON the pump using relay
  // In NC,Relay uses active high signal to power and low signal to power off
  
if (percentage <20) 
  { 
   digitalWrite(2,LOW);
  (gprs.sendSMS(PHONE_NUMBER,RICEMESSAGE1 ));
   
   //Serial.print("Send SMS Succeed!\r\n"); 
   Serial.println(" pump on, irrigation for rice has started");    
  }

  else if(percentage >85) 
  {
  digitalWrite(2,HIGH);
  (gprs.sendSMS(PHONE_NUMBER,RICEMESSAGE2 ));
 
  //Serial.print("Send SMS failed!\r\n");
  Serial.println("pump off,  irrigation for rice has stopped");
  gprs.deleteSMS(messageIndex);
 

What you do instead is send the data when the value becomes LOW.

Study the Arduino StateChangeDetection example to learn how to do that. Files>Examples>...

it has to notify the user when its HIGH and when its LOW.

Same procedure, either way. The trick is to use State Change, not a value.

how do i do that pls?

Study the example. And read the replies more carefully in the future.

ok thanks, will check it out

int i = 0,k = 0;
if (percentage <20)
{
digitalWrite(2,LOW);
Serial.println(" pump on, irrigation for rice has started");
if(i == 0)
{
(gprs.sendSMS(PHONE_NUMBER,RICEMESSAGE1 ));
i++;
}
k = 0;
}
if(percentage >85)
{
digitalWrite(2,HIGH);
Serial.println("pump off, irrigation for rice has stopped");
if (k == 0)
{
(gprs.sendSMS(PHONE_NUMBER,RICEMESSAGE2 ));
k++;
}
i = 0;
}

try like this

Please use code tags when posting code.

i just tried this code, still sending multiple SMS

I was able to solve it by using flags to indicate State change

 // Reads analog pin A0 for moisture value from the sensor
  soilMoistureValue = analogRead(A0); 

  // Uses arduino map function to change value of soil moisture to percentage
  percentage = map(soilMoistureValue, 1023, 585, 0, 100);
  Serial.println(percentage);
 
  // Using IF and ELSE function to OFF and ON the pump using relay
  // In NC,Relay uses active high signal to power and low signal to power off
  
if (percentage <=10 && flag==0) 
  { 
    delay(1000);
    if (percentage <=10)
    {
    digitalWrite(2,LOW);
    (gprs.sendSMS(PHONE_NUMBER,BEANSMESSAGE1 ));
    //Serial.print("Send SMS Succeed!\r\n"); 
    Serial.println(" pump on, irrigation for beans has started"); 
      delay(2000);
      flag=1;
   
    }
   
  }

  else if(percentage >=80 && flag==1) 
  {
    if (percentage >=80)
    {
    digitalWrite(2,HIGH);
    (gprs.sendSMS(PHONE_NUMBER,BEANSMESSAGE2 ));
 
    //Serial.print("Send SMS failed!\r\n");
    Serial.println("pump off,  irrigation for beans has stopped");
    delay(2000);
    flag=0;
     
    }
  }

How to do this if I have 3 options? In my case it's low, medium, and high. How to implement state change? Can someone help!

void loop() {
  int value = analogRead(A0);
  Serial.println(value);
 if (value < 300) {
    Serial.println(sthigh);
    sendHigh();
  } else if (value > 300 && value < 950) {
    Serial.println(stmid);
    sendMid();
  }else if (value > 950) {
    Serial.println(stlow);
    sendLow();
  }
}

I got multiple sms. If I will use flags just like from @mike050 , it only detect 2 state change not for the 3. Thank you in advance for the help!

can i see a snippet of your code when you used the state change.

I have this as of now and I am guessing it won't read the mid level when the sensor detects high

String sthigh = "Water level : HIGH";
String stmid = "Water level : MIDIUM";
String stlow = "Water level: LOW";
int i=0,k=0;

void setup()
{
  SIM900A.begin(9600);   // Setting the baud rate of GSM Module
  Serial.begin(9600);    // Setting the baud rate of Serial Monitor (Arduino)
  delay(100);
  Serial.println ("System is Ready!");
}

void loop() {
  int value = analogRead(A0);
  Serial.println(value);
 if (value < 300 && i==1 && k==1) {
    Serial.println(sthigh);
    k=0;
    i=0;
    sendHigh();
  } else if (value > 300 && value < 950 && i==0 && k==1) {
    Serial.println(stmid);
    k=1;
    i=1;
    sendMid();
  }else if (value > 950 && i==0 && k==0) {
    Serial.println(stlow);
    k=1;
    i=0;
    sendLow();
  }
}

I think this might help.

String sthigh = "Water level : HIGH";
String stmid = "Water level : MIDIUM";
String stlow = "Water level: LOW";
int flag=0;

void setup()
{
  SIM900A.begin(9600);   // Setting the baud rate of GSM Module
  Serial.begin(9600);    // Setting the baud rate of Serial Monitor (Arduino)
  delay(100);
  Serial.println ("System is Ready!");
}

void loop() {
  int value = analogRead(A0);
  Serial.println(value);
 if (value <= 300 && flag==0 || flag==1) {
    Serial.println(sthigh);
    sendHigh();
    flag==1;

  } else if (value > 300 && value < 950 && flag==1) {
    Serial.println(stmid);
    sendMid();
    flag==0;

  }else if (value >= 950 && flag==0) {
    Serial.println(stlow);
    sendLow();
    flag==1;
  }
}




You can also go through the example on your arduino IDE "File->Examples->02.Digital->StateChangeDetection"

Still sending multiple message. I will check the examples of state change detections thank you!