OOPの観点からC++でArduino用のプログラムを作成していて、問題が発生しました。クラスのメソッドは、そのクラスのコンストラクタで定義されたオブジェクトを見ることができません。私が達成しようとしたのは、DHT11センサからのデータを計算して出力するためのさまざまなメソッドを格納するオブジェクト(クラス)を作成することです。 全コード:オブジェクト内のC++オブジェクト
* DhtSensor.h
*
* Created on: 2017-04-18
* Author: Secret
*/
#ifndef DhtSensor_h
#define DhtSensor_h
class DhtSensor {
public:
DhtSensor(int dhtPin); //constructor
void read();
void printToScreen(); //a method for printing to LCD
private:
unsigned long previousMillis; //Millis() of last reading
const long readInterval = 3000; //How often we take readings
float readingData[2][30]; //store current ant last values of temperature [0] and humidity [1] readings
int readingIndex;
bool initDone; //Bool value to check if initialization has been complete (Array full)
float totalTemp;
float totalHumidity;
float avgTemp;
float avgHumidity;
float hic; //Heat Index
};
#endif
/*
* DhtSensor.cpp
*
* Created on: 2017-04-18
* Author: Secret
*/
#include "DhtSensor.h"
#include "DHT.h"
#include "Arduino.h"
DhtSensor::DhtSensor(int dhtPin){
DHT dht(dhtPin,DHT11);
dht.begin();
previousMillis = 0;
totalTemp = avgTemp = 0;
totalHumidity = avgHumidity = 0;
hic = 0;
readingIndex = 0;
initDone = false;
for(int i = 0; i<2; i++){ //matrix init
for(int j=0; j<30; j++){
readingData[i][j]=0;
}
}
}
void DhtSensor::read(){
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= readInterval){
readingData[0][readingIndex] = dht.readTemperature();
}
}
問題がた.cppファイルにread()
メソッド内で起こります。コンストラクタにオブジェクトdht
が作成されていません。 私はここで何が欠けていますか?これはオブジェクト内にオブジェクトを持つことも良い方法ですか?おそらく、私はDhtSensor
クラスからクラスにDHT
ライブラリを除外し、DhtSensor
にデータを送信するライブラリのメソッドを使用するメインクラスにDHT
オブジェクトを作成する必要がありますか?
インスタンスは、関数の一部ではなくクラスのメンバーにすることができます。 – JVApen
言語を使い始める前にC++の本を読む必要があります。 C++の試行錯誤は非常に苦痛で、通常は良い結果につながりません。 – SergeyA