Sometimes it works perfectly and sometimes it doesn't. When i use button instead of ultrasonic sensor to trigger phone call, it always works...
heres 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;
int sirena = 5;
long duration;
int distance;
void setup() {
SIM900.begin(19200);
Serial.begin(19200);
// Give time to your GSM shield log on to network
//delay(20000);
// AT command to set SIM900 to SMS mode
SIM900.print("AT+CMGF=1\r");
// Set module to send SMS data to serial out upon receipt
SIM900.print("AT+CNMI=2,2,0,0,0\r");
pinMode(led, OUTPUT);
pinMode(sirena, OUTPUT);
//digitalWrite(led,LOW);
pinMode(led2, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
// Calculate the distance:
distance = duration * 0.034 / 2;
Serial.print("Distance = ");
Serial.print(distance);
Serial.println(" cm");
if(SIM900.available() >0) {
message = SIM900.readString();
//Get the character from the cellular serial port
}else
{
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");
digitalWrite(led,LOW);
}
}
if( distance < 20 && digitalRead(led) == HIGH){
SIM900.println("ATD + +381601416081;");
SIM900.println();
digitalWrite(sirena, HIGH);
delay(300);
digitalWrite(sirena, LOW);
}
}