Switching 6 output PIN's based on input of 4 other PIN's

Based on the status combination of 4 input PIN's ,I'm trying to get a different output PIN to status HIGH .. ( in the next step to activate a relay )
So I have a kind of matrix/combination of the 4 input PIN's that each can be HIGH or LOW ( 5v DC present or not ) and depending on that combination a specific outpt port should become HIGH while all other remain LOW..
Meanwhile it became clear to be that I'm probably making multiple errors that are not detected by the compiler .... but still I don't get the result I'm looking for.

SKETCH :

// Define input pins
const int INPUT_PINS[] = { 2, 4, 7, 8 };
const int NUM_INPUTS = 4;

// Define output pins for relays
const int OUTPUT_PINS[] = { 3, 5, 6, 9, 10, 11 };
const int NUM_OUTPUTS = 6;

void setup() {
// Initialize input pins as INPUT
for (int i = 0; i < NUM_INPUTS; i++) {
pinMode(INPUT_PINS[i], INPUT);
}

// Initialize output pins as OUTPUT and set them LOW initially (relays off)
for (int i = 0; i < NUM_OUTPUTS; i++) {
pinMode(OUTPUT_PINS[i], OUTPUT);
digitalWrite(OUTPUT_PINS[i], LOW);
}
}

void loop() {
// Read the status of all input pins
// Store them in a single 4-bit integer for easier comparison
//int inputStatus = 0;
//for (int i = 0; i < NUM_INPUTS; i++) {
// Read the pin state (HIGH or LOW) and shift it to the correct bit position
// We reverse the order so pin 5 is the most significant bit for easier reading
//if (digitalRead(INPUT_PINS[i]) == HIGH) {
// inputStatus |= (1 << i); // Use bitwise OR to set the specific bit
//}

// Check specific input combinations and activate the corresponding output
// Only one output should be HIGH at any time
if (checkCombination(LOW, HIGH, LOW, HIGH)) { // Input 1:LOW, 2:HIGH, 3:LOW, 4:HIGH
activateOutput(0); // Output 1
} else if (checkCombination(LOW, HIGH, HIGH, LOW)) { // Input 1:LOW, 2:HIGH, 3:HIGH, 4:LOW
activateOutput(1); // Output 2
} else if (checkCombination(LOW, HIGH, HIGH, HIGH)) { // Input 1:LOW, 2:HIGH, 3:HIGH, 4:HIGH
activateOutput(2); // Output 3
} else if (checkCombination(HIGH, LOW, LOW, LOW)) { // Input 1:HIGH, 2:LOW, 3:LOW, 4:LOW
activateOutput(3); // Output 4
} else if (checkCombination(HIGH, LOW, LOW, HIGH)) { // Input 1:HIGH, 2:LOW, 3:LOW, 4:HIGH
activateOutput(4); // Output 5
} else if (checkCombination(HIGH, LOW, HIGH, LOW)) { // Input 1:HIGH, 2:LOW, 3:HIGH, 4:LOW
activateOutput(5); // Output 6
} else {
// If no specific combination matches, turn all outputs off
activateOutput(-1); // Deactivates all outputs
}

// Small delay to prevent rapid flickering
delay(100);
//}
}

// Helper function to compare current input states with a desired combination
bool checkCombination(int in1, int in2, int in3, int in4) {
// Note: digitalRead returns 1 for HIGH and 0 for LOW, which works with this comparison.

if (digitalRead(INPUT_PINS[0]) == in1 && digitalRead(INPUT_PINS[1]) == in2 && digitalRead(INPUT_PINS[2]) == in3 && digitalRead(INPUT_PINS[3]) == in4) {

return (1);

} else {

return (0);

}
}

// Helper function to turn off all outputs and turn on the specified one
void activateOutput(int outputIndex) {
// Turn all outputs OFF first
for (int i = 0; i < NUM_OUTPUTS; i++) {
digitalWrite(OUTPUT_PINS[i], LOW);
}

// If a valid index is provided, turn that specific output ON
if (outputIndex >= 0 && outputIndex < NUM_OUTPUTS) {
digitalWrite(OUTPUT_PINS[outputIndex], HIGH);
}
}

