If statement and &&

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable
        Hello I cant make this simple code work: if ((PRT0DR & pin5) && (startknap_lys==0)){startknap_lys=1;PRT0DR |= pin3;while(PRT0DR & pin5){}} It only has to check in input port and a value in an int. If I remove the integer it works fine. I have made it work before with checking two variables in an if statement. Both with & or &&. This has worked before: if ((value<=15)&&(value>=5)&&(skygge==1)){} And this aswell: if( (LDR_1 < 51) & (LDR_2 < 51) & (LDR_3 < 51) & (LDR_4 < 51) ) //Checking the light intensity on all 4 LDR´s So I cant seem to figure out why it doesn't want to work now. Though this is the first time I check both and input port and a variable and not only variables. Thanks And for the record, my code works, just not when I add the variable. This is the whole code:   
0 Likes
10 Replies
Anonymous
Not applicable
        Well the picture isn't the whole code, but the only thing you need of it 🙂   
0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

It would be better to post the complete project, so we all can have a look at. Use Designer's function "Archive Project" and upload the resulting .zip here (DO NOT use chrome, it won't work!)

   

 

   

Bob

0 Likes
Anonymous
Not applicable

& is a bitwise AND, && is a logical AND.

   

 

   

Have you looked at using shadow registers ?

   

 

   

    

   

         

   

http://www.cypress.com/?rid=2900    AN2094

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable
        I made it work by using 2 if statements after each other. I am not satisfied with that solution 😄 Apparently you cant check a port and a variable in the same if statement, atleast for my conclusion. And I find that odd and weird and painfull and... but I still like PSOC   
0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable
        Here it is. I wrote the code that doesn't work as a note just beneath the code that works right now. It says: "CODE THAT DOESNT WORK"   
0 Likes
Anonymous
Not applicable
        Hello Dana No I havnt, and I cant do that right this week 🙂 too busy. But I made it work with both & & && xD if you get me. I have to read up on the difference between those two.   
0 Likes
Anonymous
Not applicable
        Im off again for the next 8 hours.   
0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

Here is a good reference for C, Bob I think has another.

   


   

Attached.

   


   

Regards, Dana.

0 Likes
Anonymous
Not applicable

One last comment. I have seen compilers choke, sometimes with no warning or

   

error, on highly compounded staements. In my case embedded compund f()

   

calls. I am prone to writing code like that, but now primarily use simple statments

   

on a line by line basis. Compilers are imperfect tools.

   

 

   

Just a thought.

   


   

Regards, Dana.

0 Likes
Anonymous
Not applicable
        It aint that I don't want to read up on it, it is just that I doesn't have the programming as a job. Also I forget stuff. I learned it once in my education, but my job didn't went that way, so I forget the bitwise thing. I read up on it now, but never can remember everything. What you say about the compiler thing might be right. I cant see how it shouldn't work with my code, and also has reference to other codes written in same way that works. So it might be a compiler problem. Anyway thanks   
0 Likes