Strange behavior of pinMode() OUTPUT inside relay class object

Hi, I working on my thesis project and I have found some wired behavior of pinMode OUTPUT. Project is based on 328P + 2Ch relay with inverse logic (5v, gnd, ch1, ch2). Pins 5,6 are used for relay.

When I place definition in Class constructor, or just in setup() sequence like this relay does not work:

pinMode(_pin1, OUTPUT);
digitalWrite(_pin1, HIGH);

When i have removed pinMode(pin1, OUTPUT) from the code relay suddenly switch ON and start working properly. Also it's working on pinMode(pin1, INPUT).

According to the registrer read from bitRead(PORTD,_pin1) ) pin is in right state (0 for HIGH, 1 for LOW) but relay does not switch. Method pinMode isn't placed enywhere else in the code so I don't get it.

class relay constructor

Relay::Relay(int pin1, int memoryAdress, Consumption* consumption):

 	digitalWrite(pin1, HIGH); // because inverse logic. problem is same when this is gone
 	pinMode(pin1, OUTPUT); // working withou this line or with INPUT..

I have tried this code on several boards to avoid some mcu demage or bad connection but everything was still the same. Note that my project uses 23,960 bytes (77%) of program storage space. Global variables use 1,155 bytes (56%) of dynamic memory, leaving 893 bytes for local variables. Free SRAM left 189 bytes when all instances have been made. Project was written in OOP and it has around 30 classs.

Thanks for any reply guys. Michal

The problem is that libraries like Wire, Serial, and the timers may not have been initialized at this time. In other words, your constructor may have been called before their constructor.

You are best off doing minimal things in the class constructor, and making a "begin" function (like Serial.begin, Wire.begin, etc.) and calling that in "setup". Acceptable things to do in a constructor are to store simple types (eg. integers like pin numbers).
Credits goes to Nick Gammon

HugoPT, thanks for your reply.

You were right! This problem was caused by TimerOne object which call interrupt function on unicialised object. This btw caused voltage drop on pin to 2.5V when it should be 5V..

Now is fixed, for everyone else:

void timerIsr() {

void setup() {
  encoder = new ClickEncoder(A1, A0, A2); // working when here
  encoder = new ClickEncoder(A1, A0, A2); // not working when here