Using other library inside mine

I am trying to create a library which uses other libraries inside. For example, DallasTemperature lib.

the code for initialize ds18b20 sensor to measure temperature is something like:

#include <OneWire.h> //Se importan las librerías
#inclu#include <OneWire.h> //Se importan las librerías
#include <DallasTemperature.h>

#define Pin 3 //Se declara el pin donde se conectará la DATA

OneWire ourWire(Pin); //Se establece el pin declarado como bus para la comunicación OneWire

DallasTemperature sensors(&ourWire); //Se instancia la librería DallasTemperature

void setup() {
delay(1000);
Serial.begin(9600);
sensors.begin(); //Se inician los sensores
}

void loop() {
sensors.requestTemperatures(); //Prepara el sensor para la lectura
Serial.print(sensors.getTempCByIndex(0)); //Se lee e imprime la temperatura en grados Celsius
Serial.println(" grados Centigrados");
delay(1000); //Se provoca un lapso de 1 segundo antes de la próxima lectura
}

And after that, justcall to functions for reading values.

I wanted to include #include statements in my cpp file and “OneWire ourwire…”, “DallasTemperatue sensor…” and “sensor.begin…” in my init function. It works fine until I call sensor.requestTemperature() and sensor.getTempCByIndex(0) in other function of my library.

I get an error:

error: ‘sensors’ was not declared in this scope

I guess it is because I am declaring sensor variable inside init function. But I try to make it public in my .h and I get the same error.

How should I declare and use a library inside other?

How should I declare and use a library inside other?

Step 1: Post the code you have now. Step 2: Post the EXACT error messages.

In general, some libraries make it easy to be used in other libraries, by providing no argument constructors, and expecting all information to be passed to the instance in a begin() argument list, like HardwareSerial does it. Other libraries, like LiquidCrystal, expect a boatload of arguments to the constructor, so they are more difficult to use in your library. Not impossible, just not as easy.

I have nothing.

I just create an empty library and try to include that sensor in an initFunction(). but I just added that, nothing else.

MyLib.h
#ifndef MyLib_h
#define MyLib_h

#include "Arduino.h"
class MyLib
{
  public:
      void initFunction();
      void readTemp();
  private:
};
#endif

MyLib.cpp

include "Arduino.h"
#include <EEPROM.h>
#include <OneWire.h> 
#include <DallasTemperature.h>//Temperatue DS18B20
#include "MyLib.h"

void MyLib::initFunction(){
  OneWire ourWire(3);
  DallasTemperature sensors(&ourWire);
  sensors.begin();
}

void MyLib::readTemp(){
  Serial.println("READ TEMP:");
  sensors.requestTemperatures(); //Prepara el sensor para la lectura
  Serial.print(sensors.getTempCByIndex(0));
}

Error:

 error: 'sensors' was not declared in this scope

Well, it's fairly obvious that sensors goes out of scope when the initFunction() ends, so trying to use it in readTemp() doesn't make sense.

Your class needs a constructor. The constructor does some stuff. The initFunction() might be needed, or it might not. In any case, it is typical, in the Arduino world, to call such a method begin().

The constructor needs to create the oneWire and sensors instances, which have to be declared as private members of the class.

Your class header file would have:

private:
   OneWire oneWire;
   DallasTemperature sensors;

public:
   MyLib();

added.

Your source file would have:

MyLib::MyLib(): oneWire(3), sensors(&oneWire)
{
   // Anything else...
}

added.

Thanks PaulS. Now it doesn't crash while compiling but temperature is wrong. always gives -127. With example code it works so I have to check where the code goes wrong.

I added "sensors.begin();" in my constructor, to activate sensor so that is not the problem I guess.

I have never seen that way to pass arguments to the constructor. I will check about it.

I added "sensors.begin();" in my constructor, to activate sensor so that is not the problem I guess.

You should, as I mentioned earlier, have a begin() method where you call sensors.begin().