2017-11-24 6 views
0

私のDHTセンサーのライブラリを作成しています。 I AM_2301.cppAM_2301.hの名前を持つ2つのファイルがあります:私がしたいクラス内でグローバルな別のコンストラクタを呼び出す

#include "Arduino.h" 
#include "AM_2301.h" 

int pin =3 ; 

AM_2301 AM(pin); 

void setup() { 
    // put your setup code here, to run once: 
    Serial.begin(115200); 
    AM.begin(); 
} 

void loop() { 

    struct Two_float val; 
    val = AM.read(); 
    Serial.print("Temp: "); 
    Serial.print(val.temp); 
    Serial.print(" , "); 
    Serial.print("Humidity: "); 
    Serial.println(val.humidity); 
    delay(2000); 
    // put your main code here, to run repeatedly: 
} 

しかし、問題がある:

AM_2301.h

#ifndef AM2301_h 
#define AM2301_h 

#include "Arduino.h" 

struct Two_float { 
    float temp; 
    float humidity; 
}; 

extern int pinn; 

class AM_2301 
{ 
    public: 
    AM_2301(int pin); 
    void begin(); 
    struct Two_float read(); 
    private: 
    int _pin; 
}; 

#endif 

AM_2301.cpp

#include "Arduino.h" 
#include "AM_2301.h" 
#include "DHT.h" 

//#define pinn 3 
int pinn; 
DHT dht(pinn, DHT21); 

AM_2301::AM_2301(int pin) 
{ 
    pinMode(pin, OUTPUT); 
    Serial.print("pinn: "); 
    Serial.println(pinn); 
    _pin = pin; 
    pinn = pin; 
    //DHT dht(pinn, DHT21); 
} 


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

struct Two_float AM_2301::read() 
{ 
    struct Two_float output; 
    float h = dht.readHumidity(); 
    float t = dht.readTemperature(); 
    // check if returns are valid, if they are NaN (not a number) then something went wrong! 
    if (isnan(t) || isnan(h)) 
    { 
     Serial.println("Failed to read from DHT"); 
     t = 0; 
     h = 0; 
    } 
    output = {t, h}; 
    return output; 
} 

main.cppをのピン番号を宣言するコンストラクタ、そのピンはAM_2301.cppの別のコンストラクタに送られますが、実装する方法はわかりません。 dhtオブジェクトを自分のクラス内の他のすべての関数にグローバルにしたいと思っています。

答えて

2

あなたはAM_2301クラスのDHT対象部分を作り、その後、member initializer listを使用して、それを初期化する必要があります。

AM_2301.h

#ifndef AM2301_h 
#define AM2301_h 

#include "Arduino.h" 

struct Two_float { 
    float temp; 
    float humidity; 
}; 

class AM_2301 
{ 
    public: 
    AM_2301(int pin); 
    void begin(); 
    struct Two_float read(); 
    private: 
    DHT dht; // add a DHT object 
}; 

#endif 

AM_2301.cpp

#include "Arduino.h" 
#include "AM_2301.h" 
#include "DHT.h" 

AM_2301::AM_2301(int pin) : dht(pin, DHT21) {} // initialize the DHT object 

// other code stays the same 
+0

ありがとうございますが、 'AM_2301 :: read()'のように 'AM_2301.cpp'の他の関数でdhtオブジェクトにアクセスするにはどうしたらいいですか? –

+0

あなたと同じ方法です。 –

+0

あなたは正しく私はそれをテストし、答えをここに投稿します。 –

1

最後に、私は静的インスタンスからのポインタを使用しなければならなかった水田の答えを見つけました。answerここでは修正AM_2301.cpp次のとおりです。

#include "Arduino.h" 
#include "AM_2301.h" 
#include "DHT.h" 

static DHT *dht = NULL; //define the pointer 


AM_2301::AM_2301(int pin) 
{ 
    pinMode(pin, OUTPUT); 
    Serial.print("_pin: "); 
    Serial.println(_pin); 
    _pin = pin; 
    dht = new DHT(_pin, DHT21); //pass the initialization 
} 


void AM_2301::begin(){ 
    dht->begin(); 
} 

struct Two_float AM_2301::read() 
{ 
    struct Two_float output; 
    float h = dht->readHumidity(); 
    float t = dht->readTemperature(); 
    // check if returns are valid, if they are NaN (not a number) then something went wrong! 
    if (isnan(t) || isnan(h)) 
    { 
     Serial.println("Failed to read from DHT"); 
     t = 0; 
     h = 0; 
    } 
    output = {t, h}; 
    return output; 
} 
+1

あなたが複数のセンサを使用することを決定した場合、これは動作しません。 –

+0

@gre_gorなぜですか?理由を投稿できますか? –

+0

AM_2301オブジェクトごとに個別のDHTオブジェクトを持つことはできません。 –

関連する問題