home alarm system

Hello!

I 'm trying to do home alarm gsm system, what calls 3 times and after that send sms
or until the incoming call interrupt the process. code works to calling and sending sms put not interrupt the protsess if i call gsm modem. can somebody help me.

#include <SoftwareSerial.h>

char inchar; 
char phone_no[] = "";
const int analogPin = A0 ; 
const int threshold = 1000; 
char sisu[] = "Alarm kaivitus";
int helistamine = 0;
int sms = 0;




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

  delay(2000);

  
  delay(100);

}

void call()
{
Serial.print("AT+CLIP=1\r\n");
  Serial.println("AT+CMGF=0\r");
  delay(5000);
  Serial.println("AT");
  delay(1000);
  Serial.println("ATD+3725805****;\r\n");
  delay(9000);
  Serial.println("ATH");
  delay(1000);
  helistamine = helistamine + 1;
  delay (10000);


}
void sms1()
{


  Serial.print("AT+CMGF=1\r\n");
  delay(100);
  Serial.println("AT+CMGS=\"+3725805****\"");
  delay(100);
  Serial.print(sisu);
  delay(100);
  Serial.println((char)26);
  sms = sms + 1; 


}


void doSomething()
{
  helistamine = 5;
  sms = 5;
}

void reset()
{
  if (Serial.available() > 0)
  {
    inchar = Serial.read();
    if (inchar == '3')
    {
      delay(10);
      inchar = Serial.read();
      if (inchar == '7')
      {
        delay(10);
        inchar = Serial.read();
        if (inchar == '2')
        {
          delay(10);
          inchar = Serial.read();
          if (inchar == '5')
          {
            delay(10);
            inchar = Serial.read();
            if (inchar == '8')
            {
              delay(10);
              inchar = Serial.read();
              if (inchar == '0')
              {
                delay(10);
                inchar = Serial.read();
                if (inchar == '5')
                {
                  delay(10);
                  inchar = Serial.read();
                  if (inchar == '*')
                  {
                    delay(10);
                    inchar = Serial.read();
                    if (inchar == '*')
                    {
                      delay(10);
                      inchar = Serial.read();
                      if (inchar == '*')
                      {
                        delay(10);
                        inchar = Serial.read();
                        if (inchar == '*')
                        {
                          Serial.println("ATH");
                          delay(10);
                        
                          doSomething();
                          
                          delay(60);
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
void loop()
{ 
  


  int analogValue = analogRead(analogPin);
  if (helistamine < 3)
  {
    if (analogValue > threshold) 
    {
      reset();
      delay(10000);
      call();
      delay(50000);
      reset();
      if (helistamine > 2) 
  {
    if (sms < 1) 
    {
      if (analogValue > threshold)
      { reset();
        sms1();
      }
    }
  }
  if (analogValue < threshold)
  {

    helistamine = 0;
    sms = 0;
  }
    }
  }

  

}

Is the reset() function what you are attempting to use to interrupt the process? Perhaps you should read the contents into a string and do a proper string compare instead of endlessly nested if statements.

But the issue is the placement of the call to reset(). Perhaps move it to the top of the loop so it gets called frequently.

eg

void loop()
{
  int analogValue = analogRead(analogPin);
  reset();  
  if (helistamine < 3) {
  //etc

yes, the reset() function is what must cancel function call and function sms1. when i put reset() function before if then it's works only before

if (helistamine < 3)
  {etc

So the problem is that reset() never changes the value of helistamine? What happens to your existing logic if there is a delay longer than 10ms between characters?

My suggestion is to use something like below and call it at the top of the loop.

String inputbuff="";
void reset()
{
 while (serial.available()>0 ) {
    inputbuff+=Serial.read();
 };
 if (inputbuff.indexOf("your_reset_code")) {
 doSomething();
 inputbuff=""; // reset the input buffer
 }
}

If you are concerned about resetting before the alarm is triggered then you could modify doSomething() like

void doSomething()
{
  if (helistamine>0) helistamine = 5;
  if (sms>0) sms = 5;
}

You should also check that the brackets match where you think they do- I suspect that currently the alarm condition will never reset because you effectively have

if (analogValue > threshold)
    {
        //alarm code removed 
 if (analogValue < threshold)
 {
 helistamine = 0;
 sms = 0;
 }
}

which means it can never set the counters back to zero again.

Now it work.
I change function reset to this:

void reset()
{
   while (SIM900.available()) {
    delay(10);  
    if (SIM900.available() >0) {
      char c = SIM900.read();
      
       
      readString += c;
    } //makes the string readString
  }

  if (readString.length() >0) {
    Serial.println(readString); 
if(readString.indexOf(phone_no) >=0) {
      delay(30);
      doSomething();
    }
    

    readString=""; 
  }
}

Glad to hear it works!

You may wish to have some method of clearing readstring if it gets too long- for example if the wrong phone starts to call your GSM modem repeatedly then the string may fill up and cause a memory overflow.