0

I'm working with an Arduino UNO in my first attempt to use the OO in Wiring. I just have a question which I need help with; I'm using a class for a DHT22 sensor from freetronics, found here http://www.freetronics.com/products/humidity-and-temperature-sensor-module. Say I have a class i made called Sensor, where DHT is the library for the freetronics sensor, and DHT dht(5, DHT22) Initialises the sensor on pin 5 (which could be any digital pin):

#include "arduino.h"
#include <DHT.h>

DHT dht(1, DHT22);

class Sensor{
  public:
    Sensor(int);

};

Sensor::Sensor(int pin){
  DHT dht(pin, DHT22);
}

This is the only way to initialise DHT that does not throw compile errors, even though it is re-initialising that variiable, but if I try to put DHT dht() as a private variable it doesn't work.

But my question is, if i create two of these Objects, will they be using the same object reference dht? Or is the initialising code for dht in the correct area to act only inside the instances' scope?

Please tell me if this is confusing, and I appreciate any help in advance, Thank You :)

afiles
  • 13
  • 3
  • `DHT dht()` is not declaring an instance variable. It is declaring a function. You need `DHT dht;`, without the parentheses. If its constructor requires parameters, then you should use the initialization syntax in the constructor: `Sensor (int pin) : dht(pin) { /* ... */ }` – The Paramagnetic Croissant Oct 28 '14 at 06:47
  • This was perfect, it worked. Thanks so much! Is that Initialisation syntax required or can I do it in the function ? Is there a difference. Either way thank you so much! – afiles Oct 28 '14 at 07:57
  • yes, it's required. No, you can't do it inside the function (why do you want another way when there's one that is perfectly fit for the purpose?), yes, there is a difference (assignment is not initialization, think references, `const`-qualified objects, ...). Please read an introductory C++ book. – The Paramagnetic Croissant Oct 28 '14 at 08:09

0 Answers0