My program running incorrect

Dear all!
Sorry for my bad English skill

I have a code to control a contactor by sim900A
I would like to post below

#include <SoftwareSerial.h> 
char inchar;
SoftwareSerial SIM900(3,2);
int onoff=0; // 0 = off, 1 = on
int led =11;
int in = 12;
int stt;
void setup()
{
  Serial.begin(9600);
  SIM900.begin(2400);
  SIM900.print("AT+CLIP=1\r");
  pinMode(11, OUTPUT);
  pinMode(12,INPUT_PULLUP);
  digitalWrite(11,HIGH);
  delay(10000);
  SIM900.print("AT+CMGF=1\r");
  SIM900.print("AT+CNMI=2,2,0,0,0\r");
  Serial.println("Ready...");
  stt = digitalRead(12);
  if(stt==LOW)
  {SIM900.println("AT+CMGF=1");   
  delay(1000);  
  SIM900.println("AT+CMGS=\"0919530abc\"\r");
  delay(1000);
  SIM900.println("TURN ON");
  delay(100);
  SIM900.println((char)26);}
  else
  {SIM900.println("AT+CMGF=1");   
  delay(1000);  
  SIM900.println("AT+CMGS=\"0919530abc\"\r");
  delay(1000);
  SIM900.println("TURN OFF");
  delay(100);
  SIM900.println((char)26);}
  
  
}

  void test()
    {
  SIM900.print("AT+CMGF=1\r");
  SIM900.print("AT+CNMI=2,2,0,0,0\r");
  Serial.println("Ready...");
  stt = digitalRead(12);
  if(stt==LOW)
  {SIM900.println("AT+CMGF=1");   
  delay(1000);  
  SIM900.println("AT+CMGS=\"0919530abc\"\r");
  delay(1000);
  SIM900.println("TURN ON");
  delay(100);
  SIM900.println((char)26);}  
  
  else
  {SIM900.println("AT+CMGF=1");   
  delay(1000);  
  SIM900.println("AT+CMGS=\"0919530abc\"\r");
  delay(1000);
  SIM900.println("TURN OFF");
  delay(100);
  SIM900.println((char)26);}
  } 

void del(){
  SIM900.print("AT+CMGF=1\r");
  SIM900.println("AT+CMGD=1,4\r");
  }


void doSomething()
{
  if (onoff==0)
  {
    onoff=1;
        digitalWrite(11, LOW);
        SIM900.print("ATH\r");
        delay(10);
  } 
  else
    if (onoff==1)
    {
      onoff=0;
      
      digitalWrite(11, HIGH);
      SIM900.print("ATH\r");
      delay(10);
    }
}
 
void loop() 
{  
  if(SIM900.available() >0)
        inchar=SIM900.read();
    {      if (inchar=='0')
    {  
      delay(10);
      inchar=SIM900.read(); 
      if (inchar=='9')
      {
        delay(10);
        inchar=SIM900.read(); 
        if (inchar=='1')
        {
          delay(10);
          inchar=SIM900.read(); 
          if (inchar=='9')
          {
            delay(10);
            inchar=SIM900.read(); 
            if (inchar=='5')
            {
              delay(10);
              inchar=SIM900.read(); 
              if (inchar=='3')
              {
                delay(10);
                inchar=SIM900.read(); 
                if (inchar=='0')
                {
                  delay(10);
                  inchar=SIM900.read(); 
                  if (inchar=='a')
                  {
                    delay(10);
                    inchar=SIM900.read();
                    if (inchar=='b')
                    {
                      delay(10);
                      inchar=SIM900.read(); 
                      if (inchar=='c')
                                 
                      {
                        Serial.println("do something");
                        delay(10);
                        doSomething();
                        test();
                        del();
                       delay(1000);
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

When the auther number match, program “do something” called and run.
But it ignores " SIM900.print(“ATH\r”)". So that the contactor continuously turn ON/OFF
Some one can help me solve this problem?
Thank all!

Don't use delay() and read your gsm only when you are sure you have received the data here you check only once that SIM900.available() >0 but then you force reading data multiple times.

Please read how to use the forum and post your code using code tags (fix previous post) thx

Thank for your reply. I'm not good at English. Can you explain to me, why "sim900.print("ATH\r") ignored? Thanks you!

Fix your post please, this has nothing to do with your English, it's fine Fix also the way you read your gsm input

ATH hangs up a ringing call or a call in progres

I fixed my post, bro

Can you explain to me, why "sim900.print("ATH\r") ignored? And i need to delete "delay(10)" in the void loop? Thanks!

littlestar:
Can you explain to me, why "sim900.print(“ATH\r”) ignored?

why do you think it is ignored? you send your text all the time so your loop keeps looping

And i need to delete “delay(10)” in the void loop?

your start of the loop does this if (SIM900.available() > 0)  inchar = SIM900.read(); and then all the time goes check inchar for ‘0’ etc

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

J-M-L: why do you think it is ignored? you send your text all the time so your loop keeps looping

your start of the loop does this if (SIM900.available() > 0)  inchar = SIM900.read(); and then all the time goes check inchar for '0' etc

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

I am a newbie. My knowledge is not much Can you please correct the program? I will learn more at this site later. Thanks!

littlestar: I am a newbie. My knowledge is not much Can you please correct the program? I will learn more at this site later. Thanks!

What JML is saying is..... your code only does a check for 1 single condition, which is inchar == '0'. All the other 'if' tests are contained inside that 'if' condition.

What you probably want to do is something like...

if (inchar == '0') {
 *do whatever here*
   another_char=SIM900.read();
 *more if statements here if you want*
}

else if (inchar == '1') {
 *do something else here*
   some_another_char=SIM900.read();
 *more if statements here if you want*
}
else
{
 *do some optional thing here*
}

Also.....depending on what you want to do.... look into 'switch' and 'case' operations.

Can you please correct the program? I will learn more at this site later.

sorry no this is not the end goal of this forum.

Thank all!

I need to learn more and more

You really need to restructure the way you do the reading

J-M-L: You really need to restructure the way you do the reading

I dont see your mean Can you show me an example? Thanks!

littlestar: I dont see your mean Can you show me an example? Thanks!

It means.... at the moment..... you only have one single 'if' statement that tests for inchar == '1'. All of the other 'if' statements are contained inside your first 'if' statement.

What you will need to do is to learn about "if" statements.

Eg .... read 1 character from the buffer and store that character into a variable, such as firstchar. IF that character is '1', THEN read another character from the buffer and store that character into another variable, such as secondchar. At this time, you are still working inside the first 'IF' statement. Consider this to be a menu system. So.... initially, the software detects '1' (ie. firstchar is '1'), and then you read another character .... secondchar. So you can then work on the second character .... ie. if (secondchar == 'a'). If secondchar really is an 'a', then you could then read another character... and store that character as 'thirdchar'.... and continue on.

Your code needs to be written so that your program functions (operates) in the way that you need it to work. To learn all about this, you'll need to work through programming tutorials ..... involving 'if' 'else if' 'else'. And also maybe learn about 'switch' and 'case'.

With menu systems, you need to learn how to get INTO a situation.....and learn how to get OUT of (exit) a situation. In this sort of programming..... it involves being logical and systematic.....and house-keeping.

Thank you very much!