Can anybody get me on the correct track please ?
Thanks !

Pleasw post the code in tags.

You can use the <CODE/> button in the message composition window tool bar and do what it says

type or paste code here

Or use the Copy for Forum tool in the IDE.

You may want to use the Autoformat tool in the IDE to make your code pretty first.

Your problem is best handled with a table. That will use some language features you may not have come across yet.

What I have read seems plausible, but tedious. Let's see the sketch properly, you may have a simple error somewhere.

a7

Rather than all those if/else statements, a 16 entry lookup table requires only one line of code to determine the output states from the 16 input states.

How often are you checking the pins status? Do all the pins change simultaneously or are you checking values once per second, or some other time frame? Can one pin change value while you are checking the next pin?

// Define input pins (2-5)
const int INPUT_PINS[] = { 2, 4, 7, 8 };
const int NUM_INPUTS = 4;

// Define output pins (8-13) for relays
const int OUTPUT_PINS[] = { 3, 5, 6, 9, 10, 11 };
const int NUM_OUTPUTS = 6;



void setup() {
  // Initialize input pins as INPUT
  for (int i = 0; i < NUM_INPUTS; i++) {
    pinMode(INPUT_PINS[i], INPUT);
  }

  // Initialize output pins as OUTPUT and set them LOW initially (relays off)
  for (int i = 0; i < NUM_OUTPUTS; i++) {
    pinMode(OUTPUT_PINS[i], OUTPUT);
    digitalWrite(OUTPUT_PINS[i], LOW);
  }
}

void loop() {
  // Read the status of all input pins
  // Store them in a single 4-bit integer for easier comparison
  //int inputStatus = 0;
  //for (int i = 0; i < NUM_INPUTS; i++) {
  // Read the pin state (HIGH or LOW) and shift it to the correct bit position
  // We reverse the order so pin 5 is the most significant bit for easier reading
  //if (digitalRead(INPUT_PINS[i]) == HIGH) {
  //  inputStatus |= (1 << i);  // Use bitwise OR to set the specific bit
  //}

  // Check specific input combinations and activate the corresponding output
  // Only one output should be HIGH at any time
  if (checkCombination(LOW, HIGH, LOW, HIGH)) {          // Input 1:LOW, 2:HIGH, 3:LOW, 4:HIGH
    activateOutput(0);                                   // Output 1
  } else if (checkCombination(LOW, HIGH, HIGH, LOW)) {   // Input 1:LOW, 2:HIGH, 3:HIGH, 4:LOW
    activateOutput(1);                                   // Output 2
  } else if (checkCombination(LOW, HIGH, HIGH, HIGH)) {  // Input 1:LOW, 2:HIGH, 3:HIGH, 4:HIGH
    activateOutput(2);                                   // Output 3
  } else if (checkCombination(HIGH, LOW, LOW, LOW)) {    // Input 1:HIGH, 2:LOW, 3:LOW, 4:LOW
    activateOutput(3);                                   // Output 4
  } else if (checkCombination(HIGH, LOW, LOW, HIGH)) {   // Input 1:HIGH, 2:LOW, 3:LOW, 4:HIGH
    activateOutput(4);                                   // Output 5
  } else if (checkCombination(HIGH, LOW, HIGH, LOW)) {   // Input 1:HIGH, 2:LOW, 3:HIGH, 4:LOW
    activateOutput(5);                                   // Output 6
  } else {
    // If no specific combination matches, turn all outputs off
    activateOutput(-1);  // Deactivates all outputs
  }

  // Small delay to prevent rapid flickering
  delay(100);
  //}
}

// Helper function to compare current input states with a desired combination
bool checkCombination(int in1, int in2, int in3, int in4) {
  // Note: digitalRead returns 1 for HIGH and 0 for LOW, which works with this comparison.

  if (digitalRead(INPUT_PINS[0]) == in1 && digitalRead(INPUT_PINS[1]) == in2 && digitalRead(INPUT_PINS[2]) == in3 && digitalRead(INPUT_PINS[3]) == in4) {

    return (1);

  } else {

    return (0);
  }
}

