Array of functions (addressing multiple identical sensors)

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):

#include "DHT.h"
#define DHTPIN 2     // what pin we're connected to
#define DHTTYPE DHT11   // DHT 11
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600); 
  dht.begin();
}

void loop() {
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  Serial.print("Humidity: "); 
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: "); 
  Serial.print(t);
  Serial.println(" *C");
}

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 ?

Any help is seriously appreciated.

/O.

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.

You could always try this:

DHT myDHT[MaxSensors] = {DHT(0, 11),DHT(1, 11),DHT(2, 11),DHT(3, 11),DHT(4, 11)};

EDIT: Sorry, those should have been in Capitals, fixed now.

I used the library from Adafruit, on github, see this link GitHub - adafruit/DHT-sensor-library: Arduino library for DHT11, DHT22, etc Temperature & Humidity Sensors.
It has a class DHT, not dht. The example there uses dht as the instance, not DHT:

DHT dht(pin, 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.

int pins[MaxSensors] = { 3, 4, 5, 6, 7 };
DHT *myDHT[MaxSensors];
for (int n=0; n<MaxSensors; n++)
{
    myDHT[n] = new DHT(pins[n], 11); // define a sensor at pin 'n' with sensor type DHT11
}
for (int n=0; n<MaxSensors; n++)
{
  float h = myDHT[n]->readHumidity();
  float t = myDHT[n]->readTemperature();
}

ETA: If the DHT class has a copy constructor, I think you could also do:

DHT myDHT[MaxSensors] = {
    DHT(3, 11),
    DHT(4, 11),
    DHT(5, 11),
    DHT(6, 11),
    DHT(7, 11)
};

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.

Hmmm -- I think I see where you got your Karma from.
It looks like it compiles without errors, now I have this, which compiles without errors:

#include <Arduino.h>
#include <DHT.h>

const int maxDHT = 5;
DHT* myDHT[maxDHT]; // array of 5 pointers to DHTs
 // DHT aDHT(1,11); // dummy DHT with pin 1 and Type 11
float temp, humid;

void setup(){
  Serial.begin(9600);
  for (int i=0; i<(maxDHT-1); i++) {
    Serial.print(i), Serial.print("\n");
    myDHT[i] = new DHT(i, 11); //define a new DHT at pin 11;
  };
};

void loop(){
  for (int i=0; i<(maxDHT-1); i++) {
    temp=myDHT[i]->readTemperature();
    humid=myDHT[i]->readHumidity();
    delay(2500);
    Serial.print(i), Serial.print(", T = "); Serial.print(temp); Serial.print("\n");
    Serial.print(i), Serial.print(", H = "); Serial.print(humid); Serial.print("\n");
  };
};

Addendum -

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:

maxDHT Bytes
5 6066
6 6068
7 6070
8 6072
n 6056+2*n

Thanks for all your help, people!

O.

So... contrary to many posts on the web, 'new' does exist

For a long time (prior to IDE version 1.0 I suspect), it didn't - hence the confusion caused by older discussions of the topic.

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.

Hello everybody

It seems to me, that I have a similar situation. Unfortunately I am still not quite sure if what I have done works after having read this thread.

Here is how I tried to creat objects and start them for 3xDHT-22 sensors (the variables were declared earlier:

int dhtObjects[3] = {dhtOut, dhtHot, dhtAmbient};
for (int i=0; i<3; i++){
DHT dhtObjects[i] (TempHumPin[i], DHTTYPE);
dhtObjects[i].begin(); // start DHT-22 Objects
} // for

Is it true that a for loop can only be in a function? So in the initial statements I couldn't put a for loop?

Thanks for your help!

moses

This thread is 6 years old. You should probably start a new one. And, for a chance at getting decent help, post your full code.

Is it true that a for loop can only be in a function?

Yes

for (int i=0; i<3; i++){
DHT dhtObjects[i] (TempHumPin[i], DHTTYPE);
dhtObjects[i].begin(); // start DHT-22 Objects
}

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?

Hello PaulS

How are dhtOut, dhtIn, and dhtAmbient defined? Are they REALLY ints?

They should be the variables of the dht-22 objects.

You are creating 3 arrays of objects,

The goal was to:

  • define 3 dht objects
  • and have the pins be attributed to the objects through an array.
  • after the object is defined the three sensors should be startet.

writing outside the bounds of the array each time

How come? Where is the error?

Thank you,

moses

How come?

It's your code. You tell us why you are doing something dumb like that.

Where is the error?
for (int i=0; i<3; i++){
DHT dhtObjects[i] (TempHumPin[i], DHTTYPE);

When i is 0, you are creating an array, dhtObjects[], that can hold no elements. Then you try to write to the first element of the 0 element array.

When i is 1, you are creating an array, dhtObjects[], that can hold one element. Then you try to write to the second element of the 1 element array.

When i is 2, you are creating an array, dhtObjects[], that can hold two elements. Then you try to write to the third element of the 2 element array.

Do I need to go on?

Hello PaulS

Thank you for the reply.

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

moserroger:
Hello PaulS

Thank you for the reply.

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.

Thank you Jiggy-Ninja

I will do that.