Constructor for AT24CX

Hello, I have a strange problem.
In this…

#include <AT24CX.h>	//Library for using the EEPROM AT24C32/64 (c) 2014 Christian Paul

AT24CX mem;		

void setup ()
{ 
   Serial.println("I am in void setup");
   mem.read(Addr_Weather_history, (byte*)Weather_history, sizeof(Weather_history));
}

It works if I compile it for Arduino UNO or MEGA.
If I compile for Arduino DUE, there is no syntactic errors but program do NOT anything…

It hangs somewhere in constructor of AT24CX.
It does NOT write anything to serial port.

As soon as I place constructor directly to void setup, like that…

void setup ()
{ 
   AT24CX mem;		
   Serial.println("I am in void setup");
   mem.read(Addr_Weather_history, (byte*)Weather_history, sizeof(Weather_history));
}

it works perfectly with DUE too. But of course… “mem” is private for setup() only.

Do you have any idea about reason? I need “mem” like global object.

Thank you. Roman

I need "mem" like global object.

// Global
AT24CX *pMem = NULL;

void setup()
{
   pMem = new AT24CX();
   Serial.println("I am in void setup");
   pMem->read(Addr_Weather_history,
                       (byte*)Weather_history,
                       sizeof(Weather_history));

References in other places to mem (mem.xxx) become references to pMem (pMem->xxx).

Great, thank you.

In this time I solved it by re-declaration object "mem" in void loop(), for example, again. It works too.

void setup ()
{ 
   AT24CX mem;     
   Serial.println("I am in void setup");
   mem.read(Addr_Weather_history, (byte*)Weather_history, sizeof(Weather_history));
}
void loop()
{
   AT24CX mem;     
   mem.write(.....);
}

Is it bad too much or it could be correct solution?

Anyway... has anybody idea of reason for that? Why is there this big and unpleasant difference between Arduino Mega and Arduino DUE?

Thx, Roman

Is it bad too much or it could be correct solution?

I don't know what a AT24CX is, so I can't comment.

Why is there this big and unpleasant difference between Arduino Mega and Arduino DUE?

We have only your word that there is a difference. You have not posted working code or output that illustrates that there IS a difference.

PaulS: I don't know what a AT24CX is, so I can't comment.

We have only your word that there is a difference. You have not posted working code or output that illustrates that there IS a difference.

I have described what is AT24CX. Just look at first post.

I did. I posted the main part of two versions of code which perfectly illustrates what is the point. Or is it necessary to post complet example with libraries? Do you have Arduino Mega and Arduino DUE to try to?

Link to the library you're using. I suspect the answer is in the library source code - either something is different between the architectures, or there's an additional initialization step only needed on the SAM board to initialize I2C.

ok, here is a link... https://github.com/cyberp/AT24Cx

I think there is (at the moment of declaration object AT24CX) only one important command... wire.begin();

It starts I2C and it matters where I place it... in front of void setup() or in it.

Maybe bug of Arduino IDE...

P.S. I have tried another library for flash EEPROM AT24C... it has exactly same problem on Arduino DUE.