Using a class within a class / OneWire

I've created my own library/class called "DS18B20" where the constructor takes a PIN_NUMBER parameter. This is used to instantiate a One Wire class using an initiator list in the .cpp library file in the form DS18B20::DS18B20(uint8_t OneWirePin) : OneWireLAN(OneWirePin) { ... } where the OneWireLAN is defined in the private class section of the header file as "OneWire OneWireLAN;".
This works perfectly well, but only when I define this class inside the main loop of the Arduino project.

void loop(){
   static DS18B20 thermometers(ONE_WIRE_PIN);

As soon as I declare my class as a global one outside of the main or setup functions the sketch compiles and uploads but hangs at runtime and the serial output isn't initialized (using an Arduino Micro).
I can't figure out what the problem might be. When I make an empty constructor it works, but as soon as I execute any command with the OneWireLAN class it stops working.

Am I missing some intrinsic limitation to declaring a class within a class or perhaps with the OneWire library?


An object is an instantiation of a class. ... private members of a class are accessible only from within other members of the same class (or from their "friends"). protected members are accessible from other members of ...

mrBurnette - while it was an interesting link, it isn't relevant to the problem at hand.

I've been playing around a bit and discovered that if I comment out my "delay()" calls, or replace them with "delayMicroseconds()", then I can indeed place my instantiation in the global space. I can't quite figure out why this is happening, but at least I can continue development with the library.

I can't quite figure out why this is happening, but at least I can continue development with the library.

You probably should NOT continue with development until you DO understand why this is happening.

If you need help with that, post ALL of your code.

Are you doing any h/w operations in the constructor?
Typically you don't want to do much in a constructor other than parameter saving since there can be sequencing and resource issues.

One difference between global and local objects is when the constructor will be called.
If global it will be called before main() which is before some of the Arduino initialization takes place which is done before setup() and loop() are called.
Things like interrupts are not enabled yet.

When the object is declared inside loop() the object constructor is guaranteed to be called after all the other Arduino initialization has completed.

This is why you so so many classes with a begin() or init() function. The constructor is not able to do the h/w initialization.

--- bill

(Sorry for not replying to the thread sooner)
I was indeed doing hardware operations during the class instantiation which was causing it to not return. I've now completed moving all the functionality which was formerly in program code into a class and have tested and run it with 30 sensors. This is part of larger project that aims to optimize energy consumption on mobile refrigeration units such as those found in boats and mobile homes by combining accurate measurement and speed control of the compressor with PID technology.

I do have a library that supports the Maxim DS1822, DS1825, DS18B20, DS1820, DS18S20 and DS28EA00 1-Wire thermometers and minimizes the RAM use by utilizing EEPROM to store address information.

I've also added a calibration system that I've used in the past on the 1-Wire thermometers which brings the measurement standard deviation between many thermometers down to a very low level.

The library Wiki pages can be found here