Object from library is only working when defined in the same function? (solved)

I’m currently upgrading my library to place text at corners of a 1602 lcd, to work with both ‘default wiring’ as ‘I2C wiring’.
Everything is working fine with the ‘default wiring’, just connecting D0-D4.
When I’m using I2C, i bump into the following problem:

When defining my object outside a function, so the ‘normal’ place to define public objects, my code stops working.
When it’s defined inside a function (setup or loop for instance) it’s working fine.
I’ve added the comments in the code below where the code stops working, and where it’s working fine.

#define I2C

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <RTClib.h>
#include <Menu\Menu.h>
#include <Encoder.h>

#define pinLED 13

RTC_DS1307 rtc;
Encoder enc(2, 3, 4);

LiquidCrystal_I2C lcd(0x27, 16, 2);  // Set the LCD I2C address
//when this is added, nothing will work...
Menu* lcdMenu2 = new Menu(&lcd, &rtc, 16, 2);

#pragma region pinverbindingen
#pragma endregion

void setup()

	pinMode(pinLED, OUTPUT);

	//this block works perfectly fine
	Menu* lcdMenu1 = new Menu(&lcd, &rtc, 16, 2);
	lcdMenu1->rechtsBoven("first test");
	//end of first test block

void loop()

//	lcdMenu2->rechtsBoven("hallo"); //this won't work

I’ve uploaded my library to github: (forgive me if it’s not 100% correct, first time on github :wink: ) https://github.com/ArduinoLcdMenu/Menu
Also the cpp & header are included in the attachments.

using only object lcdMenu1:

after using object lcdMenu2: (the part where it stops working)

Menu.cpp (6.14 KB)

Menu.h (5.77 KB)

    String _lO; //      'right under'
    String _lB; //      'right above'
    String _rO; //      'right under'
    String _rB;//       'right above'
    String _cB;//       center..
    String _cO;//

I got that far in the first file, and quit reading. Do NOT use the String class on a low-memory Arduino.

Possibly: http://www.gammon.com.au/forum/?id=12153#trap20

What's wrong with doing new in setup?


Menu* lcdMenu2;

void setup()
    lcdMenu2 = new Menu(&lcd, &rtc, 16, 2);

Or, this might work:

Menu lcdMenu2 (&lcd, &rtc, 16, 2);

void setup()
  // whatever

The problem was solved after splitting the constructor. after constructing the object, I call a initiation method to do the commands that were previously on the constructor. That’s solving the problem. (I like your website btw :slight_smile: )

I will try to convert the code using strings to char arrays too :slight_smile: