4-to-16 line decoder/demultiplexer

Hi all
I am working on a 4 -16 decoder for my Yaesu 991A band select for low pass filter.
Would like some help on cleaning up the code and some help turning it into a library.

#include <Arduino.h>
    /* Writen by -------------------- 5/14/2023                      */
    /* 4 to 16 Decoder            */
    /* Writen for the Yaesu FT-991A Band selection for LPF Switching*/
    /* All inputs !!!DO NOT to exceed 3V !!! use voltage divider on inputs*/
    /* All outputs use transiters to switch your relays*/
    
    int A_IN, B_IN, C_IN, D_IN, OF_IN, zro, one, two, thr, fou, fiv, six, sev, eig, nin, ten, elv, twv, tht, frt, fit, SELECTED;

    /* Decoder input pins*/
    int A_Pin = 25;    // Input A for decoding
    int B_Pin = 27;    // Input B for decoding
    int C_Pin = 31;    // Input C for decoding
    int D_Pin = 33;    // Input D for decoding
    
     /* On/Off state input pin if not needed set to -1*/
    int OF_Pin = 35;    // Input pin for on/off state of input device

    /* Decoder output pins if not needed set to -1*/
    int zro_Pin = -1;    
    int one_Pin = 43;   
    int two_Pin = 45;   
    int thr_Pin = 47;   
    int fou_Pin = 49;   
    int fiv_Pin = 51;   
    int six_Pin = -1;  
    int sev_Pin = -1;   
    int eig_Pin = -1;   
    int nin_Pin = -1;   
    int ten_Pin = -1;   
    int elv_Pin = -1;   
    int twv_Pin = -1;   
    int tht_Pin = -1;   
    int frt_Pin = -1;   
    int fit_Pin = -1;  
    int ofs = 0;

void setup() {  // put your setup code here, to run once:

    // Sets pin for input or output
    pinMode(A_Pin, INPUT);    
    pinMode(B_Pin, INPUT);    
    pinMode(C_Pin, INPUT);    
    pinMode(D_Pin, INPUT);    
    pinMode(OF_Pin, INPUT);

    pinMode(zro_Pin, OUTPUT);    
    pinMode(one_Pin, OUTPUT);    
    pinMode(two_Pin, OUTPUT);    
    pinMode(thr_Pin, OUTPUT);   
    pinMode(fou_Pin, OUTPUT);    
    pinMode(fiv_Pin, OUTPUT);
    pinMode(six_Pin, OUTPUT);    
    pinMode(sev_Pin, OUTPUT);    
    pinMode(eig_Pin, OUTPUT);    
    pinMode(nin_Pin, OUTPUT);   
    pinMode(ten_Pin, OUTPUT);    
    pinMode(elv_Pin, OUTPUT); 
    pinMode(twv_Pin, OUTPUT);    
    pinMode(tht_Pin, OUTPUT);   
    pinMode(frt_Pin, OUTPUT);    
    pinMode(fit_Pin, OUTPUT); 

    
}

