IF

Hello, i’m trying make a start and stop basic like in automation. I have this code for do, and my problem is, i need do someone condition in same if for example this:

if (pin1 = true) and pin2 = true) or (pin1 = true and pin9 = true))
{
digitalWrite(9, HIGH);
}
}

How do i make this?

Thanks and excuse me my english.

The assignment operator is =. The equality operator is ==. They are NOT interchangeable.

&& is the logical and operator. || is the logical or operator.

I think your "=" is the problem - not enough of them. https://www.arduino.cc/en/Reference/If

juanrs196: Hello, i'm trying make a start and stop basic like in automation. I have this code for do, and my problem is, i need do someone condition in same if for example this:

if (pin1 = true) and pin2 = true) or (pin1 = true and pin9 = true)) { digitalWrite(9, HIGH); } }

How do i make this?

Thanks and excuse me my english.

No need to apologize for anything HERE.

You create " compound condition "

if( (pin1 && pin2) || ( pin1 && pin9)) ...

of course you replace the pinX with digitalRead(pinX)

The parentheses are strictly not necessary , but makes code more readable / user friendly . You could get more creative knowing that "compound condition " is evaluated "from left to right" and the evaluation is terminated when true.

In your case - pin9 will not be checked if pins 1 and 2 are true.

Have fun coding.

if( pin1 && (pin2 || pin9) )

Here’s a motor start / stop sketch that I use (pushbuttons connected between GND and input pins):

const byte ledPin = 13,
           startPin = 4,
           stopPin = 5;
bool motorRun = false;           
void setup()
{
  pinMode(motorPin,OUTPUT);
  pinMode(startPin,INPUT_PULLUP);
  pinMode(stopPin,INPUT_PULLUP); 
}
void loop()
{
  motorRun = digitalRead(stopPin) && (!digitalRead(startPin)
    || motorRun);
  digitalWrite(ledPin,motorRun); 
}

You can use the words and and or in place of && and || and IMHO they make code easier to understand

...R

outsider: Here's a motor start / stop sketch that I use (pushbuttons connected between GND and input pins):

const byte ledPin = 13,
           startPin = 4,
           stopPin = 5;
bool motorRun = false;           
void setup()
{
  pinMode(motorPin,OUTPUT);
  pinMode(startPin,INPUT_PULLUP);
  pinMode(stopPin,INPUT_PULLUP); 
}
void loop()
{
  motorRun = digitalRead(stopPin) && (!digitalRead(startPin)
    || motorRun);
  digitalWrite(ledPin,motorRun); 
}

Please be careful when NOT using parentheses in compound condition. Sorry , I may have mislead you in my original post. && has precedence over || ( left to right) and the above compound condition MAY not evaluate the way you want it to.