## Pin check issue

8 posts / 0 new
Author
Message

Ok im not sure why the pin check in the if statement doesn't work. It works fine by itself but not as in the statement below. Can someone tell me why?

im trying to create something like:

```if((1 & 1) != 0)
{
//Do work
}
```
```	uint8_t Check = 0b00000001

if((PORTB & _BV( PB7 ) & (Check & (1<<0))) != 0)
{
//Do work
}
```

[/code]

You need to use PINB, not PORTB.

Regards,
Steve A.

The Board helps those that help themselves.

Try this. I like parens.

```	if (((PINB & _BV(PB7)) && (Check & (1 << 0))) != 0)
{
//Do work
}
```

"I may make you feel but I can't make you think" - Jethro Tull - Thick As A Brick

"void transmigratus(void) {transmigratus();} // recursio infinitus" - larryvc

"It's much more practical to rely on the processing powers of the real debugger, i.e. the one between the keyboard and chair." - JW wek3

"When you arise in the morning think of what a privilege it is to be alive: to breathe, to think, to enjoy, to love." -  Marcus Aurelius

I think you want && between to two statements. As written, the result would evaluate to:
0x80 & 0x01 = 0x00
or
0x00 & 0x01 = 0x00

Thus we can see that the result will never be non-zero.
Using a logical and (&&) it would evaluate as:

true && true = true
false && true = false

This is a common mistake - never confuse bit wise operators with logical operators.

Thanks Koshchi, larryvc & Kartman. I didn't know you could use a PINx command and i wasn't aware about &&.

So
& is bit wise
&& is logical operators (truth table of AND)

Cool.

warren1 wrote:
Cool.
Yeah. :wink:

"I may make you feel but I can't make you think" - Jethro Tull - Thick As A Brick

"void transmigratus(void) {transmigratus();} // recursio infinitus" - larryvc

"It's much more practical to rely on the processing powers of the real debugger, i.e. the one between the keyboard and chair." - JW wek3

"When you arise in the morning think of what a privilege it is to be alive: to breathe, to think, to enjoy, to love." -  Marcus Aurelius

Quote:

I didn't know you could use a PINx

In the context give, it is not "could" but "must".

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]