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 !
