title: how to read char array from serial monitor and command arduino accordingly?
body: <p>currently, I am working on a project to read char inputs from serial monitor and command Arduino to switch on/off specific pins. The problem I am facing is, I am unable to read the complete char array entered in the serial monitor. can anyone tell me what am I doing wrong?</p>
  
  <pre><code>#define X 13 //led pin
  
  char txt[15]; 
  int i; 
  int Status=0;
  
  void setup() {   // put your setup code here, to run once:  
  pinMode(X,OUTPUT);// setting the pin flow of control as output
  
   Serial.begin(9600);  
   while(!Serial)   
   {
      ;  //to wait for pc to connect  
   }   
   Serial.println("\nHome Automation");   
   dashprint();  
  
  
  }
  
  void loop() {   // put your main code here, to run repeatedly:  
  if(Serial.available()&gt;0)    
  {   i=0;
      while(Serial.available()&gt;0)   //if serial available
      { char inchar=Serial.read();
        txt[i]=inchar; // add char to txt string
        i++;// increment to where to write next
        txt[i]='\0'; //null termination
  
       }    
    Serial.print(txt);    
    check();
    } 
    }
  
  void dashprint() //to print dashes 
  {
  Serial.println("-----------------------------------------------");  
  Serial.println("give me some command"); //ask for command    
    }
  
  void check() 
  {    if(strncmp(txt,"ON",2)==0)   
        {
           digitalWrite(X,HIGH);
           Status=1;
            }   
        else if(strncmp(txt,"OFF",3)==0)  
         { digitalWrite(X,LOW);
           Status=0;
            }   
        else if(txt=="STATUS")  
         { 
             } 
        else Serial.println("ERROR");
     }
  </code></pre>
  
  <p>output:</p>
  
  <h2>Home Automation</h2>
  
  <p>give me some command
  OERROR
  NERROR</p>
  
  <p>ERROR</p>
  
  <p>expected output:</p>
  
  <h2>Home Automation</h2>
  
  <p>give me some command
  ON</p>