Hi everyone! This is my first post after spending hours searching for my answer without success. I've only completed some pretty simple projects with the Arduino and I'm trying to get to the next level--you know, from infant to toddler.
Below is working code for my DHT22 Temperature & Humidity sensor:
//
// FILE: dht_test.pde
// PURPOSE: DHT library test sketch for Arduino
// BY: Rob Tillaart http://playground.arduino.cc/Main/DHTLib
#include <dht.h>
dht DHT;
#define DHT11_PIN 4
#define DHT22_PIN 5
void setup()
{
Serial.begin(115200);
Serial.println("DHT TEST PROGRAM ");
Serial.print("LIBRARY VERSION: ");
Serial.println(DHT_LIB_VERSION);
Serial.println();
Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)");
}
void loop()
{
// READ DATA
Serial.print("DHT22, \t");
int chk = DHT.read22(DHT22_PIN);
switch (chk)
{
case DHTLIB_OK:
Serial.print("OK,\t");
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.print("Checksum error,\t");
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.print("Time out error,\t");
break;
default:
Serial.print("Unknown error,\t");
break;
}
// DISPLAY DATA
Serial.print(DHT.humidity, 1);
Serial.print(",\t");
Serial.println(DHT.temperature, 1);
delay(1000);
// READ DATA
Serial.print("DHT11, \t");
chk = DHT.read11(DHT11_PIN);
switch (chk)
{
case DHTLIB_OK:
Serial.print("OK,\t");
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.print("Checksum error,\t");
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.print("Time out error,\t");
break;
default:
Serial.print("Unknown error,\t");
break;
}
// DISPLAT DATA
Serial.print(DHT.humidity,1);
Serial.print(",\t");
Serial.println(DHT.temperature,1);
delay(1000);
}
//
// END OF FILE
//
And here is the sample code for the MuxShield by MayhewLabs Mux Shield | Mayhew Labs
//Mux_Shield_DigitalIn_Example
//http://mayhewlabs.com/arduino-mux-shield
/*
This example shows how to read and store all pins as digital inputs into arrays and print the results over serial.
Switches should be connected from ground to the pin input.
Internal pull-up resistors are used on the inputs, so when a switch is depressed, the reading
changes from 1 to 0, but to correct this logic, the digitalRead function is corrected with '!'
to actually store a 1 when a switch is depressed.
To simplify this code further, one might use nested for loops or function calls.
*/
//Give convenient names to the control pins
#define CONTROL0 5
#define CONTROL1 4
#define CONTROL2 3
#define CONTROL3 2
//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];
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(28800);
//Set analog pins to digital input
pinMode(14, INPUT);
pinMode(15, INPUT);
pinMode(16, INPUT);
//Turn on pullup resistors
digitalWrite(14, HIGH);
digitalWrite(15, HIGH);
digitalWrite(16, HIGH);
}
void loop()
{
//This for loop is used to scroll through and store the 16 inputs on the FIRST multiplexer
for (int i=0; i<16; i++)
{
//The following 4 commands set the correct logic for the control pins to select the desired input
//See the Arduino Bitwise AND Reference: http://www.arduino.cc/en/Reference/BitwiseAnd
//See the Aruino Bitshift Reference: http://www.arduino.cc/en/Reference/Bitshift
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(14);
}
//This for loop is used to scroll through the SECOND 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));
mux1array[i] = !digitalRead(15);
}
//This for loop is used to scroll through the THIRD 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));
mux2array[i] = !digitalRead(16);
}
//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
//The following lines are for printing out results of array1
Serial.print("mux1array: ");
for (int i=0; i<16; i++)
{
Serial.print(mux1array[i]);
Serial.print("-");
}
Serial.println();
//The following lines are for printing out results of array2
Serial.print("mux2array: ");
for (int i=0; i<16; i++)
{
Serial.print(mux2array[i]);
Serial.print("-");
}
Serial.println();
}
I'm stuck at trying to adapt the DHT22 code to read the muxshield. I had the sensor plugged in, the code--what I thought--correct, and the sensor in mux0array[0] and I wasn't receiving anything. I have not been able to find any simple code examples integrating sensors with the mux shield. I'm hoping to grasp how the muxshield works so I'll be able to adapt many sensors to the mux. Any help will be greatly appreciated.
Thanks!!