Pages: [1]   Go Down
Author Topic: Array of functions (addressing multiple identical sensors)  (Read 947 times)
0 Members and 1 Guest are viewing this topic.
Sydney, AUS
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Leeds, UK
Offline Offline
Edison Member
*
Karma: 78
Posts: 1719
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You could always try this:
Code:
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.
« Last Edit: January 10, 2013, 06:30:29 am by Tom Carpenter » Logged

~Tom~

Sydney, AUS
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12568
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
int pins[MaxSensors] = { 3, 4, 5, 6, 7 };
DHT *myDHT[MaxSensors];
Code:
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:
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:
DHT myDHT[MaxSensors] = {
    DHT(3, 11),
    DHT(4, 11),
    DHT(5, 11),
    DHT(6, 11),
    DHT(7, 11)
};
« Last Edit: January 10, 2013, 06:26:59 am by PeterH » Logged

I only provide help via the forum - please do not contact me for private consultancy.

Leeds, UK
Offline Offline
Edison Member
*
Karma: 78
Posts: 1719
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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:

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

~Tom~

Sydney, AUS
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Sydney, AUS
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

New Jersey
Offline Offline
Faraday Member
**
Karma: 65
Posts: 3638
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12568
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

I only provide help via the forum - please do not contact me for private consultancy.

Pages: [1]   Go Up
Jump to: