Arduino c++ classes, How to make instance variables of another class/library

一曲冷凌霜 提交于 2020-01-06 22:00:46

问题


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

来源:https://stackoverflow.com/questions/26602268/arduino-c-classes-how-to-make-instance-variables-of-another-class-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!