why is the strcmp function not working here?Please help me out

Hello everyone.
I am working on a small project to control an led from my android device using a bluetooth module.I have installed an app named S2 terminal for bluetooth on my android.But the strcmp function does not seem to work here and i cannot identify the problem.

#include "string.h"

int ledgreen=9;

int tx=1;
int rx=0;
char inSerial[15];

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

pinMode(ledgreen, OUTPUT);

pinMode(tx, OUTPUT);
pinMode(rx, INPUT);
allpinslow();
}

void loop(){

int i=0;

delay(500);
if (Serial.available() > 0) {
while (Serial.available() > 0) {
inSerial*=Serial.read();*

  • i++; *
  • }*
    _ inSerial*='\0';_
    Check_Protocol(inSerial);
    _
    }}*_

void allpinslow()
{
digitalWrite(ledgreen, LOW);
*} *

void Check_Protocol(char inStr[]){
_
int i=0;
_
* int m=0;*
* Serial.println(inStr);*

* if(!strcmp(inStr,"gon")){ //Ledgreen ON*
* allpinslow();*
* digitalWrite(ledgreen, HIGH);*
* Serial.println("Green ON");*
* for(m=0;m<11;m++){*
* inStr[m]=0;}*
* i=0;}*
else if(!strcmp(inStr,"goff")){ //Ledgreen OFF
* allpinslow();*
* digitalWrite(ledgreen, LOW);*
* Serial.println("Green OFF");*
* for(m=0;m<11;m++){*
* inStr[m]=0;}*
* i=0;}*

* else{*
* for(m=0;m<11;m++){*
* inStr[m]=0;*
* }*
* i=0;*
}}

please use code tags.... half of the code is now italic :wink:

What is the output of the sketch?

Please post the input also.

You can still change your post and add code tags around the code.

See #7 of How to use this forum

  pinMode(tx, OUTPUT);
  pinMode(rx, INPUT);

Why ?

What have you got the line ending set to in the Serial Monitor ? If it is CR, LF or both then they will be included in the received string.

strcmp() returns 0 if the strings match, and returns +1 or -1 depending on which string comes first in the dictionary if they do not match. You are assuming that true means that they match. That is wrong.

@PaulS: where did you see that?

tarandeep:

 if(!strcmp(inStr,"gon")){      //Ledgreen ON

}
else if(!strcmp(inStr,"goff")){      //Ledgreen OFF

but probably the follow would work better anyway (if there is more in the buffer)

 if(!strncmp(inStr, "gon", 3)){      //Ledgreen ON

} else if(!strncmp(inStr, "goff", 4)){      //Ledgreen OFF

where did you see that?

I missed the ! in that statement. I never write calls to strcmp() that way.

if(strcmp("this", "that") == 0)
{
   // The computer is broken
}

is just easier for me to follow.

PaulS:
strcmp() returns 0 if the strings match, and returns +1 or -1 depending on which string comes first in the dictionary if they do not match. You are assuming that true means that they match. That is wrong.

almost right , the spec - http://www.cplusplus.com/reference/cstring/strcmp/ - states

strcmp() compare the chars at subsequent positions and returns the diff if unequal

int strcmp(const char* s1, const char* s2)
{
    while(*s1 && (*s1==*s2))
        s1++,s2++;
    return *(const unsigned char*)s1-*(const unsigned char*)s2;
}

Assume this done to minimize footprint / maximize performance.

I do not know of any application that uses this difference (as it is not known at which position it is)

OK. I should have said strcmp() returns A positive or negative value, depending on the dictionary order,if the two strings are not equal.

I agree that using the return value to do anything useful, other then in the positive, zero, or negative sense for sorting, would be unusual.

The -1 0 1 of the compare function return values are used for sorting.

This will never work:

char inSerial[15];
...

       inSerial=Serial.read();
             i++; 
       }
       inSerial='\0';

You need to index into the array inSerial[].

@jremington: the [ i ] were probably interpreted and removed outside the code tags.

Thanks. I wasted only a minute or two.

@jremington: I had the supected that error too, even tried to make the OP add the tags to remove that ambiguity, but failed.
I still go for the strncmp. :wink:

With the array index added to the original code it works as long as only gon or goff are sent. If an LF or CR are included then, no surprise, it doesn't.