// Helper function to turn off all outputs and turn on the specified one
void activateOutput(int outputIndex) {
  // Turn all outputs OFF first
  for (int i = 0; i < NUM_OUTPUTS; i++) {
    digitalWrite(OUTPUT_PINS[i], LOW);
  }

  // If a valid index is provided, turn that specific output ON
  if (outputIndex >= 0 && outputIndex < NUM_OUTPUTS) {
    digitalWrite(OUTPUT_PINS[outputIndex], HIGH);
  }
}

Thanks,
That should be ok now ... sorry for the trouble ... I'm really 100% new to all this ..

Well done, THX.

So I read and then execute your code…

I did not confirm the accuracy of the translation from your table to your code, but I did observe a few combinations of inputs.

Fiddling with the inputs did change the outputs in a plausible manner.

One change I did make to your code is to use INPUT_PULLUP as the pin mode.

So it's not a major softwar issue, if software at all.

What are you seeing that you shoukd not, that is to ask in what way is it disappointing you now?

How are your switches wired? Are you sure they are wired as switches, and are they pulled up or down externally?

Pro tip: learn about the serial monitor and use it to make your sketches "talk". It is your first and cheapest tool for debugging. You can use it to print the values of key variables and see that they are informing this flow through your code like you want.

a7

See if this helps you.

