simalrow 0 Posted May 30, 2015 Share Posted May 30, 2015 (edited) WHY IS THIS If statement NOT WORKING? Hi,I added some code to the example program to send an alphaumeric string to com port to identify the input but the iF statement does not work as expected and cannot understand why ? It always goes to else line regardless of buttonState being 0 or 1Advice would be appreciatedCheers /* DigitalReadSerial with on-board Pushbutton Reads a digital input on pin 5, prints the result to the serial monitor Harware Required: * MSP-EXP430G2 LaunchPad This example code is in the public domain. */ // digital pin 5 has a pushbutton attached to it. Give it a name: int pushButton = 5; // the setup routine runs once when you press reset: void setup() { // initialize serial communication at 4800 bits per second: Serial.begin(4800); // msp430g2231 must use 4800 // make the on-board pushbutton's pin an input pullup: pinMode(pushButton, INPUT_PULLUP); } // the loop routine runs over and over again forever: void loop() { // read the input pin: int buttonState = digitalRead(pushButton); // print out the state of the button: Serial.print(buttonState); if (buttonState =0) { Serial.print("p1"); } else { Serial.print("P1"); } delay(1000); // delay in between reads for stability } Edited May 30, 2015 by bluehash [ADMIN] Please code tags next time. Easier to read! Quote Link to post Share on other sites
abecedarian 330 Posted May 30, 2015 Share Posted May 30, 2015 Try if (buttonState == 0)Further advice:- use code tags when posting code. You can do that with the button labeled "<>" in the toolbar above. simalrow 1 Quote Link to post Share on other sites
RROMANO001 6 Posted May 30, 2015 Share Posted May 30, 2015 WHY IS THIS If statement NOT WORKING? Hi,I added some code to the example program to send an alphaumeric string to com port to identify the input but the iF statement does not work as expected and cannot understand why ? It always goes to else line regardless of buttonState being 0 or 1 ____ snip ____ Serial.print(buttonState); if (buttonState =0) ----- snip ------- Hi , as abcedarian wrote too, this is a typical beginner but seldom non beginner too hard to find on big code. Compiler generate a warning sometimes many of us ignore then program behaviour appear as strange, comparison in this case is substituted by an assignment, this assignment on c forever return the assigned value. If returned value is zero is equivalent to false, if not zero it is true so your code forever execute as if(0) or if(FALSE). Hope this can help understand better. simalrow 1 Quote Link to post Share on other sites
simalrow 0 Posted May 31, 2015 Author Share Posted May 31, 2015 Thank you, all good now. Is there a way to automatically filter only the compiler warning messages from the large list of messages produced ? Apologies for being a newbie with Energia Quote Link to post Share on other sites
simalrow 0 Posted May 31, 2015 Author Share Posted May 31, 2015 P.S. this is the compiler warning message I received for the original code, still a bit confusing to me:- DigitalReadSerial.ino: In function 'void loop()':DigitalReadSerial.ino:31:24: warning: suggest parentheses around assignment used as truth value [-Wparentheses]DigitalReadSerial.ino: In function 'void loop()':DigitalReadSerial.ino:31:24: warning: suggest parentheses around assignment used as truth value [-Wparentheses] Quote Link to post Share on other sites
rockets4kids 204 Posted May 31, 2015 Share Posted May 31, 2015 The simple solution is to always place the constant on the left hand side. This will always force an error if you use = instead of == RROMANO001, simalrow and abecedarian 3 Quote Link to post Share on other sites
RROMANO001 6 Posted May 31, 2015 Share Posted May 31, 2015 Thank you, all good now. Is there a way to automatically filter only the compiler warning messages from the large list of messages produced ? Apologies for being a newbie with Energia I am also newbie on energia, I am using under Linux so I leave a console open where I can see error messages, I suppose you can open a cmd widows if you are on win or same console on IOS, anyway you can browse messages and find warning and errors or simply select all, copy and paste on an editor then analyse with search. If you also installed CCS import sketch on CCS, error console list warning then you get on line with a double click on error line. simalrow 1 Quote Link to post Share on other sites
RROMANO001 6 Posted May 31, 2015 Share Posted May 31, 2015 P.S. this is the compiler warning message I received for the original code, still a bit confusing to me:- DigitalReadSerial.ino: In function 'void loop()': DigitalReadSerial.ino:31:24: warning: suggest parentheses around assignment used as truth value [-Wparentheses]DigitalReadSerial.ino: In function 'void loop()': DigitalReadSerial.ino:31:24: warning: suggest parentheses around assignment used as truth value [-Wparentheses] I need help from you, please locate where is the line 32 column 24, post that line and I see what can be. I also suppose this come from if statement where an integer value returned instead of boolean. When test is for zero I use if(!variable) instead of if(variable==0) !variable is equivalent to variable==0 variable is equivalent to variable!=0 in your case it read as if(!buttonState) Preferences are left to user, if you use buttonPressed then it is better assign correct value to variable: buttonPressed = !digitalRead(pushButton); Take care of another possible set of error: Logic function: They return true false generally 1 and 0 and test for zero or non zero == equality != exor ! NOT && And || Or Bitwise logic, they act bit for bit to variable so: ~ NOT revert bit for bit (if zero return oxffff.... if non zero return non zero) & bitwise and, if both variable are different from zero but on different bits it return zero | bitwise or, it return zero if both are false but non zero instead of 1 ^ bitwise xor Precedence of operator are not the same, bitwise are better to be surrounded by parentheses. simalrow 1 Quote Link to post Share on other sites
cde 334 Posted May 31, 2015 Share Posted May 31, 2015 warning: suggest parentheses around assignment used as truth value This is explaining what went wrong. An if statement has two parts, the if command telling the microcontroller to run a test, and the test condition. Here you don't use a test condition. Instead, you use an assignment. "buttonState =0" That is, you are assigning 0 to buttonState. Common mistake. You need to use a test comparator. That's two equal signs in a row. buttonState == 0. Honestly, that both assignment and "is equal to" have similar characters is as dumbas the constant use of X as a variable in math when multiplication symbol is x. It is just asking for legibility issues like this, but its hard to change decades of coding habits. simalrow 1 Quote Link to post Share on other sites
abecedarian 330 Posted May 31, 2015 Share Posted May 31, 2015 Since you are testing a variable against being equal to 0 [zero], you could do if (buttonState) //check buttonState { // do something because buttonState is not 0 } else { // do something else because buttonState is 0 } simalrow 1 Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.