Ultrasonic sensor with GSM board - calling within a distance

Hi All!

My problem is that I am trying to initiate a call with my SIM900 GSM board based on a distance measurement using an ultrasonic sensor (HCSR04) and the call does not happen. I am using Arduino Uno.
I am not sure if this is a programming issue or a hardware issue (not enough power maybe?). Calling is working with the same hardware if I use a simple call.Call command in the loop without the sensor. So the power is enough for the GSM board if I do not use the sensor. I do not know the sensor's power consumption. I connected the sensor's VCC to the 5V pin on the Arduino.

#include <SoftwareSerial.h>
#include "SIM900.h"
#include "sms.h"
#include "call.h"
SMSGSM sms;
CallGSM call;

#define echoPin 8 // Echo Pin
#define trigPin 9 // Trigger Pin

int distance;
int safetyDistance;
long duration;
const int ledPin =  13;

void setup() {

  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  gsm.begin(9600);        // for GSM shield  
  delay(1500);  
  Serial.begin(9600); // for serial monitor
  delay(1500);

}

void loop() {

  // Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);

  // Clears the trigPin
digitalWrite(trigPin, LOW);
delay(500);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delay(1000);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance
distance= duration/58.2;

safetyDistance = distance;
if (safetyDistance <= 50){
 delay(8000);
 digitalWrite(ledPin, HIGH);
 call.Call("+36306004751");
 delay(3000);
 Serial.println("dialing");
 delay(10000);  // wait for 30 seconds...
 call.HangUp();
 delay(4000);
}
else
{
  Serial.println("No movement");
  digitalWrite(ledPin, LOW);
}



}

Please help me to resolve it. I would need it for security reasons for my remote location.

Thank you in advance.

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delay(1000);
digitalWrite(trigPin, LOW);

You think 1000ms == 10 us.?

I modified that value and not corrected the comment. Disregard it please. It does not really matter from the aspect of the solution I think. But correct me if I am wrong. Thanks.

It does not really matter from the aspect of the solution I think

It matters greatly if the sonar pings on the rising edge of the trigger pulse.

What do the debug prints show?

Hi Awol,
Thanks for your comment. Prints in the serial show the appropriate lines. Distance are printed based on the defined delays and calling is starting as print shows "dialing". But the call never comes in. The weird thing is that the gsm board's status and net light is blinking following the trigger and echo signals in the same time. And the led has also a slighter bright when it turns on.

Any idea?
Thank you

Please someone give a little help.

Some intelligent arrangement of code might be useful.

  // Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);

  // Clears the trigPin
digitalWrite(trigPin, LOW);
delay(500);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delay(1000);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance
distance= duration/58.2;

Why the hell would you print some bogus value, label it distance, and THEN measure the distance?

 delay(10000);  // wait for 30 seconds...

Stupid comment. That is NOT what the code does.

What pins is the call library expecting to use? Are they actually available?

I'd ask you what distance you actually measure, but it is obvious that you haven't a clue, so I won't bother.