Go Down

Topic: Array of functions (addressing multiple identical sensors) (Read 1 time) previous topic - next topic

ottehoman

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

Code: [Select]

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

Code: [Select]

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.

PaulS

Quote
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.

Tom Carpenter

#2
Jan 10, 2013, 12:17 pm Last Edit: Jan 10, 2013, 12:30 pm by Tom Carpenter Reason: 1
You could always try this:
Code: [Select]
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.
~Tom~

ottehoman

I used the library from Adafruit, on github, see this link https://github.com/adafruit/DHT-sensor-library.
It has a class DHT, not dht. The example there uses dht as the instance, not DHT:
Code: [Select]

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.
Code: [Select]

#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
};



PeterH

#4
Jan 10, 2013, 12:21 pm Last Edit: Jan 10, 2013, 12:26 pm by PeterH Reason: 1

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.

Code: [Select]

int pins[MaxSensors] = { 3, 4, 5, 6, 7 };
DHT *myDHT[MaxSensors];

Code: [Select]

for (int n=0; n<MaxSensors; n++)
{
   myDHT[n] = new DHT(pins[n], 11); // define a sensor at pin 'n' with sensor type DHT11
}

Code: [Select]

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:

Code: [Select]

DHT myDHT[MaxSensors] = {
    DHT(3, 11),
    DHT(4, 11),
    DHT(5, 11),
    DHT(6, 11),
    DHT(7, 11)
};
I only provide help via the forum - please do not contact me for private consultancy.

Tom Carpenter



I've come up with the following, which compiles. Not sure if this is the right way to go, or if it's garbage.
Code: [Select]


  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.
~Tom~

ottehoman

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:

Code: [Select]

#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");
 };
};

ottehoman

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:







maxDHTBytes
56066
66068
76070
86072
n6056+2*n


Thanks for all your help, people!

O.

wildbill

Quote
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.

PeterH

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.
I only provide help via the forum - please do not contact me for private consultancy.

Go Up