How can make a moisture sensor trigger an SMS message from the GSM shield

So, i am doing a project using a moisture sensor, arduino, and a GSM shield.

What i want is a text message whenever it is very dry. First, i did the code where writing the letter on the serial monitor triggers the SMS message and it worked.
This part of the code shows what I mean:-

void loop()
{

if (Serial.available())
{
char c = Serial.read();
if (c == ‘W’)
{
writeSMS();
}
}
}

After that, I added the moisture sensor to the code and made a value from the moisture sensor trigger an SMS. the code below shows what I mean:-

void loop()
{
int sensorValue = analogRead(mostureSensor);
Serial.println(sensorValue);
delay(30000);
if (Serial.available())
{

if (sensorValue >= 645)
{
writeSMS();
}
}
}

However, it did not work. The value reached 645, but no text message was sent. The serial monitor keeps reading values every half a minute and it acts as if the arduino is not connected to the GSM shield.

Can anybody tell what mistake I have or what should I do?
I am beginner to arduinoes and I am should get this done ASAP. please help me if you know what is going on.

Serial.available() checks if data is available on the serial port for reading. So in your case you need to send something to the serial port before it will send the SMS.

If you take that condition out, it will work

void loop()
{
  int sensorValue = analogRead(mostureSensor);
  Serial.println(sensorValue);
  delay(30000);
  if (sensorValue >= 645)
  {
    writeSMS();
  }
}

Notes: 1) I don't know how relevant measured moisture data still is after 30 seconds :D 2) If you want to keep your code responsive, don't use delays. If you're also e.g. in future want to read a button, it will only be read every 30 seconds; chances are slim that you press it at the right time ;) 3) Please post code between code tags type ``` [code] ```

paste your code after that type ``` [/code] ``` after the pasted code

Thank you so much for that. I did exactly what you asked me to do and deleted the delay. However, it still doesnt work. The moisture sensor keeps reading values very fast and it doesnt go to the command of @ writing an SMS@. I am so confused and I need to get this done not after today.

Post your new code (complete).

#include <SoftwareSerial.h>
// Include the MG2639 Cellular Shield library
#include <SFE_MG2639_CellShield.h>
const char EOM_CHAR = '~';

char myPhone[15];

int mostureSensor = 0;

void setup() 
{
  Serial.begin(9600);

  int beginStatus = cell.begin();
  if (beginStatus <= 0)
  {
    Serial.println(F("Unable to communicate with shield. Looping"));
    while(1)
      ;
  }
  delay(2000);
  
  sms.setMode(SMS_TEXT_MODE);
  
 
}

void loop() 
{ 
  int sensorValue = analogRead(mostureSensor);
  Serial.println(sensorValue);
  
  if (Serial.available())
  {
    
    if (sensorValue >= 645)
    {
      writeSMS();
    }
  }
}

void writeSMS()
{
 
  
  Serial.print(F("Sending a message to "));
  Serial.println("5128254963");
  
  sms.start("5128254963");
  if (Serial.available())
  {
  sms.write("water.");
  
  }
 
  
  Serial.println();
  Serial.println("Sending the message.");
  Serial.println();
  
  sms.send();

  
}

In my code (above) I showed you to take the Serial.available() out. Why are you waiting for serial data to come in before sending the SMS ???

Below a cleaned up version of your code.

void loop()
{
  int sensorValue = analogRead(mostureSensor);
  Serial.println(sensorValue);

  if (sensorValue >= 645)
  {
    writeSMS();
  }

  delay(30000);
}

This uses the blocking delay. If you want something non-blocking so you can still react on incoming data on the serial port or a button press

void loop()
{
  // start of 30 second 'delay'
  static unsigned long start30seconds = 0;
  // current time;
  unsigned long currentTime = millis();


  // if it's time
  if(currentMillis - start30seconds >= 30000)
  {
    // reset start time
    start30seconds = currentMillis;
    // read sensor
    int sensorValue = analogRead(mostureSensor);
    // and display (for debugging)
    Serial.println(sensorValue);

    // check limit
    if (sensorValue >= 645)
    {
      // send sms
      writeSMS();
    }
  }

  // you can read buttons here or react in serial port or whatever
  // they will not be affected by the 'delay'

}

Code not compiled or tested.

It works now, thank you so much . I really appreciate your help. God bless you