Go Down

Topic: Uno keeps restarting during setup() - is it memory issue? (Read 370 times) previous topic - next topic

Czachor

Hello Guys,

I have a strange issue after last code was uploaded. My Arduino Uno V3 keeps restarting in the loop when is trying execute code inside setup() block. I would like to know the reason of this issue. Code is very short, so I believe most of you will be able to find the answer very quickly. A few technical points:

  • Arduino Uno R3
  • Arduino IDE 1.8.8
  • Windows 10
  • Libraries:
    LiquidCrystal_I2C
    DS1302 RTC
    DHT11
    4 Buttons

This code works fine:

Controller.ino:
Code: [Select]
#include <Menu.h>
#include "DHT.h"

#define DHTPIN 9

DHT dht(DHTPIN, DHT11);
Menu *menu;

void setup() {
 
  dht.begin();
  menu = new Menu;

}

void loop()
{
  short int humidity = dht.readHumidity();
  short int temperature = dht.readTemperature() * 10;

  menu->update(temperature, humidity);
}



Menu.h - Header file of my Menu library:
Code: [Select]
#ifndef MENU_H
#define MENU_H

#include "Arduino.h"
#include "Button.h"
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>
#include  <virtuabotixRTC.h>

#define EEPROM_ADDRESS_TEMPERATURE_SETPOINTS 0
#define EEPROM_ADDRESS_TEMPERATURE_HYSTERESIS 576
#define EEPROM_ADDRESS_TEMPERATURE_OFFSET 578
#define EEPROM_ADDRESS_TEMPERATURE_ALARM 580
#define EEPROM_ADDRESS_HUMIDITY_SETPOINTS 584
#define EEPROM_ADDRESS_HUMIDITY_HYSTERESIS 872
#define EEPROM_ADDRESS_HUMIDITY_OFFSET 874
#define EEPROM_ADDRESS_HUMIDITY_ALARM 876

class Menu {

public:
Menu();
void update(short int temperature, short int humidity);
void show(String text);
String displayEmptyRow();

short dateTime[6];

short temperatureHysteresis;
short temperatureOffset;
short temperatureAlarm[2];
short temperatureSetpoint;
short humidityHysteresis;
short humidityOffset;
short humidityAlarm[2];
short humiditySetpoint;
private:
    LiquidCrystal_I2C* lcd;
virtuabotixRTC* RTC;

void setMoveDirection();
void moveLeft();
void moveRight();
void moveUp();
void moveDown();
void move();
void moveTo(String position);
void dispay();
String fillTo16Char( String text, char align = '0');
void setInitValues();
String leadZeros(short number, short digits = 2);
String leadZeros(String number, short digits = 2);

short _temperatureSetpoints[12][24];
short _menuPosition[4];
short _currentLevel;
short _displayPosition;
char _direction;
int _c;
Button _buttonMenu;
Button _buttonUp;
Button _buttonDown;
Button _buttonSet;
String _datePart[5];
String _months[12];
String _alarmHeader[2];
uint8_t _arrow_up[8];
uint8_t _arrow_down[8];
uint8_t _hour_minutes[8];
uint8_t _plusMinus[8];
int _temperature;
int _humidity;
};

#endif //MENU_H


Menu.cpp
Code: [Select]
#include "Menu.h"

Menu::Menu() :
_buttonMenu(2),
_buttonUp(3),
_buttonDown(4),
_buttonSet(5),
_menuPosition { 0, 0, 0, 0 },
_datePart { "YEAR", "MONTH", "DAY", "HOUR", "MINUTE" },
_months { "Jan.", "Feb.", "Mar.", "Apr.", "May", "June", "July", "Aug.", "Sept.", "Oct.", "Nov.", "Dec." },
_alarmHeader {"Up limit", "Down limit"},
_arrow_up {0x04, 0x0a, 0x15, 0x04, 0x04, 0x04, 0x04},
_arrow_down {0x04, 0x04, 0x04, 0x04, 0x15, 0x0a, 0x04},
_hour_minutes {0x1f, 0x15, 0x1f, 0x00, 0x00, 0x00, 0x00},
_plusMinus {0x04, 0x04, 0x1f, 0x04, 0x04, 0x00, 0x1f}
{
lcd = new LiquidCrystal_I2C(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
lcd->begin(16, 2);  // Inicjalizacja LCD 2x16
lcd->createChar(1, _arrow_up);
lcd->createChar(2, _arrow_down);
lcd->createChar(3, _hour_minutes);
lcd->createChar(4, _plusMinus);

RTC = new virtuabotixRTC(6, 7, 8);
}


As I said, the code above works fine, but if I put just one line - array declaration inside Menu.h:

Quote
short _humiditySetpoints[12][2];
Then Uno is restarting again and again on the below step inside the setup() block:

Quote
menu = new Menu;
I assume that this is due to reaching the memory limit, but when I uploading the code, compiler says:

Quote
Sketch uses 7524 bytes (23%) of program storage space. Maximum is 32256 bytes.
Global variables use 565 bytes (27%) of dynamic memory, leaving 1483 bytes for local variables. Maximum is 2048 bytes.
So it looks like there is a lot of free memory to use. Do you have any ideas what is the real reason of this issue?

Thanks for your support in advance.

westfw

Quote
Code: [Select]
void setup() {
  dht.begin();
  menu = new Menu;
}


Try making "menu" a global variable instead of dynamically allocated, and see what the build says about memory consumption.

Czachor

Thank you for your response. I did this test on the last working version of code.

Dynamically allocated:
Quote
Sketch uses 22092 bytes (68%) of program storage space. Maximum is 32256 bytes.
Global variables use 616 bytes (30%) of dynamic memory, leaving 1432 bytes for local variables. Maximum is 2048 bytes.
Global variable:
Quote
Sketch uses 21626 bytes (67%) of program storage space. Maximum is 32256 bytes.
Global variables use 1393 bytes (68%) of dynamic memory, leaving 655 bytes for local variables. Maximum is 2048 bytes.
I changed dynamic to global for LCD as well as DTH, but not much has changed:
Quote
Sketch uses 20830 bytes (64%) of program storage space. Maximum is 32256 bytes.
Global variables use 1425 bytes (69%) of dynamic memory, leaving 623 bytes for local variables. Maximum is 2048 bytes.
There is not even warning that memory allocation is close to maximum.

Go Up