 # DI 4bit counter and DO

Hi everyone!
I’m just a few days new to Arduino, the project I’m on there is a section that I always fail to program.
There are 4 DI that should work as a binary counter 0 to 15, they shall be read all the time.
Then there are DO that 2 of them need to activate depending on the value in the counter (i.e. if all DI read 0000 then DO 1 and 2 should be the only actives, if the counter reads 0001 then DO 3 and 4 should be the only actives… and so on).
I believe my code is not stable enought reading the inputs and does some crazy stuff, probably I’m using too much code and it can be simplified by the use of some array or similar but I still can’t figure out how to do this.

Well, you have my sympathy. Things like that have happened to me too.

nenodc: I believe my code is not stable enought reading the inputs and does some crazy stuff, probably I'm using too much code and it can be simplified by the use of some array or similar but I still can't figure out how to do this.

If you still want help from here, please post your code to give people here a chance to troubleshoot it! ;)

here is a link with tips how to speed up getting good answers best regards Stefan

Hi! thanks for your reply. Here is part of the code, I've done some changes and wrote comments in English for easier understanding. I have not my board here during the weekend so I could not test more on my own. As I said it would be great to get advises on how to make the code thinner; as you see I just defined for when the counter is 0 and I'll need to get to 18, that will be a hell of lines!.

``````int InBit1 = 6;           // Input bit 1 connected to digital pin 6
int InBit2 = 7;           // Input bit 2 connected to digital pin 7
int InBit3 = 8;           // Input bit 3 connected to digital pin 8
int InBit4 = 9;           // Input bit 4 connected to digital pin 9
int InBit5 = 10;          // Input bit 5 connected to digital pin 10
int ValInBit1 = 0;        // Variable to store the bit 1 read value
int ValInBit2 = 0;        // Variable to store the bit 2 read value
int ValInBit3 = 0;        // Variable to store the bit 3 read value
int ValInBit4 = 0;        // Variable to store the bit 4 read value
int ValInBit5 = 0;        // Variable to store the bit 5 read value
int ValInBitsReal = 0;    // Variable to store the five bits real number
int Out1A = 16;           // Output 1 block A
int Out2A = 17;           // Output 2 block A
int Out3A = 18;           // Output 3 block A
int Out4A = 19;           // Output 4 block A
int Out5A = 20;           // Output 5 block A
int Out6A = 21;           // Output 6 block A
int Out7A = 22;           // Output 7 block A
int Out8A = 23;           // Output 8 block A
int Out9A = 24;           // Output 9 block A
int Out10A = 25;          // Output 10 block A
int Out11A = 26;          // Output 11 block A
int Out12A = 27;          // Output 12 block A
int Out13A = 28;          // Output 13 block A
int Out14A = 29;          // Output 14 block A
int Out15A = 30;          // Output 15 block A
int Out16A = 31;          // Output 16 block A
int Out17A = 32;          // Output 17 block A
int Out18A = 33;          // Output 18 block A
int Out1B = 34;           // Output 1 block B
int Out2B = 35;           // Output 2 block B
int Out3B = 36;           // Output 3 block B
int Out4B = 37;           // Output 4 block B
int Out5B = 38;           // Output 5 block B
int Out6B = 39;           // Output 6 block B
int Out7B = 40;           // Output 7 block B
int Out8B = 41;           // Output 8 block B
int Out9B = 42;           // Output 9 block B
int Out10B = 43;          // Output 10 block B
int Out11B = 44;          // Output 11 block B
int Out12B = 45;          // Output 12 block B
int Out13B = 46;          // Output 13 block B
int Out14B = 47;          // Output 14 block B
int Out15B = 48;          // Output 15 block B
int Out16B = 49;          // Output 16 block B
int Out17B = 50;          // Output 17 block B
int Out18B = 51;          // Output 18 block B

