/*void setup(){
  pinMode(13, OUTPUT);
  Serial.begin(9600);
}


void loop(){
  Serial.println(analogRead(A5));
  digitalWrite(13, HIGH);
  delay(500);
  digitalWrite(13, LOW);
  Serial.println(analogRead(A5));
  delay(500);
  
}
*/


const int controlPin[] {2, 3, 4, 5}; // control pins s0-s3
#define    mux1Common 6

const int mux2Common = 7;
const int numPins = 5;
const int pins[] {8, 9, 10, 11, 12}; // output pins to train controller
int number = 0; // holds number of the button pressed

void setup() {
  pinMode(6,INPUT_PULLUP);
  pinMode( 7, INPUT_PULLUP);
  for (int i = 0; i < 4; i++) pinMode(controlPin[i], OUTPUT); // four control pins
  for (int i = 0; i < 5; i++) pinMode(pins[i], OUTPUT); // five output pins
  Serial.begin(9600);
  
}

void loop() {
  for (int i = 0; i < 16; i++) { // channels
    for (int s = 0; s < 4; s++) {
      digitalWrite (controlPin[s], bitRead(i, s)); 
      //delay(500);// set four control pins
    }
    if (digitalRead(6)==0) {
      number = i;
    } // any on mux#1 button pressed
    else if (digitalRead( 7)==0) {
      number = i + 16; // any on mux#2 button pressed
      Serial.println("number+16");
    }
    else {
      number = 0; // nothing pressed
      //Serial.println("number=0");
    }
    for (int x = 0; x < 5; x++) {
      digitalWrite(pins[x], bitRead(number, x));
    }
   if (number != 0) 
    {
      delay(50)  ; 
      // delay (250);
    
    Serial.println(number);}

  }
}



/*
    {
  "version": 1,
  "author": "Amit Singh",
  "editor": "wokwi",
  "parts": [
   { "type": "wokwi-arduino-uno", "id": "uno", "top": 118, "left": 48, "attrs": {} },
   { "type": "chip-CD74HC4067", "id": "CD74HC4067", "top": -28.94, "left": 126.8, "attrs": {} },
   {
  "type": "wokwi-led",
  "id": "led1",
  "top": -13.78,
     "left": 52.7,
  "attrs": { "color": "red" }
   }
   ],
  "connections": [
     [
       "CD74HC4067:GND",
       "uno:GND.2",
       "black",
       [ "h34.65", "v-27.94", "h-308.94", "v405.86", "h261.99" ]
     ],
     [ "CD74HC4067:I0", "led1:A", "green", [ "h-43.95", "v20.05" ] ],
     [ "led1:C", "uno:GND.1", "green", [ "v78.76", "h101.27" ] ],
     [ "CD74HC4067:VCC", "uno:5V", "red", [ "h-164.99", "v329.55", "h8.51" ] ],
     [ "CD74HC4067:COM", "uno:10", "yellow", [ "h62.9", "v12.92" ] ],
     [ "CD74HC4067:E", "CD74HC4067:S3", "green", [ "h0" ] ],
     [ "CD74HC4067:GND", "CD74HC4067:S3", "black", [ "h17.37", "v4.98" ] ],
     [ "CD74HC4067:GND", "CD74HC4067:S2", "black", [ "h23.27", "v15.88" ] ],
     [ "CD74HC4067:GND", "CD74HC4067:S1", "black", [ "h32.38", "v26.06" ] ],
     [ "CD74HC4067:GND", "CD74HC4067:S0", "black", [ "h37.92", "v38.03" ] ],
     [ "logic1:GND", "uno:GND.1", "black", [ "h-12.43", "v21.04", "h-98.65" ] ],
     [ "logic1:D7", "CD74HC4067:COM", "green", [ "h-122.05", "v-9.1" ] ],
     [ "logic1:D0", "CD74HC4067:S3", "green", [ "h-13.8", "v-15.2" ] ],
     [ "CD74HC4067:I1", "logic1:D1", "green", [ "h0" ] ],
     [ "CD74HC4067:I2", "logic1:D2", "green", [ "h0" ] ],
     [ "CD74HC4067:I3", "logic1:D3", "green", [ "h0" ] ],
     [ "CD74HC4067:I15", "logic1:D4", "green", [ "h29.66", "v16.62" ] ],
     [ "logic1:D6", "CD74HC4067:I11", "green", [ "h0" ] ],
     [ "logic1:D5", "CD74HC4067:I13", "green", [ "h-26.13", "v-7.02" ] ]
   ],
  "dependencies": {}}*/
multiplexerBreakout