void setup() {
  pinMode(2, INPUT);
  pinMode(4, INPUT);
  pinMode(7, INPUT);
  pinMode(8, INPUT);
  pinMode(3, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  digitalWrite(3, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
}

void loop() {
// It assigns a weight to each entry and adds it to a variable
  byte status = digitalRead(2);
  status += digitalRead(4) * 2;
  status += digitalRead(7) * 4;
  status += digitalRead(8) * 8;

// Activate the corresponding output according to the value of the variable
  digitalWrite(3, status == 10);
  digitalWrite(5, status == 6);
  digitalWrite(6, status == 14);
  digitalWrite(9, status == 1);
  digitalWrite(10, status == 9);
  digitalWrite(11, status == 5);

  delay(500);
}

Yes, a table driven approach makes it easy to tinker with.

I fed @cw4ever's sketch to an AI and with a little guidance it wrote

// Four input buttons
const int INPUT_PINS[4] = {2, 4, 7, 8};

// Six output LEDs / relays
const int OUTPUT_PINS[6] = {3, 5, 6, 9, 10, 11};

// 16×6 lookup table
// Each row: { LED0, LED1, LED2, LED3, LED4, LED5 }
const int OUTPUT_TABLE[16][6] = {
 {0,0,0,0,0,0},   // 0
 {0,0,0,0,0,1},   // 1
 {0,0,0,0,1,0},   // 2
 {0,0,0,0,1,1},   // 3
 {0,0,0,1,0,0},   // 4
 {0,0,0,1,0,1},   // 5
 {0,0,0,1,1,0},   // 6
 {0,0,0,1,1,1},   // 7
 {0,0,1,0,0,0},   // 8
 {0,0,1,0,0,1},   // 9
 {0,0,1,0,1,0},   // 10
 {0,0,1,0,1,1},   // 11
 {0,0,1,1,0,0},   // 12
 {0,0,1,1,0,1},   // 13
 {0,0,1,1,1,0},   // 14
 {0,0,1,1,1,1},   // 15
};

void setup() {
 Serial.begin(115200);

 for (int i = 0; i < 4; i++) {
   pinMode(INPUT_PINS[i], INPUT_PULLUP);
 }
 for (int i = 0; i < 6; i++) {
   pinMode(OUTPUT_PINS[i], OUTPUT);
 }
}

void loop() {

 // ===== INPUT =====
 // Compute the 0–15 index, no bitwise operators
 int index =
     8 * (digitalRead(INPUT_PINS[0]) == HIGH) +
     4 * (digitalRead(INPUT_PINS[1]) == HIGH) +
     2 * (digitalRead(INPUT_PINS[2]) == HIGH) +
     1 * (digitalRead(INPUT_PINS[3]) == HIGH);

 // ===== PROCESS =====
 // Nothing to do — direct lookup
 // OUTPUT_TABLE[index][0..5] contains the LED states.

 // ===== OUTPUT =====
 for (int i = 0; i < 6; i++) {
   digitalWrite(OUTPUT_PINS[i], OUTPUT_TABLE[index][i]);
 }

 delay(50);
}

Which works fine. Just fill in the output table. I asked my AI to do:

// OUTPUT_TABLE[index][outputPin]
// 16 input combinations → 6 outputs per row.
// A '1' means the corresponding OUTPUT_PINS[x] is driven HIGH.

const int OUTPUT_TABLE[16][6] = {

 // index 0  (0000)  all off
 {0,0,0,0,0,0},

 // index 1  (0001)  all off
 {0,0,0,0,0,0},

 // index 2  (0010)  all off
 {0,0,0,0,0,0},

 // index 3  (0011)  all off
 {0,0,0,0,0,0},

 // index 4  (0100)  all off
 {0,0,0,0,0,0},

 // index 5  (0101)  LOW,HIGH,LOW,HIGH → output 0
 {1,0,0,0,0,0},

 // index 6  (0110)  LOW,HIGH,HIGH,LOW → output 1
 {0,1,0,0,0,0},

 // index 7  (0111)  LOW,HIGH,HIGH,HIGH → output 2
 {0,0,1,0,0,0},

 // index 8  (1000)  HIGH,LOW,LOW,LOW → output 3
 {0,0,0,1,0,0},

 // index 9  (1001)  HIGH,LOW,LOW,HIGH → output 4
 {0,0,0,0,1,0},

 // index 10 (1010)  HIGH,LOW,HIGH,LOW → output 5
 {0,0,0,0,0,1},

 // index 11 (1011)  all off
 {0,0,0,0,0,0},

 // index 12 (1100)  all off
 {0,0,0,0,0,0},

 // index 13 (1101)  all off
 {0,0,0,0,0,0},

 // index 14 (1110)  all off
 {0,0,0,0,0,0},

 // index 15 (1111)  all off
 {0,0,0,0,0,0}
};

a7

Thanks for the help, I'll continue working on this.

Thanks for the help, I'll continue working on this !

What (and how) is connected to your input pins?

Hi, nothing is connected to the PIN's during the test.
I'm just checking if 5v DC is present on a PIN when that PIN is HIGH and 0v when that PIN is LOW.
But, I'm beginning to suspect that this is not correct in that way ...

Hi, @cw4ever
Welcome to the forum.

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

Yes your input connection layout is very important.

Tom.... :smiley: :+1: :coffee: :australia:

This is probably your problem, change that line to:

pinMode(INPUT_PINS[i], INPUT_PULLUP);

Nothing wrong with that, at least for pins that are behaving the way you think.

But if an output is not stable, as your code may be writing HIGH and LOW rapidly, a DVM is less useful than just hanging a resisotr and LED off the pin.

The LED and resistor are a bit more convenient as well.

As @JCA34F points out, leaving inputs floating means the reading is basically random, so you were perhaps getting random results.

a7

To do that you need a pull-down resistor on each input pin, a 10K should be fine.
Do not use INPUT_PULLUP . Just make the pinModes INPUT as you had in your original code.

@cw4ever has four inputs that will each be HIGH or LOW.

This does not require one to choose between pins that are pulled up internally, pulled up externally or pulled down externally.

It is more common to see pulled up inputs, usually using the internal availability. Some who are in the habit use a resistor.

In cases where the weak nature of the internal pull-up means it is inadequate a smaller value resistor is necessary

a7

It's clear I still have some homework to do.
First, learn to walk before I try to run... :face_with_raised_eyebrow:
For simplicity, I'll start with one input and one output pin (for example, two as input and three as output).
Thanks in advance to everyone for their advice and help.

I just reread the whole thread to see if you ever answered the question about how you know when all the pins have been set and there will be no more changes.
I do not see anything telling your program to now test the pins as there will not be any new changes.