Using ultrasonic sensor to prompt SMS at certain value

I have been trying to detect distance with an ultrasonic sensor and when a certain distance is measured for the Arduino UNO to prompt an SMS using the V2 shield. I have been having a problem as when the desired value is reached the program stops and does not send the SMS.

I believe the issue lies within the IF statement as seen in the code attached but I am not sure what is wrong as I am still very new to Arduino.

Any help would be great. Thanks in advance.

// this constant won't change.  It's the pin number
// of the sensor output:
const int pingPin = 7;


#include <GSM.h>

#define PINNUMBER ""

// initialize the library instance
GSM gsmAccess;
GSM_SMS sms;


void setup()
{
  // initialize serial communication:
  Serial.begin(9600);
}

void loop() 
{
  // establish variables for duration of the ping,
  // and the distance result in and centimeters:
  long duration, cm;

  
  delay(2000); //duraton between pulses
  
  // The signal is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the output signal, a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the signal to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  cm = microsecondsToCentimeters(duration);

  if (cm >= 15) 
  {

  char *phoneNumber = "****";  // telephone number to send sms

  // sms text
  char *messageToSend = "Woohoo";
  
  sms.beginSMS(phoneNumber);
  sms.print(messageToSend);
  sms.endSMS();
  
  } 
  
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  delay(100);
  
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The signal travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
  
}

// The same pin is used to read the signal

Really? Most ping sensors have a trigger pin and an echo pin.

Why is it so difficult for you to put a Serial.print() statement in the if(cm >= 15) body?

Sorry I had inserted the Serial.print() into the IF statement but I am still not receiving and SMS for some reason.

It will print the value when above or equal to 15cm then stop. So I have another question of how to return back into the program loop after it has completed the IF statement?

Sorry I had inserted the Serial.print() into the IF statement but I am still not receiving and SMS for some reason.

Nor are you providing any proof.

Can you send an SMS UNCONDITIONALLY?

If not, quit dragging that red whale around. The problem has NOTHING to do the the distance measure.

If you CAN send (and receive) an SMS when the SMS is sent unconditionally, then the ping sensor is causing a hardware problem.

So I have another question of how to return back into the program loop after it has completed the IF statement?

You have no understanding of how code is executed, apparently, or you would not need to ask this question.

When the body of the if statement is completed, the next line of code after the end of the if statement IS executed. You don't have to DO anything to make that happen.

I HAVE suggested that you print a message BEFORE (trying to) send(ing) the SMS AND AFTER sending it.

You have NEVER posted code that does that, so I do not, for a minute, believe that you have done that.

I'm done trying to help, until you do that AND post the code AND post the output along with your observations regarding what you were doing and what the code actually did.

I have done this as I said.

Yes I can send an SMS unconditionally so it is not the GSM shield itself as far as I am aware.

The program will enter the if statetment when >15. It will then print the value that is >15 once before it hangs and does not send an SMS or print any further values.

For some reason my PC will not let me picture the serial monitor to illustrate so I had to attach it as PDF.

// this constant won't change.  It's the pin number
// of the sensor output:
const int pingPin = 7;


#include <GSM.h>

#define PINNUMBER ""

// initialize the library instance
GSM gsmAccess;
GSM_SMS sms;


void setup()
{
  // initialize serial communication:
  Serial.begin(9600);
}

void loop() 
{
  // establish variables for duration of the ping,
  // and the distance result in and centimeters:
  long duration, cm;

  
  delay(2000); //duraton between pulses
  
  // The signal is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the output signal, a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the signal to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  cm = microsecondsToCentimeters(duration);

  if (cm >= 15) 
  {

  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  
  char *phoneNumber = "*****1234";  // telephone number to send sms

  // sms text
  char *messageToSend = "Woohoo";
  
  sms.beginSMS(phoneNumber);
  sms.print(messageToSend);
  sms.endSMS();

  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  
  } 
  
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  delay(100);
  
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The signal travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
  
}

SM.pdf (150 KB)

The program will enter the if statetment when >15.

WRONG! The program will evaluate the condition in the if statement EVERY time.

It may execute the body of the if statement, if the condition is true.

It really helps to explain things correctly.

For some reason my PC will not let me picture the serial monitor to illustrate so I had to attach it as PDF.

More horseshit. If you were able to copy and paste the text into a pdf file, you were able to paste it to the forum.

I will NOT bother trying to open pdf files.

Do you really need to print the distance three times?