I have a programming question that I can't seem to figure out; I'm using the dht.h library for temperature/humidity sensors, and I want to wrap a SHT22 within a class to make it accessible in a common way and similar to other sensors in the system (they all have a method to read the value, format it for the serial port and format it for the LCD).
To do so I include the DHT.h and this defines a class call "dht"
To wrap this inside my class I create it as a private instance buy defining "dht SensorDHT22;" but I always get an error "'dht' does not name a type". If I do the same definition in my main loop it is happy with that definition - so i've done something wrong in my class definition... Can anybody tell me what?
MB
/*
DHT22TempHumidity - basic template for LMS 10000 sensor
Copyright (c) 2012 Mark Batchelour. All right reserved.
LMS 10000 reCreation project. www.LMS10000.org
*/
// ensure this library description is only included once
#ifndef DHT22TempHumidity_h
#define DHT22TempHumidity_h
// include types & constants of Wiring core API
#include "Arduino.h"
#include <dht.h>
// library interface description
class DHT22TempHumidity
{
// user-accessible "public" interface
public:
DHT22TempHumidity(int);
float GetValue(void);
float ReadSensor(void);
void DisplaySerial(void);
void DisplayLcd(void);
// library-accessible "private" interface
private:
float value;
dht SensorDHT22; // <----- Here I'm trying to crate an instance of a dht from dht.h but I get an error "'dht' does not name a type"
};
#endif
Thanks for your comment; The sketch doesn't do anything with this class yet as it fails to compile so far. The intention is to instantiate an instance of DHT22TempHumidity which contains all the access to the dht class. I'm not really worried about hiding it, so if the dht was public that would be fine, but it doesn't seem to work like that either.
in the dht example program the dht class is instantiated by;
Sorry, cut and paste error; Here is the body of the DHT22TempHumidity class;
/*
DHT22TempHumidity.cpp - basic template for LMS 10000 sensor
Copyright (c) 2012 Mark Batchelour. All right reserved.
www.LMS10000.org
*/
// include core Wiring API
#include "arduino.h"
// include this library's description file
#include "DHT22TempHumidity.h"
#include <dht.h>
// Constructor /////////////////////////////////////////////////////////////////
// Function that handles the creation and setup of instances
DHT22TempHumidity:: DHT22TempHumidity(int sensorPin)
{
// initialize this instance's variables
value = sensorPin;
// init
}
// Public Methods //////////////////////////////////////////////////////////////
// Functions available in Wiring sketches, this library, and other libraries
float DHT22TempHumidity::GetValue(void)
{
}
float DHT22TempHumidity::ReadSensor(void)
{
}
void DHT22TempHumidity::DisplaySerial(void)
{
}
void DHT22TempHumidity::DisplayLcd(void)
{
}
// Private Methods /////////////////////////////////////////////////////////////
// Functions only available to other functions in this library
As I say the main sketch does nothing with my class yet as it doesn't compile, but here it is;
So, what I said in reply #1 is true. You are trying to hide the fact that dht.h is required from the sketch.
As I said, you can't do that.
Why, you might ask. Well, that's relatively simple. You don't own, maintain, or distribute the dht library. You just want to use it. But, suppose other people want to use your library. It won't compile or link for anyone that doesn't have the dht library installed. And, from looking at the sketch that uses (or will) your library, it isn't obvious that the dht library is needed.
One would actually have to look at the source for your library to see what other library or libraries are needed.
And that is the thing that the Arduino team does not want users to have to do.