Jump to content
43oh

Why does this code's iF statement not work


Recommended Posts

 
 

 

clear.gifclear.gifclear.gif
WHY IS THIS If statement NOT WORKING?
clear.gifclear.gifclear.gifclear.gif
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

Advice would be appreciated

Cheers
/*
  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 by bluehash
[ADMIN] Please code tags next time. Easier to read!
Link to post
Share on other sites
clear.gifclear.gifclear.gif
WHY IS THIS If statement NOT WORKING?
clear.gifclear.gifclear.gifclear.gif
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.

Link to post
Share on other sites

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]

Link to post
Share on other sites

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.

Link to post
Share on other sites

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.

Link to post
Share on other sites

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.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...