If Statement Does Not Work! What Am I Doing Wrong? - Arduino Forum
Maybe your like
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
anon73444976 August 24, 2021, 6:53pm 2Try 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.
wolfray99 August 24, 2021, 6:57pm 3Thank 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.
But my question. Did i write the If statement correct? Should it work like this??
regards
anon73444976 August 24, 2021, 6:58pm 4Those 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.
wolfray99 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 &&?
anon73444976 August 24, 2021, 7:04pm 6Well, 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.
wolfray99 August 24, 2021, 7:08pm 7Well 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.
anon73444976 August 24, 2021, 7:11pm 8It 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
cattledog August 24, 2021, 7:13pm 9Those 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.
wolfray99 August 24, 2021, 7:16pm 10But if i use .indexof it shoudl work i presume, But in my opinion i wrote the IF with the two conditions wrong.
anon73444976 August 24, 2021, 7:17pm 11But if i use .indexof it shoudl work i presume
Presume all you like. I see no indexOf.
I'll leave you now
wolfray99 August 24, 2021, 7:19pm 12Well goodluck and thank you for your patience .....
okayelectronics August 24, 2021, 7:20pm 13Hi 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); } wolfray99 August 24, 2021, 7:26pm 14((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!
ToddL1962 August 24, 2021, 7:30pm 15I 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() { } anon73444976 August 24, 2021, 7:37pm 16I 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 firstdigiadd August 25, 2021, 8:51am 17All 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
UKHeliBob August 25, 2021, 8:55am 18@firstdigiadd has been suspended for posting spam
Let that be a lesson to others
anon35827816 August 25, 2021, 8:58am 19Instead 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.
wolfray99 August 25, 2021, 9:36am 20Actualy 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 →Related topics
| 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 |
Tag » Arduino If Else Statement Not Working
-
If And Else Statements, If Statement Not Working - Arduino Forum
-
[SOLVED] If Statement Not Working With String - Arduino Forum
-
If Else Statements Not Executing As Expected - Arduino Forum
-
Why Does "if...else If...else" Not Work, But "if...if...else" Does?
-
2nd If Statement Not Working - Arduino Forum
-
If Statement Not Working - Programming Questions - Arduino Forum
-
IF Statement Doesn't Work Properly - Arduino Forum
-
If Statement Not Working - Programming Questions - Arduino Forum
-
If-else Statement Not Working - Arduino Stack Exchange
-
Arduino IDE: Conditional(if-else-if) Statements - STEMpedia
-
Arduino Project Issues With IF Statements - Stack Overflow
-
Arduino If-else Statement - Linux Hint
-
2.7 Understanding If / Else And While Statement In Arduino
-
Tutorial 11: If Statement (and Else-if), Comparison Operators And ...