Led won't go off while using ultrasonic sensor

Hi, I'm using GSM GPRS SIM900 module to turn one Led ON or OFF (depending on specific SMS from a specific number that i use function indexOf to compare). Now if that LED is on, and if ultrasonic sensor detects object 20cm or closer to it, GSM module starts to call defined phone number. Everything works properly except when i want to turn off LED, it calls defined number instead of turning LED off. (when i send SMS with content OFF, it calls me instead of turning LED off). Now if i use taster to trigger a phone call instead of ultrasonic sensor, everything works properly. Why can't i turn led for indication off while using ultrasonic sensor? (sometimes it works and sometimes it would get triggered on it's own). Is my ultrasonic sensor broken or is there a false in my code?
Here is full code:

#include <SoftwareSerial.h>
#define trigPin 2
#define echoPin 3
// Configure software serial port
SoftwareSerial SIM900(7, 8);
//Variable to save incoming SMS characters
char incoming_char=0;
String message;
int led = 9; //led for indication
int sirena = 5; //alarm sound
int button = 6; //if i'm using this button instead of ultrasonic sensor for triggereing, it works properly
long duration;
int distance;

void setup() {
// Give time to your GSM shield log on to network
 // AT command to set SIM900 to SMS mode
  // Set module to send SMS data to serial out upon receipt 
  pinMode(led, OUTPUT);
  pinMode(button, INPUT_PULLUP);
  pinMode(sirena, OUTPUT);

   pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
void loop() {
  digitalWrite(trigPin, LOW);
  digitalWrite(trigPin, HIGH);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  // Calculate the distance:
  distance = duration * 0.034 / 2;
  Serial.print("Distance = ");  
  Serial.println(" cm");
  if(SIM900.available() >0) {
    message = SIM900.readString();
    //Get the character from the cellular serial port
  message = "";
   if(message.indexOf("+381601416081")> -1){
  if(message.indexOf("ON") > -1){
      Serial.println("LED ON");
     digitalWrite(led, HIGH);
    else if(message.indexOf("OFF") > -1){

      Serial.println("LED OFF");

 if(distance < 20  && digitalRead(led) == HIGH){
  SIM900.println("ATD + +381601416081;");
  digitalWrite(sirena, HIGH);
  digitalWrite(sirena, LOW);
  // AT command to hang up
  SIM900.println("ATH"); // hang up

There are very long delays in your code. While the uC is executing the delay() function, it is blocked into a loop and it can't do anything else, including taking care of LEDs. The first thing I'd try would be to refactor the code with a non-blocking FSM. Try this approach and see if anything improves.

As a side note (although unrelated to your problem), I'd avoid the String class altogether and use a traditional array of char instead.

As addional information I will spent a short analysis of used delay()´s.


Why do use the delay() function?
I guess, you shall check the needs for this function.

Well there's only one long delay (20 seconds for GSM to connect to network at the beginning), i removed that delay now, and i also removed the alarm one completely and set the call duration to 4 seconds). Now i do not have delays at all in my program (except for one of call duration). It's still making problems...

delay(20000); was for GSM to connect to network (I removed that)
delay(100); i removed also

delayMicroseconds(5); is to clear the trigPin (of ultrasonic senosr) by setting it to low
then i trigger it by setting it to HIGH for 10 micro seconds? - delayMicroseconds(10); i need that...
delays 10, 100 and 50 i also removed now.

delay(10000) is for call duration and i removed it completely
delay(4000) is for siren(alarm) to alert. i set that to 500 just for purpose of testing

Now it works fine, thanks.

I read somewhere that i need to use short delays like 100 and 50 so that arduino doesn't run into problem, do i really need to use them, seems like they were making problems here.

There's no obligation whatsoever to use delays at all in your sketches. If you don't need them, that's fine. They can be practical sometimes, but personally I try to avoid them at all whenever feasible.

This rule certainly is not set in stone. You can miss events with delays as short as 10 or 20 ms, yet sometimes you can get away with longer delays. It really depends on what else you need the uC to do and on how fast or slow the events you are trying to respond to are.

There is never a case for needing a delay() call. If your code now works without delay(), you needn't worry about that any longer.

I see, my program works properly now. Thanks.

What is confusing is that this program worked fine yesterday with all delays and now it wouldn't...

So my program started to make same flaws after some time even without all the delays, then I found out that every time I send SMS to GSM module, ultrasonic sensor randomly calculates some distance of 5 or less centimeters (even tho nothing is this close to it). GSM module interrupts work of ultrasonic sensor and thats what was triggering phone calls.