Input Condition Problem

int input_1 = 8;
int input_2 = 9;
int input_3 = 10;
int errorLed = 13;

void setup ()
{
  pinMode (input_1, INPUT);
  pinMode (input_2, INPUT);
  pinMode (input_3, INPUT);

  pinMode (outputLed, OUTPUT);
}

void loop ()
{
  if (digitalRead (input_1) && digitalRead (input_2) || digitalRead (input_1) && digitalRead (input_3) 
        || digitalRead (input_2) && digitalRead (input_3))
  {
    digitalWrite (errorLed, HIGH);
  }
  else
  {
    digitalWrite (errorLed, LOW);
  }
}

`
the If condition is better to be in one line so it’s more understandable

It should be something like this. It can be more compact but if you are new, this is kind of extended so you can understand it better
`

1 Like

Hi
try this code.
I haven’t tested it in use, I just compiled it.

#define relay1  19
#define relay2  20
#define relay3  3
#define relay4  4
#define relay5  5
#define relay6  6
#define relay7  7
#define relay8  8
#define relay9  9
#define relay10  10
#define relay11  11
#define relay12  12
#define relay13  13
#define relay14  14
#define relay15  15
#define relay16  16
#define relay17  17
#define relay18  18


#define switch1  21
#define switch2  22
#define switch3  23
#define switch4  24
#define switch5  25
#define switch6  26
#define switch7  27
#define switch8  28
#define switch9  29
#define switch10  30
#define switch11  31
#define switch12  32
#define switch13  33
#define switch14  34
#define switch15  35
#define switch16  36
#define switch17  37
#define switch18  38

#define LED  39


//-----------------------------------------------------------
void setup() {
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  pinMode(relay3, OUTPUT);
  pinMode(relay4, OUTPUT);
  pinMode(relay5, OUTPUT);
  pinMode(relay6, OUTPUT);
  pinMode(relay7, OUTPUT);
  pinMode(relay8, OUTPUT);
  pinMode(relay9, OUTPUT);
  pinMode(relay10, OUTPUT);
  pinMode(relay11, OUTPUT);
  pinMode(relay12, OUTPUT);
  pinMode(relay13, OUTPUT);
  pinMode(relay14, OUTPUT);
  pinMode(relay15, OUTPUT);
  pinMode(relay16, OUTPUT);
  pinMode(relay17, OUTPUT);
  pinMode(relay18, OUTPUT);

  pinMode(LED, OUTPUT);

  pinMode(switch1, INPUT_PULLUP);
  pinMode(switch2, INPUT_PULLUP);
  pinMode(switch3, INPUT_PULLUP);
  pinMode(switch4, INPUT_PULLUP);
  pinMode(switch5, INPUT_PULLUP);
  pinMode(switch6, INPUT_PULLUP);
  pinMode(switch7, INPUT_PULLUP);
  pinMode(switch8, INPUT_PULLUP);
  pinMode(switch9, INPUT_PULLUP);
  pinMode(switch10, INPUT_PULLUP);
  pinMode(switch11, INPUT_PULLUP);
  pinMode(switch12, INPUT_PULLUP);
  pinMode(switch13, INPUT_PULLUP);
  pinMode(switch14, INPUT_PULLUP);
  pinMode(switch15, INPUT_PULLUP);
  pinMode(switch16, INPUT_PULLUP);
  pinMode(switch17, INPUT_PULLUP);
  pinMode(switch18, INPUT_PULLUP);
}
//-----------------------------------------------------------
void loop() {
  int addBit = 0;                             // Multi sw control
  int cntlBit = 0;                            // Switch count
  int relayControl = 0;                       // Relay control
  int cntlPick = 0;                           // Relay pick control

  for (int i = 21; i < 39; i++)               // Verify port from 21 to 38
  {
    if (digitalRead(i) == false)               // If sw is pressed 
    {
      bitSet(relayControl, cntlBit);          // Set relayControl bit ON index by cntlBit
      addBit = addBit + 1;                    // Add 1 to Multi sw control
    }
    cntlBit++;                                // Increment index cntlBit
  }

  if (addBit > 1)                             // If > 1 mult switch detect
  {
    digitalWrite (LED, HIGH);                 // Turn LED ON
    return;                                   // Return
  }
  if (addBit == 1)                            // If 1 only switch detect
  {
    for (int j = 3; j < 21; j++)              // Select relay from port 3 to 20
    {
      if (bitRead(relayControl, cntlPick) == 1) //  If select bit is ON by index cntlPick
      {
        digitalWrite(j, HIGH);        // turn the relay on (HIGH is the voltage level)
        delay(1000);
        digitalWrite(j, LOW);         // turn the relay on (HIGH is the voltage level)
      }
    }
    cntlPick++;                               // Incremente index cntlPick
  }
  while (1) {}                                // Stop and hold
}
2 Likes

The simple way

// pseudo code
onSwitches = 0;
if(digitalRead(sw1) == on){
  onSwitches++;
}
if(digitalRead(sw2) == on){
  onSwitches++;
}
// and so on for each switch
// finally
if(onSwitches > 1){
// do something
}