void loop() {
  // put your main code here, to run repeatedly:


    A_IN = digitalRead(A_Pin); //State of input A
    B_IN = digitalRead(B_Pin); //State of input B
    C_IN = digitalRead(C_Pin); //State of input C
    D_IN = digitalRead(D_Pin); //State of input D
    OF_IN = digitalRead(OF_Pin); //State of input device On/Off
    

  // Decode the input 0 - 15
    if (A_IN == LOW && B_IN == LOW && C_IN == LOW && D_IN == LOW) {SELECTED = 0;} 
    if (A_IN == LOW && B_IN == LOW && C_IN == LOW && D_IN == HIGH) {SELECTED = 1;} 
    if (A_IN == LOW && B_IN == LOW && C_IN == HIGH && D_IN == LOW) {SELECTED = 2;} 
    if (A_IN == LOW && B_IN == LOW && C_IN == HIGH && D_IN == HIGH) {SELECTED = 3;} 
    if (A_IN == LOW && B_IN == HIGH && C_IN == LOW && D_IN == LOW) {SELECTED = 4;} 
    if (A_IN == LOW && B_IN == HIGH && C_IN == LOW && D_IN == HIGH) {SELECTED = 5;} 
    if (A_IN == LOW && B_IN == HIGH && C_IN == HIGH && D_IN == LOW) {SELECTED = 6;} 
    if (A_IN == LOW && B_IN == HIGH && C_IN == HIGH && D_IN == HIGH) {SELECTED = 7;} 
    if (A_IN == HIGH && B_IN == LOW && C_IN == LOW && D_IN == LOW) {SELECTED = 8;}  
    if (A_IN == HIGH && B_IN == LOW && C_IN == LOW && D_IN == HIGH) {SELECTED = 9;} 
    if (A_IN == HIGH && B_IN == LOW && C_IN == HIGH && D_IN == LOW) {SELECTED = 10;} 
    if (A_IN == HIGH && B_IN == LOW && C_IN == HIGH && D_IN == HIGH) {SELECTED = 11;}
    if (A_IN == HIGH && B_IN == HIGH && C_IN == LOW && D_IN == LOW) {SELECTED = 12;}
    if (A_IN == HIGH && B_IN == HIGH && C_IN == LOW && D_IN == HIGH) {SELECTED = 13;}
    if (A_IN == HIGH && B_IN == HIGH && C_IN == HIGH && D_IN == LOW) {SELECTED = 14;}
    if (A_IN == HIGH && B_IN == HIGH && C_IN == HIGH && D_IN == HIGH) {SELECTED = 15;}
    
    
    
    
  //Set pin state on or off
    if (SELECTED == 1){digitalWrite(zro_Pin, HIGH);} else {digitalWrite(zro_Pin, LOW);}
    if (SELECTED == 2){digitalWrite(one_Pin, HIGH);} else {digitalWrite(one_Pin, LOW);}
    if (SELECTED == 3){digitalWrite(two_Pin, HIGH);} else {digitalWrite(two_Pin, LOW);}
    if (SELECTED == 4){digitalWrite(thr_Pin, HIGH);} else {digitalWrite(thr_Pin, LOW);}
    if (SELECTED == 5){digitalWrite(fou_Pin, HIGH);} else {digitalWrite(fou_Pin, LOW);}
    if (SELECTED == 6){digitalWrite(fiv_Pin, HIGH);} else {digitalWrite(fiv_Pin, LOW);}
    if (SELECTED == 7){digitalWrite(six_Pin, HIGH);} else {digitalWrite(six_Pin, LOW);}
    if (SELECTED == 8){digitalWrite(sev_Pin, HIGH);} else {digitalWrite(sev_Pin, LOW);}
    if (SELECTED == 9){digitalWrite(eig_Pin, HIGH);} else {digitalWrite(eig_Pin, LOW);}
    if (SELECTED == 10){digitalWrite(nin_Pin, HIGH);} else {digitalWrite(nin_Pin, LOW);}
    if (SELECTED == 5){digitalWrite(ten_Pin, HIGH);} else {digitalWrite(ten_Pin, LOW);}
    if (SELECTED == 6){digitalWrite(elv_Pin, HIGH);} else {digitalWrite(elv_Pin, LOW);}
    if (SELECTED == 7){digitalWrite(twv_Pin, HIGH);} else {digitalWrite(twv_Pin, LOW);}
    if (SELECTED == 8){digitalWrite(tht_Pin, HIGH);} else {digitalWrite(tht_Pin, LOW);}
    if (SELECTED == 9){digitalWrite(frt_Pin, HIGH);} else {digitalWrite(frt_Pin, LOW);}
    if (SELECTED == 10){digitalWrite(fit_Pin, HIGH);} else {digitalWrite(fit_Pin, LOW);}
    if (OF_IN == HIGH){ofs = 1;} else {ofs = 0;}
    

}

Thank you Jim

what is "4 -16 decoder"?

4 - 16 decoder checks the state of 4 input pins rather there high or low then decodes the input to tell witch of the 16 output pins to turn on.

just like this IC ?

is your board Mega2560? is it allowed to change pin numbers?

Yes but I want to use a Arduino due to decode instead of using a chip. I am making a control board for an RF amplifier. The Arduino due will also be displaying Watts out, reflect, SWR, temperature and more.
Hardware: Arduino due
Display : 2.4" oled SSD1309_128X64
I believe you can use any digital pin you assign I maybe wrong about that.
That is why I am here to make shore thing are correct.
When it comes to coding for Arduino I know enough to get myself in trouble lol.

const byte Input_Pin = {  16,   17,    18,     19     };
const byte OUTput_Pins = {54, 69}; //from to
int OF_Pin = 35;    // Input pin for on/off state of input device

void setup() {
  for (byte i = OUTput_Pins[0]; i <= OUTput_Pins[1]; i++)pinMode(i, OUTPUT);
}

void loop() {
  uint32_t portA = PINA;
  byte InputByte = byte((portA >> 10) & 0x0F);
  for (byte i = OUTput_Pins[0]; i <= OUTput_Pins[1]; i++)digitalWrite(i, LOW);
  digitalWrite(InputByte + OUTput_Pins[0], HIGH);
  ofs = digitalRead(OF_Pin);
}

Wow shortened up the code big time.
Will test the code.
Thank you

I don't need to use all 16 output pins.
Can I set
const byte OUTput_Pins[] = {54, 69}; to const byte OUTput_Pins[] = {-1, 54, 56, 58, 60, 62}; 6-15 to -1 for none uses at this time?
I believe I miss under stud you when you asked if the pins can be changed.
I would like to be able to change witch pins are being used for what.

Hello OM oldtimmer

Post a block diagram of the hardware setup used.

Here is a PDF of block diagram I hope this is what you wanted.
I am only using 5 pins for the LPF but need to be able to read the state of 10 outputs from the decoder.
I Would like to be able to read all 16 states so I can add features later.
In my code I used the word SELECTED so I could tell the decoder witch pin to turn on if pin35 was low witch would be manual mode.
I am making a shield that will plug into the Due and using the pins on the end header for the Decoder makes it easier when it comes to running traces.