void setup() {
// Setup code, run once:
pinMode(InBit1, INPUT);   // Set the digital pin 6 as input
pinMode(InBit2, INPUT);   // Set the digital pin 7 as input
pinMode(InBit3, INPUT);   // Set the digital pin 8 as input
pinMode(InBit4, INPUT);   // Set the digital pin 9 as input
pinMode(InBit5, INPUT);   // Set the digital pin 10 as input
pinMode(Out1A, OUTPUT);   // Set the digital pin 16 as output
pinMode(Out2A, OUTPUT);   // Set the digital pin 17 as output
pinMode(Out3A, OUTPUT);   // Set the digital pin 18 as output
pinMode(Out4A, OUTPUT);   // Set the digital pin 19 as output
pinMode(Out5A, OUTPUT);   // Set the digital pin 20 as output
pinMode(Out6A, OUTPUT);   // Set the digital pin 21 as output
pinMode(Out7A, OUTPUT);   // Set the digital pin 22 as output
pinMode(Out8A, OUTPUT);   // Set the digital pin 23 as output
pinMode(Out9A, OUTPUT);   // Set the digital pin 24 as output
pinMode(Out10A, OUTPUT);  // Set the digital pin 25 as output
pinMode(Out11A, OUTPUT);  // Set the digital pin 26 as output
pinMode(Out12A, OUTPUT);  // Set the digital pin 27 as output
pinMode(Out13A, OUTPUT);  // Set the digital pin 28 as output
pinMode(Out14A, OUTPUT);  // Set the digital pin 29 as output
pinMode(Out15A, OUTPUT);  // Set the digital pin 30 as output
pinMode(Out16A, OUTPUT);  // Set the digital pin 31 as output
pinMode(Out17A, OUTPUT);  // Set the digital pin 32 as output
pinMode(Out18A, OUTPUT);  // Set the digital pin 33 as output
pinMode(Out1B, OUTPUT);   // Set the digital pin 34 as output
pinMode(Out2B, OUTPUT);   // Set the digital pin 35 as output
pinMode(Out3B, OUTPUT);   // Set the digital pin 36 as output
pinMode(Out4B, OUTPUT);   // Set the digital pin 37 as output
pinMode(Out5B, OUTPUT);   // Set the digital pin 38 as output
pinMode(Out6B, OUTPUT);   // Set the digital pin 39 as output
pinMode(Out7B, OUTPUT);   // Set the digital pin 40 as output
pinMode(Out8B, OUTPUT);   // Set the digital pin 41 as output
pinMode(Out9B, OUTPUT);   // Set the digital pin 42 as output
pinMode(Out10B, OUTPUT);  // Set the digital pin 43 as output
pinMode(Out11B, OUTPUT);  // Set the digital pin 44 as output
pinMode(Out12B, OUTPUT);  // Set the digital pin 45 as output
pinMode(Out13B, OUTPUT);  // Set the digital pin 46 as output
pinMode(Out14B, OUTPUT);  // Set the digital pin 47 as output
pinMode(Out15B, OUTPUT);  // Set the digital pin 48 as output
pinMode(Out16B, OUTPUT);  // Set the digital pin 49 as output
pinMode(Out17B, OUTPUT);  // Set the digital pin 50 as output
pinMode(Out18B, OUTPUT);  // Set the digital pin 51 as output

}

void loop() {
// Main code, run repeatedly:

if (ValInBit1 == HIGH)
ValInBitsReal += 1;
if (ValInBit2 == HIGH)
ValInBitsReal += 2;
if (ValInBit3 == HIGH)
ValInBitsReal += 4;
if (ValInBit4 == HIGH)
ValInBitsReal += 8;
if (ValInBit5 == HIGH)
ValInBitsReal += 16;
return (ValInBitsReal);

if (ValInBitsReal == 0)
digitalWrite(Out1A, LOW);   // Sets the digital pin 16 OFF
digitalWrite(Out2A, LOW);   // Sets the digital pin 17 OFF
digitalWrite(Out3A, LOW);   // Sets the digital pin 18 OFF
digitalWrite(Out4A, LOW);   // Sets the digital pin 19 OFF
digitalWrite(Out5A, LOW);   // Sets the digital pin 20 OFF
digitalWrite(Out6A, LOW);   // Sets the digital pin 21 OFF
digitalWrite(Out7A, LOW);   // Sets the digital pin 22 OFF
digitalWrite(Out8A, LOW);   // Sets the digital pin 23 OFF
digitalWrite(Out9A, LOW);   // Sets the digital pin 24 OFF
digitalWrite(Out10A, LOW);  // Sets the digital pin 25 OFF
digitalWrite(Out11A, LOW);  // Sets the digital pin 26 OFF
digitalWrite(Out12A, LOW);  // Sets the digital pin 27 OFF
digitalWrite(Out13A, LOW);  // Sets the digital pin 28 OFF
digitalWrite(Out14A, LOW);  // Sets the digital pin 29 OFF
digitalWrite(Out15A, LOW);  // Sets the digital pin 30 OFF
digitalWrite(Out16A, LOW);  // Sets the digital pin 31 OFF
digitalWrite(Out17A, LOW);  // Sets the digital pin 32 OFF
digitalWrite(Out18A, LOW);  // Sets the digital pin 33 OFF
digitalWrite(Out1B, LOW);   // Sets the digital pin 34 OFF
digitalWrite(Out2B, LOW);   // Sets the digital pin 35 OFF
digitalWrite(Out3B, LOW);   // Sets the digital pin 35 OFF
digitalWrite(Out4B, LOW);   // Sets the digital pin 37 OFF
digitalWrite(Out5B, LOW);   // Sets the digital pin 38 OFF
digitalWrite(Out6B, LOW);   // Sets the digital pin 39 OFF
digitalWrite(Out7B, LOW);   // Sets the digital pin 40 OFF
digitalWrite(Out8B, LOW);   // Sets the digital pin 41 OFF
digitalWrite(Out9B, LOW);   // Sets the digital pin 42 OFF
digitalWrite(Out10B, LOW);  // Sets the digital pin 43 OFF
digitalWrite(Out11B, LOW);  // Sets the digital pin 44 OFF
digitalWrite(Out12B, LOW);  // Sets the digital pin 45 OFF
digitalWrite(Out13B, LOW);  // Sets the digital pin 46 OFF
digitalWrite(Out14B, LOW);  // Sets the digital pin 47 OFF
digitalWrite(Out15B, LOW);  // Sets the digital pin 48 OFF
digitalWrite(Out16B, LOW);  // Sets the digital pin 49 OFF
digitalWrite(Out17B, LOW);  // Sets the digital pin 50 OFF
digitalWrite(Out18B, LOW);  // Sets the digital pin 51 OFF
}
``````

