If Statement Does Not Work! What Am I Doing Wrong? - Arduino Forum

If statement does not work! What am i doing wrong? Projects Programming August 24, 2021, 6:51pm 1

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

August 24, 2021, 6:53pm 2

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.

August 24, 2021, 6:57pm 3

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

August 24, 2021, 6:58pm 4 wolfray99:

Those values are correct smsString and CallerID "Ontvanger"

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.

August 24, 2021, 7:02pm 5

[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 &&?

August 24, 2021, 7:04pm 6

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.

August 24, 2021, 7:08pm 7

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.

August 24, 2021, 7:11pm 8

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

August 24, 2021, 7:13pm 9 wolfray99:

Those values are correct smsString and CallerID "Ontvanger" Have the right content. if i serial print them.

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

August 24, 2021, 7:16pm 10

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

August 24, 2021, 7:17pm 11 wolfray99:

But if i use .indexof it shoudl work i presume

Presume all you like. I see no indexOf.

I'll leave you now

August 24, 2021, 7:19pm 12

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

August 24, 2021, 7:20pm 13

Hi there

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

sketch.ino - Wokwi Arduino and ESP32 Simulator

Run IoT and embedded projects in your browser: ESP32, Arduino, Pi Pico, and more. No installation required!

Also, refer to

String Comparison Operators | Arduino Documentation

Learn how to make alphabetic comparisons between Strings. They are useful for sorting and alphabetizing, among other things.

string - Arduino Reference

The Arduino programming language Reference, organized into Functions, Variable and Constant, and Structure keywords.

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); } August 24, 2021, 7:26pm 14 okayelectronics:

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

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!

August 24, 2021, 7:30pm 15 wolfray99:

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

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() { } August 24, 2021, 7:37pm 16 wolfray99:

I know understand what i did wrong

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 August 25, 2021, 8:51am 17

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

August 25, 2021, 8:55am 18

@firstdigiadd has been suspended for posting spam

Let that be a lesson to others

August 25, 2021, 8:58am 19 wolfray99:

Instead of me copy paste my cpompleet code and need to alter 65 phone numbers!! and personal data.

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.

August 25, 2021, 9:36am 20

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!

next page →
Topic Replies Views Activity
If statement not working with strings General Guidance 9 96 June 4, 2025
Using "if" statement on character within a string Programming 6 542 July 22, 2024
Problem in meinem Projekt mit IF Abfrage Deutsch 18 246 November 2, 2024
[SOLVED]IF gives always true Programming 15 139 December 15, 2024
Why my "IF" statement is not is not working Programming 5 202 October 16, 2023
Unfortunately, your browser is unsupported. Please switch to a supported browser to view rich content, log in and reply.

Tag » Arduino If Else Statement Not Working