block diagram amp.pdf (13.3 KB)

Nope, byte only does a range from 0-255.
Use int instead, it has a range from -32767 to 32767.

i take this pins because they are on one port and are consequent, for arduino maping and for µC register. if you will using only 5 outputs, you can just forget to use other, but if you will use other pins for other purpose, for example 68/69 for CAN bus, the sketch need little modification. that why we always asking full code, schematic and setup diagram.

upd. I forgot that so was at beginning, but then i made array and now is unimportant what pin is wanted as output.

-1 for pin not used at this time.

how about doing nothing about pins that not used?

const byte Input_Pin[4] = {  16,   17,    18,     19     };
const byte OUTput_Pins[5] = {54, 56, 58, 60, 62};

void setup() {
  for (byte i = 0; i < 5; i++)pinMode(OUTput_Pins[i], OUTPUT);
}

void loop() {
  byte InputByte = byte((REG_PIOA_PDSR >> 10) & 0x0F);
  for (byte i = 0; i < 5; i++)digitalWrite(OUTput_Pins[i], LOW);
  if (InputByte && InputByte < 6)digitalWrite(OUTput_Pins[InputByte - 1], HIGH);
}
/*
   0001 - 54
   0010 - 56
   0011 - 58
   0100 - 60
   0101 - 62
*/

UPD.: oops, by at91samd they named completely different its ports registers

kolaha
What are your #include's.
error: 'PINA' was not declared in this scope is it a char, int, bool ?

updated. now it compiles.

Thank you very much

Got most of it working.
kolaha your help sent me in a different direction and 2 nights of little sleep Thank you. You helped
more then you know.
The problem I can take the digital output pins high but can't seem to pull them back low.
Something is wrong with my (for if statement).
Any help would be appreciated.

#include <Arduino.h>
#include <SPI.h>
#include <U8g2lib.h>
    /* Writen by ---------------- 5/14/2023  With help from the Arduino forms Thank you all */
    /* 4-to-16 line decoder/demultiplexer          */
    /* Writen for the Yaesu FT-991A Band selection for LPF Switching*/
    /* All inputs !!!DO NOT to exceed 3V !!! use voltage divider on inputs*/
    /* All outputs use transiters to switch your relays*/
    U8G2_SSD1309_128X64_NONAME2_F_4W_SW_SPI u8g2(U8G2_R0, /* clock=*/ 13, /* data=*/ 11, /* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 8);    
    
    int OF_IN;
    /* Decoder input pins*/
    int Input_Pin[] = {25, 27, 29, 31};
    int pinCount = 4;
    int port_Out ;
    int port_on;    
    /* Decoder output pins if not needed set to -1*/
    int Output_Pin[] = {-1, 43, 45, 47, 49, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
    int poutCount = 16;
        
     /* On/Off state input pin if not needed set to -1*/
    int OF_Pin = 35;    // Input pin for on/off state of input device
    int ofs = 0;
    
void setup() {  // put your setup code here, to run once:

    u8g2.begin();
    //Serial.begin(9600);
    
     // Sets pin for input
    for (int thisPin = 0; thisPin < pinCount; thisPin++) {pinMode(Input_Pin[thisPin], INPUT);}
    pinMode(OF_Pin, INPUT);

    // Sets pin for output
    for (int thisPin = 0; thisPin < poutCount; thisPin++) {pinMode(Output_Pin[thisPin], OUTPUT);}
     
}

void loop() {
  
    u8g2.firstPage();
    //Read the 4 input pins port_Out 0 to 15
    port_Out = (digitalRead(Input_Pin[0]) & 0x1) | ((digitalRead(Input_Pin[1]) & 0x1)<<1) | ((digitalRead(Input_Pin[2]) & 0x1) << 2) |((digitalRead(Input_Pin[3]) & 0x1)<< 3);
    OF_IN = digitalRead(OF_Pin); //State of input device On/Off
     
  //Set pin state on or off
    for (int i = 0; i < poutCount; i++){if (i != port_Out); digitalWrite(Output_Pin[port_Out], LOW);}
    if (port_Out > 0){digitalWrite(Output_Pin[port_Out], HIGH);}
      
    if (OF_IN == HIGH){ofs = 1;} else {ofs = 0;}
   do {
    u8g2.setFontDirection(0);
    u8g2.setFont(u8g2_font_6x13_tr);
    u8g2.setCursor(0, 13);
    u8g2.print(port_Out);
   
  } while ( u8g2.nextPage() );
     

}

Thank you Jim

Found the problem.

for (int i = 0; i < poutCount; i++){if (i != port_Out); digitalWrite(Output_Pin[port_Out], LOW);}
to
for (int i = 0; i < poutCount; i++){if (i != port_Out); digitalWrite(Output_Pin[i], LOW);}

Amazing what 1 letter can do lol.
Thanks to all for the help.