Does it work? No? What happens that shouldn't happen? What shouldn't happen that does happen?

I would suggest that you learn about array variables ('arrays') before you go too far with this current code.

nenodc:
Hi! thanks for your reply. Here is part of the code, I’ve done some changes and wrote comments in English for easier understanding. I have not my board here during the weekend so I could not test more on my own. As I said it would be great to get advises on how to make the code thinner; as you see I just defined for when the counter is 0 and I’ll need to get to 18, that will be a hell of lines!.

using arrays would make it so much simpler! something like this maybe…
(compiles, NOT tested!)

``````const int OUTPUT_BLOCK_SIZE = 18; //number of outputs per output block
const int INPUT_BITS = 5; //number of inputs bits/digital inputs
int InBits[INPUT_BITS] = {6, 7, 8, 9, 10}; // Input digital pins (b0 - b4)
int ValInputBits[INPUT_BITS];        // Variable to store the bit 1 read value
int OutA[OUTPUT_BLOCK_SIZE] = {16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33}; //block A Output digital pins
int OutB[OUTPUT_BLOCK_SIZE] = {34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51}; //block B Output digital pins

void setup() {
// Setup code, run once:
for (int i = 0; i < INPUT_BITS; ++i) {
pinMode(InBits[i], INPUT);   // Set the digital pin as input
}

for (int j = 0; j < OUTPUT_BLOCK_SIZE; ++j) {
pinMode(OutA[j], OUTPUT);   // Set the digital pin as Output
pinMode(OutB[j], OUTPUT);   // Set the digital pin as Output
}
}

void loop() {
// Main code, run repeatedly:
int decimalVal = 0;    // Variable to store combine the five bits

for (int i = 0; i < INPUT_BITS; ++i) {
}

for (int j = 0; j < INPUT_BITS; ++j) {
decimalVal |= ValInputBits[j] << j; // combine all the bits to get the decimal value
}

//reset all blocks to LOW
for (int k = 0; k < OUTPUT_BLOCK_SIZE; ++k) {
digitalWrite(OutA[k], LOW);   // Sets the digital pin OFF
digitalWrite(OutB[k], LOW);   // Sets the digital pin OFF
}

//check where decimal value is greater than 0 but less than OUTPUT_BLOCK_SIZE
if (decimalVal > 0 && decimalVal <= OUTPUT_BLOCK_SIZE) {
--decimalVal; //subtract 1 (array starts from zero!)
digitalWrite(OutA[decimalVal], HIGH);   // Sets the digital pin ON
digitalWrite(OutB[decimalVal], HIGH);   // Sets the digital pin ON
}
}
``````

FYI is the snippet you posted, I think this line is/was the issue and should be removed/commented out:

``````return (ValInBitsReal);
``````

hope that helps…

Hi, Lets get back to basics. Did you write your code in stages to test your input and output circuits.

How have you got your digital inputs wired? Are they switches, do you have pulldown or pullup resistors with each switch? Can you please post a circuit diagram of your project? [u]Have you verified with simple code that you are reliably reading the inputs?[/u]

Thanks.. Tom... :)