10 Replies Latest reply on Apr 2, 2014 3:47 PM by userc_41378

If statement and &&

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:
• 1. Re: If statement and &&
Well the picture isn't the whole code, but the only thing you need of it :)
• 2. Re: If statement and &&

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

• 3. Re: If statement and &&

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

Have you looked at using shadow registers ?

Regards, Dana.

• 4. Re: If statement and &&
I made it work by using 2 if statements after each other. I am not satisfied with that solution :D 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
• 5. Re: If statement and &&
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"
• 6. Re: If statement and &&
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.
• 7. Re: If statement and &&
Im off again for the next 8 hours.
• 8. Re: If statement and &&

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

Attached.

Regards, Dana.

• 9. Re: If statement and &&

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.

• 10. Re: If statement and &&
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