0

Is there a way to define class variables in the constructor or in a method rather than at the top of the document?

I have the following code in my custom library:

#include "DHT.h"

// DHT settings.
#define DHTPIN 7 // Digital pin connected to the DHT sensor.
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor.

void DhtLib::doSomeStuff() {
  dht.begin();
  float temp = dht.readTemperature();
  ...
}

I would like to be able to set the pins with a function. But for this to work I have to find a way to define the dht variable in a different place than at the top of the document within the // DHT settings.. Best would be in a method such as:

void DhtLib::begin(int dhtPin) {
  DHT dht(dhtPin, DHTTYPE);
}

This works, but only within this function. I need this to be accessible in the entire document, not just a single function.

How can I achieve that?

EDIT 1:

Entire code of the custom DhtLibrary.h file:

#ifndef DhtLibrary_h
#define DhtLibrary_h

#if (ARDUINO >= 100)
  #include "Arduino.h"
#else
  #include "WProgram.h"
#endif

class DhtLib {
  private:
    DHT dht;
    float readTemperatureFromDHT();
    float readHumidityFromDHT();
    float calculateHeatIndex(float temperature, float humidity);
    
  public:
    // Constructor.
    DhtLib(int dhtPin) : dht(dhtPin, DHT22) {}

    // Methods.
    void begin();
    String getHumiTempJson();
};

#endif

The error with this above code is:

In file included from /home/myuser/Documents/Codes/temperature-control/slave2-arduino/slave2-arduino.ino:2:0:
DhtLibrary.h:12:5: error: 'DHT' does not name a type
     DHT dht;
1

There's numerous ways of doing this. The best is probably to have a DHT object as a class property and set it up with an initializer list:

#include "DHT.h"

class DhtLib {
    private:
        DHT dht;

    public:
        DhtLib(int dhtPin) : dht(dhtPin, DHT22) {}
        void begin();
        void doSomeStuff();
};

void DhtLib::begin() {
    dht.begin();
}

void DhtLib::doSomeStuff() {
    float temp = dht.readTemperature();
}

DhtLib thing(7);

void setup() {
    thing.begin();
}

void loop() {
    thing.doSomeStuff();
}
| improve this answer | |
  • Thanks for the quick answer! Can you please elaborate the part DhtLib(int dhtPin) : dht(dhtPin, DHT22) {}? I don't quite understand the parts of what it does exactly. Is that still a constructor definition? And don't I have to also declare the DHT dht; from the DhtLibrary.h in the DhtLibrary.cpp file? – Socrates Jul 19 at 15:46
  • That is the "initializer list". Yes it's the constructor. When you construct it passes the variables up to the DHT object you set up in the private section. The DHT dht is declared inside the DhtLib class. – Majenko Jul 19 at 15:47
  • Ok, got it. Well, if I place the DHT dht; in the private: section the compiler is unhappy, giving the error DhtLibrary.h:20:5: error: 'DHT' does not name a type. It expects DHT dht(DHTPIN, DHTTYPE);. – Socrates Jul 19 at 15:49
  • 1
    You have to include the DHT.h library in your header so it knows what a DHT is. – Majenko Jul 19 at 16:06
  • 1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.