What am I doing wrong with logical and?

Hello everyone, first post!

I am trying to set up an infrared remote to control some relays and I can't seem to figure out how to get logical and working.

My code:

    if (results.value == IR_TOGGLE) {
      static int m = HIGH;
      m = !m;
      //digitalWrite(RELAY_I, m);
      //digitalWrite(RELAY_II, m);
      digitalWrite(RELAY_I && RELAY_II, m);
      results.value = 0x00000000;
    }

That is in my loop().

So, if I uncomment the first two digitalWrite lines and I comment out the third, I get the results I want (both relays turning off and on with each key press).

The third digitalWrite line, as is, only controls RELAY_I and not RELAY_II. I am able to control RELAY_II with a second key, though.

What am I doing wrong here? Thanks!

do you think digitalWrite can take arguments more than 2...
you are intending to give it 3 arguments
see this:
https://www.arduino.cc/reference/en/language/functions/digital-io/digitalwrite/
logical and is used as conditions...what do you think you are using it as??
better use it this way
digitalWrite(RELAY_I, m);
digitalWrite(RELAY_II, m);
why would you use
digitalWrite(RELAY_I && RELAY_II, m);
even if it was right...
lemme know what you did

digitalWrite(RELAY_I && RELAY_II, m); Not valid.

Check the syntax for digitalWrite().

It's valid :wink: But it's not what OP wants :slight_smile:

&& will result in true or false (1 or 0) so the statement either sets pin 1 or pin 0 to the value stored in m.

Why would you ever think that would work? digitalWrite needs to know WHICH pin number to set to either LOW (0V) or HIGH (5V). Logical AND returns either true (1) or false (0). So all digitalWrite could ever do with your code is write either pin 1 or pin 0. How is that useful?

Regards,
Ray L.

Alright, so I misunderstood how to use AND with digitalWrite. I plan on flashing my code to a 'tiny85 and I'm trying to compact my code as much as I am able. I removed the line in question.Thanks everyone!

You can use direct port manipulation. One statement to control both relays.

Question
Why do you use a static variable inside an if?

You can use direct port manipulation. One statement to control both relays.

Question
Why do you use a static variable inside an if?

could you please tell me what is port manipulation...
a newbie here :slight_smile:

Google is your friend.

Google: port manipulation arduino

See:
https://www.arduino.cc/en/Reference/PortManipulation

If you had posted your code, we wouldn't have to ask: which pins are RELAY_I && RELAY_II on?

Google is your friend.

Google: port manipulation arduino

See:
Arduino - PortManipulation

thanks brah!
i forgot to mention that am a moron

Good night Mr. M. :slight_smile:

“i forgot to mention that am a moron”

No you are not.

There is a lot out there to learn, little by little things become clearer.

On the way, have fun!