If statement does not work! What am i doing wrong?

Hello,

I could you some help with this piece of code. The IF statement does not work.

I have searched several topics but i can not find what i am doing wrong.

 if  (smsString == ("Test") && (Ontvanger == ("+3161234567")) )
                 {
                 NummerAlarm = ("+311234567");
                   Serial.println("Relay Aan");             
                 //(!fona.sendSMS((char*) NummerAlarm.c_str(),"#PWD123456#OUT1=ON"));
                 delay(16000);
                 //(!fona.sendSMS((char*) NummerAlarm.c_str(),"#PWD123456#OUT1=OFF"));
                  Serial.println("Relay Uit");
                  Serial.println(callerIDbuffer);
//                 Serial.println("");
             
              } 

              else 
              {
               Serial.println("Does not work");
              }

I would like the following:

If Sms message is "Test" And Test is send by number "+311234567" then serial print "Relay On"

But Serial print always say doesnt work!!

Tried && + and also .equals.

Hope some one could help

Try printing the Strings you are trying to compare to see if they are what you expect them to be, and don't have unexpected non-printing characters, like CR or LF.

Please remember to post all your code.

Thank you for your Awnser. Those values are correct smsString and CallerID "Ontvanger"
Have the right content. if i serial print them.

There are a lot of phone numbers i really not like to publish online. :slight_smile:
But my question. Did i write the If statement correct? Should it work like this??

regards

I guess I'll just have to take your word for that.
You didn't post your code or your test method, so I can't test.

Try to imagine if, as you seem to think, the "if" didn't work.
Now, imagine the chaos.

Good luck.

[quote="wolfray99, post:1, topic:898853"]
if (smsString == ("Test") && (Ontvanger == ("+3161234567")) )
[/quote](

I would only like to know if i wrote the IF statement correct.. with the () and the &&?

Well, I wouldn't have used quite so many ( ), but yes, assuming you know that all the datatypes are correct

I can't tell, because YOU DIDN'T POST ALL YOUR CODE.

Well maybe you could show me how you would write that little line of code! Maybe that gives me a idea . Instead of me copy paste my cpompleet code and need to alter 65 phone numbers!! and personal data.

It isn't about that "little line of code", it's about the data.

I don't know how you got to so much code that you didn't test a simple case

The CR/LF characters will not print. You need to serial print byte by byte to confirm that the additional characters are not there.

But if i use .indexof it shoudl work i presume, But in my opinion i wrote the IF with the two conditions wrong.

Presume all you like. I see no indexOf.

I'll leave you now

Well goodluck and thank you for your patience .....

Hi there

here is one simplified example for you. You can see the code and the serial terminal outputs as well:

Also, refer to

https://www.arduino.cc/reference/tr/language/variables/data-types/string/

Here is the code section

String smsString, Ontvanger, testmsg, testnumber;

void setup() {

  Serial.begin(115200);

  smsString = String("Test");

  Ontvanger = String("+3161234567");

  testmsg = String("Test");

  testnumber = String("+3161234567");

}

void loop() {

  if  ((smsString == testmsg) && (Ontvanger == testnumber) )

  {

    Serial.println("Relay Aan");

    delay(1000);

    Serial.println("Relay Uit");

  }

  else

  {

    Serial.println("Does not work");

  }

  delay(1000);

}

Thank you very much. As i mentioned all the strings where filled with the right data..
I just needed this

((smsString == testmsg) && (Ontvanger == testnumber) )

I know understand what i did wrong

Thank you again!

I'll bite. Here is a test you could have done yourself. It prints to the serial console Works. Which means, of course, you have a problem in the portion of the code which you did not provide.

String smsString = "Test";
String Ontvanger = "+3161234567";

void setup()
{
  Serial.begin(115200);  
  if (smsString == ("Test") && (Ontvanger == ("+3161234567")) )
  {
    Serial.println("Works");
  }
  else
  {
    Serial.println("Doesn't");
  }
}

void loop()
{
}

Doesn't seem likely from what you've told us.

Remember: the first rule of Dunning-Kruger Club is you don't know you're a member of Dunning-Kruger Club.

void setup()
{
  Serial.begin (115200);
  String Ontvanger = "12345678";
  String smsString = "Silly";

  if (Ontvanger == "12345678" && smsString == "Silly") {
    Serial.println ("All good");
  } else {
    Serial.println ("Fail");
  }
}

void loop(){}

(Like I said, not so many ( ) )

1 Like

All the replies on this solution is really amazing and learning for me. As I am beginner In programming but by reading all the replies will really worth it.
Cheers guys and thanks a lot

Moderator edit : removed spam link

@firstdigiadd has been suspended for posting spam

Let that be a lesson to others

Alarm bells are ringing if you're hard coding dozens of phone numbers and names into your code. Not so much a privacy issue, but a total lack of efficiency in how you've set up your code. I bet your if statement isn't really the problem. You have very fundamental problems in the core concept of your program.

Actualy it was. I needed to change my code because of a other idea of achieving what i want.
i never used if statement with two conditions. And all i needed was a point in the right direction.

And i now have found it by some replies! especially [okayelectronics] with out nagging he pointed me in the right direction.

So thank you all for your help. But i really do not like how some people respond here. So i will leave this forum. And wish you all the best!