DHT22 with 16 CH multiplexer

Hi guys

I’m a basic user of Arduino and have very limited knowledge of programming.
Currently, I’m involving in a project with DHT22 sensors connected to 16CH multiplexer (lack of digital pins).

It is hardly found examples of this. I was following the following link as guidance,

I took the .h and .cpp files from the link and modified as per the discussion in that.
No compiling error in the sketch.
However, I could not get the sensor output.

If anyone can help in this regards will be greatly appreciated.

.h file (The file name changed to dht2 (I have another file with the name of dht).

// 
//    FILE: dht2.h
// VERSION: 0.1.05
// PURPOSE: DHT Temperature & Humidity Sensor library for Arduino
//
//     URL: http://arduino.cc/playground/Main/DHTLib
//
// HISTORY:
// see dht.cpp file
// 

#ifndef dht2_h
#define dht2_h

#if ARDUINO < 100
#include <WProgram.h>
#else
#include <Arduino.h>
#endif

#define DHT_LIB_VERSION "0.1.05"

#define DHTLIB_OK                               0
#define DHTLIB_ERROR_CHECKSUM   -1
#define DHTLIB_ERROR_TIMEOUT    -2
#define ADHTLIB_ERROR_TIMEOUT    -2
#define BDHTLIB_ERROR_TIMEOUT    -2
#define CDHTLIB_ERROR_TIMEOUT    -2
#define DDHTLIB_ERROR_TIMEOUT    -2
#define DHTLIB_INVALID_VALUE    -999

class dht
{
public:
       int read11(uint8_t pin, uint8_t channel);
   int read22(uint8_t pin, uint8_t channel);
       double humidity;
       double temperature;

private:
       uint8_t bits[5];  // buffer to receive data
       int read(uint8_t pin, uint8_t channel);
};
#endif
//
// END OF FILE
//

.cpp file

// assume the multiplexer is connected to Arduino pin 14, you need to add the mux channel in the DHT lib read function
// so you call it something like DHT.read22(14, 5);

//
//    FILE: dht2.cpp
// VERSION: 0.1.05
// PURPOSE: DHT Temperature & Humidity Sensor library for Arduino
//
// DATASHEET: 
//
// HISTORY:
// 0.1.05 fixed negative temperature bug (thanks to Roseman)
// 0.1.04 improved readability of code using DHTLIB_OK in code
// 0.1.03 added error values for temp and humidity when read failed
// 0.1.02 added error codes
// 0.1.01 added support for Arduino 1.0, fixed typos (31/12/2011)
// 0.1.0 by Rob Tillaart (01/04/2011)
// inspired by DHT11 library
//

#include "dht.h"

#define TIMEOUT 10000

/////////////////////////////////////////////////////
//
// PUBLIC
//


// return values:
// DHTLIB_OK
// DHTLIB_ERROR_CHECKSUM
// DHTLIB_ERROR_TIMEOUT
#include "dht2.h"

#define TIMEOUT 10000

//Give convenient names to the control pins
#define CONTROL0 5    
#define CONTROL1 4
#define CONTROL2 3
#define CONTROL3 2

/////////////////////////////////////////////////////
//
// PUBLIC
//


// return values:
// DHTLIB_OK
// DHTLIB_ERROR_CHECKSUM
// DHTLIB_ERROR_TIMEOUT
int dht::read11(uint8_t pin, uint8_t channel)
{
       // READ VALUES
       int rv = read(pin, channel);
       if (rv != DHTLIB_OK)
   {
               humidity    = DHTLIB_INVALID_VALUE; // or is NaN prefered?
               temperature = DHTLIB_INVALID_VALUE; 
               return rv;
       }

       // CONVERT AND STORE
       humidity    = bits[0];  // bit[1] == 0;
       temperature = bits[2];  // bits[3] == 0;

       // TEST CHECKSUM
       uint8_t sum = bits[0] + bits[2]; // bits[1] && bits[3] both 0
       if (bits[4] != sum) return DHTLIB_ERROR_CHECKSUM;

       return DHTLIB_OK;
}

// return values:
// DHTLIB_OK
// DHTLIB_ERROR_CHECKSUM
// DHTLIB_ERROR_TIMEOUT
int dht::read22(uint8_t pin, uint8_t channel)
{
       // READ VALUES
       int rv = read(pin, channel);
       if (rv != DHTLIB_OK)
      {
               humidity    = DHTLIB_INVALID_VALUE;  // invalid value, or is NaN prefered?
               temperature = DHTLIB_INVALID_VALUE;  // invalid value
               return rv;
       }

       // CONVERT AND STORE
       humidity    = word(bits[0], bits[1]) * 0.1;

       if (bits[2] & 0x80) // negative temperature
       {
                       temperature = word(bits[2]&0x7F, bits[3]) * 0.1;
                       temperature *= -1.0;
       }
       else
       {
                       temperature = word(bits[2], bits[3]) * 0.1;
       }

       // TEST CHECKSUM
       uint8_t sum = bits[0] + bits[1] + bits[2] + bits[3];
       if (bits[4] != sum) return DHTLIB_ERROR_CHECKSUM;

       return DHTLIB_OK;
}


int dht::read(uint8_t pin, uint8_t channel)
{
 digitalWrite(CONTROL0, channel & 15); 
 digitalWrite(CONTROL1, channel & 7);  
 digitalWrite(CONTROL2, channel & 3);  
 digitalWrite(CONTROL3, channel & 1); 

       // INIT BUFFERVAR TO RECEIVE DATA
       uint8_t cnt = 7;
       uint8_t idx = 0;

       // EMPTY BUFFER
       for (int i=0; i< 5; i++) bits[i] = 0;

       // REQUEST SAMPLE
       pinMode(pin, OUTPUT);
       digitalWrite(pin, LOW);
       delay(20); 
       digitalWrite(pin, HIGH);
       delayMicroseconds(40);
       pinMode(pin, INPUT);

       // GET ACKNOWLEDGE or TIMEOUT
       unsigned int loopCnt = TIMEOUT;
       while(digitalRead(pin) == LOW)
               if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT;

       loopCnt = TIMEOUT;
       while(digitalRead(pin) == HIGH)
               if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT;

       // READ THE OUTPUT - 40 BITS => 5 BYTES
       for (int i=0; i<40; i++)
       {
               loopCnt = TIMEOUT;
               while(digitalRead(pin) == LOW)
                       if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT;

               unsigned long t = micros();

               loopCnt = TIMEOUT;
               while(digitalRead(pin) == HIGH)
                       if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT;

               if ((micros() - t) > 40) bits[idx] |= (1 << cnt);
               if (cnt == 0)   // next byte?
               {
                       cnt = 7;   
                       idx++;      
               }
               else cnt--;
       }

       return DHTLIB_OK;
}

The sketch

#include "dht2.h"
//#include "dht.h"

dht DHT;

#define CONTROL0 5    
#define CONTROL1 4
#define CONTROL2 3
#define CONTROL3 2
//Mux in "SIG" pin
//int SIG_pin = 8;

//Create arrays for data from the the MUXs
//See the Arduino Array Reference: http://www.arduino.cc/en/Reference/Array
int mux0array[16];
//int mux1array[16];
//int mux2array[16];


#define DHTTYPE DHT22
#define DHT22_PIN 8
//uint8_t pin = {8};
uint8_t channel[8] =  { 0,4 };
//#define DHT22_Channel (0,4)

void setup()
{
  //Set MUX control pins to output
  pinMode(CONTROL0, OUTPUT);
  pinMode(CONTROL1, OUTPUT);
  pinMode(CONTROL2, OUTPUT);
  pinMode(CONTROL3, OUTPUT);
  
  //Open the serial port at 28800 bps
  Serial.begin(9600);
  
  //Set analog pins to digital input  
//  pinMode(8, INPUT);         
  //pinMode(15, INPUT);
  //pinMode(16, INPUT);
  
  //Turn on pullup resistors
  digitalWrite(8, HIGH);       
  //digitalWrite(15, HIGH);
  //digitalWrite(16, HIGH);
}
  

void loop()
{
  //int chk = DHT.read22(DHT22_PIN,DHT22_CHANNEL);
  //int read(uint8_t pin, uint8_t channel);
//int chk = dht.read22(DHT22_PIN, channel[idx] );
  //int read22(DHT22_PIN,DHT22_Channel);
  int chk = DHT.read22(DHT22_PIN, channel[8] );

  float t = DHT.temperature; // Gets the values of the temperature
  float h = DHT.humidity; // Gets the values of the humidity
  
  //This for loop is used to scroll through and store the 16 inputs on the FIRST multiplexer
  for (int i=0; i<16; i++)
  {
    digitalWrite(CONTROL0, (i&15)>>3); 
    digitalWrite(CONTROL1, (i&7)>>2);  
    digitalWrite(CONTROL2, (i&3)>>1);  
    digitalWrite(CONTROL3, (i&1));     
    
    //Read and store the input
    //Since internal pullup is on, the pin goes low on changing, so the value needs to be flipped from 0 to 1 (!)
    mux0array[i] = !digitalRead(8);
  }
  
  
  
  //The following lines are for printing out results of array0
  Serial.print("mux0array: ");
  for (int i=0; i<16; i++)
  {
    Serial.print(mux0array[i]);
    Serial.print("-");
  }
  Serial.println();  //line feed
  delay (1000);
  }

The serial output

mux0array: 0-0-0-0-0-0-0-0-1-1-1-1-1-1-1-1-
mux0array: 0-0-0-0-0-0-0-0-1-1-1-1-1-1-1-1-
mux0array: 0-0-0-0-0-0-0-0-1-1-1-1-1-1-1-1-
mux0array: 0-0-0-0-0-0-0-0-1-1-1-1-1-1-1-1-
mux0array: 0-0-0-0-0-0-0-0-1-1-1-1-1-1-1-1-
mux0array: 0-0-0-0-0-0-0-0-1-1-1-1-1-1-1-1-
mux0array: 0-0-0-0-0-0-0-0-1-1-1-1-1-1-1-1-
mux0array: 0-0-0-0-0-0-0-0-1-1-1-1-1-1-1-1-
mux0array: 0-0-0-0-0-0-0-0-1-1-1-1-1-1-1-1-
mux0array: 0-0-0-0-0-0-0-0-1-1-1-1-1-1-1-1-