Noob question

Hi peeps, my coding skills are rather limited & I need a few pointers. I'm reading 4 digital inputs and, depending on their value, setting 8 digital outputs. As I need to drive 8 outputs from just 4 inputs I'm using some simple binary to handle it. For example:

If input: pin 1=LOW, pin 2=LOW, pin 3=LOW, pin 4=HIGH then that's a binary 1 so set digital output pin 1 to HIGH

And so forth...

Here's my code so far, I'm struggling!

int outPins[] = {2,3,4,5,6,7,8,9}; // array of the output pins connected to the driver board
int inPins[] = {10,11,12,13}; // array of the input pins
int val = 0; // variable for reading the pin status
int inputstatus[] = {10,11,12,13}; // array of the input pin status
int num_outpins = 6; // the number of output pins (i.e. the length of the array)
int num_inpins = 4; // the number of input pins (i.e. the length of the array)

void setup()
{
int i;
for (i = 0; i < num_outpins; i++) // the array elements are numbered from 0 to num_outpins - 1
pinMode(outPins*, OUTPUT); // set each pin as an output*
}
{

  • int g;*
  • for (g = 0; g < num_inpins; g++) // the array elements are numbered from 0 to num_inpins - 1*
    _ pinMode(inPins*, INPUT); // set each pin as an input*_
    }
    void loop(){
    * //check input pins*
    * int f;*
    * for (f = 0; g < num_inpins; f++) // the array elements are numbered from 0 to num_inpins - 1*
    * inputstatus[f] = digitalRead([f]); // read input values*
    * // now set the pins that only have a single value*

* if (inPins(4) == HIGH) { // check if the input is HIGH - that means the corrsponding relay should be turned on*
* digitalWrite(outPins(2), HIGH); // turn output pin 2 On*
* }*
* if (inPins(3) == HIGH) { // check if the input is HIGH - that means the corrsponding relay should be turned on*
* digitalWrite(outPins(3), HIGH); // turn output pin 3 On*

* }*
* if (inPins(2) == HIGH) { // check if the input is HIGH - that means the corrsponding relay should be turned on*
* digitalWrite(outPins(3), HIGH); // turn output pin 3 On*
* }*

* if (inPins(1) == HIGH) { // check if the input is HIGH - that means the corrsponding relay should be turned on*
* digitalWrite(outPins(8), HIGH); // turn output pin 8 On*
* }*

}

}

How about making a number from the binary inputs using something like:-

num = digitalRead(inPins[0]) | (digitalRead(inPins[1]) << 1) | (digitalRead(inPins[2]) << 2) | (digitalRead(inPins[3]) << 3);

the | symbol is a bit wise OR function you could replace it with a + here but that's a bit wimpy.

Then use a case statement on num to set the output to what you want.

(you can simplify the case statement to some algorithem but get your head round this first)

i'd keep an eye on your brackets, [] are used in arrays and () are used for function inputs. your code seems to mix and match them

I like Grumpy_Mike's suggestion.

It's also worth nothing that 4 inputs can generate 16 different possible outcomes, not 8.

Mikal