Hi all -
I'm trying to build a small temperature monitor, with an UnoR3 and 5 DHT11 temperature/humidity sensors. These are 3-wire digital thingies (5V, GND, data). I'd like to have the code sort-of generic, but can't figure out how to set it up properly.
The library uses something like this (simplified from original example):
DHT is a class for the sensor and defined in the DHT library.
dht(pin,type) is a constructor for the sensor and defined in the DHT library.
This good for one sensor, but now I'd like to do the same for 3 or 4 or 5...
Something like this:
const int MaxSensors = 5;
DHT myDHT[MaxSensors]; // an array[0..4] of sensors
void setup() {
for (int n=0; n<MaxSensors-1; n++) {
myDHT[n] = dht(n, 11); // define a sensor at pin 'n' with sensor type DHT11
}
}
void loop(){
// etc.
}
However, I can't get this to work. So ...
Given a given class DHT with a constructor dht(pin, type), how do I create an array of sensors with pins 0,1,2,3,4 and type 'whatever' in Arduino ?
Given a given class DHT with a constructor dht(pin, type), how do I create an array of sensors with pins 0,1,2,3,4 and type 'whatever' in Arduino ?
How about a link to this DHT library? The one on the playground does not have a class called DHT (it's called dht, and, yes, case matters) nor does it have a two argument constructor.
Edit: You could always add a no-argument constructor and methods to set the pin and type.
I've come up with the following, which compiles. Not sure if this is the right way to go, or if it's garbage.
#include <Arduino.h>
#include <DHT.h>
const int maxDHT = 5;
DHT* myDHT[maxDHT]; // array of 5 pointers to DHTs
void setup(){
Serial.begin(9600);
for (int i=0; i<(maxDHT-1); i++) {
Serial.print(i), Serial.print("\n");
DHT aDHT(i, 11); //define a new DHT at pin i with type 11;
myDHT[i]=&aDHT; // store pointer to aDHT in array myDHT at index i
};
};
void loop(){
// do some stuff with it here
};
ottehoman:
Given a given class DHT with a constructor dht(pin, type), how do I create an array of sensors with pins 0,1,2,3,4 and type 'whatever' in Arduino ?
The simplest approach I can see is to declare an array of pointer-to-dhc and 'new' each dht object in setup.
ottehoman:
I've come up with the following, which compiles. Not sure if this is the right way to go, or if it's garbage.
Serial.begin(9600);
for (int i=0; i<(maxDHT-1); i++) {
Serial.print(i), Serial.print("\n");
DHT aDHT(i, 11); //define a new DHT at pin i with type 11;
myDHT[i]=&aDHT; // store pointer to aDHT in array myDHT at index i
};
The problem with that code is that the aDHT instances go out of scope at the end of the for loop and so the array of pointers will become invalid.
So... contrary to many posts on the web, 'new' does exist in avr-gcc (under Arduino-1.0.1 in my case). That makes things a lot easier. It seems the latest version does indeed compile, and code size seems to scale with maxDHT:
Why are you using 'i<(maxDHT-1)' everywhere? If maxDHT is the number of DHTs then the condition should be 'i < maxDHT'. If it isn't, then perhaps you should be defining some other constant which is.
You are creating 3 arrays of objects, and then writing outside the bounds of the array each time. The arrays of objects go out of scope immediately, so the whole for loop is a waste of effort.
int dhtObjects[3] = {dhtOut, dhtHot, dhtAmbient};
How are dhtOut, dhtIn, and dhtAmbient defined? Are they REALLY ints?
I understand your explanation, but I dont know how to change it or declare it correctly. Is it better if I put:
for (int i=0; i<3; i++){
DHT dhtObjects[] (TempHumPin[i], DHTTYPE);
Leaving the array size undefined? Instead of i?
Thank you.
moses
Don't put the array declaration inside the for loop. Create the array outside the loop with whatever size you need, then put the for loop in setup() to fill it up.