Problem to read from internal EEprom

Hi all

I'm trying to build a calendar/timer to switch on and off lights via modbus.
I'm using ATmega328-PU with a 16x2 lcd , a DS1307 and serial connection for modbus ( i'm using it over RS232 for tests now ).
All work fine, modbus send and receive command and lcd display is working fine.
When i try to write timer data on EEprom it works fine, but when i try to retrieve it all the program stop to work, lcd dislay shows weird things, serial data are a mess.
If i try to load only 9 values from the EEprom it works, but if i try to load more values i have the problem:

// this portion of code mess up all things :
for ( int i=1; i<128; i++ )
{
buffer_EEprom[i]=EEPROM.read(i);
}

Large portion of code here :

// MODBUS CALENDAR WITH LCD SCREEN

// CPU : ATMEGA328-PU 
// pin map
// 0,1            : serial port for modbus link
// 2,3,4,5,7,8    : 16x2 lcd
// 13             : led
// A0,A1,A2,A3    : butttons with pull down resistor
// A4,A5(SDA/SCL) : DS1307 via i2c


// EEPROM
#include <EEPROM.h>
int buffer_EEprom[128];

// LCD
// FASTER LIBRARY :
// https://bitbucket.org/fmalpartida/new-liquidcrystal/wiki/Home
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 7, 5, 4, 3, 2);

// MODBUS
#include <ModbusMaster.h>
ModbusMaster node(1);

// DS1307
#include <Wire.h>
#define DS1307_ADDRESS 0x68

byte zero = 0x00; //workaround for issue #527
byte second ;
byte minute ;
byte hour ;
byte weekDay; 
byte monthDay;
byte month;
byte year;
char buffer_lcd[3]="";

//MENU

#define MENU_OROLOGIO 10
#define SPLASH 16

const char* riga_1[] = { 
"< IMPOSTAZIONI >",  // menu 0  "menu impostazioni"
"<   NORMALE    >",  // menu 1  "funzionamento normale"
"<SOLO ENTRATA  >",  // menu 2  "sola entrata"
"<SOLO USCITA   >",  // menu 3  "sola uscita"
"<SEMPRE CHIUSO >",  // menu 4  "sempre chiuso"
"<SEMPRE APERTO >",  // menu 5  "sempre aperto"
"<APERT.PARZIALE>",  // menu 6  "apertura parziale"
"CONNESSIONE     ",  // menu 7  "monitor"
"CONNESSIONE     ",  // menu 8  "stato cancello"
"<AP.PARZ. SET. >",  // menu 9  "set % apertura parziale
"<ORARIO>        ",  // meno 10 "orologio"
"<TIMER         >",  // meno 11 "TIMER"
"<TIMER ATTIVO ?>",  // MENU 12 //"<TIPO TIMER    >",  // MENU 
"<ORARIO INIZIO >",  // MENU 13 // "<ORARIO FINE   >",  // MENU 
"<INVIA COMANDO >",  // MENU 14
"<TIPO COMANDO  >",   // MENU 15
"                ",
"SERIALE CN3     "
}; 

const char* riga_2[] = {    
"[OK]            ", 
"IMPOSSIBILE     "}; 


int REFRESH=100 ;
int menu_visualizzato = SPLASH;
int setting_orario = 0;

// TIMER


byte timer_numero = 1;
byte timer_abilitato=EEPROM.read(2);
byte timer_comando =EEPROM.read(3);
byte timer_tipo_comando =EEPROM.read(4);
byte timer_giorno_inizio =EEPROM.read(5);
byte timer_ora_inizio = EEPROM.read(6);
byte timer_minuto_inizio = EEPROM.read(7);
byte timer_giorno_fine = EEPROM.read(8);
byte timer_ora_fine = EEPROM.read(9);
byte timer_minuto_fine = EEPROM.read(10);
byte timer_comando_inviato = 0 ;


// BUTTONS
int state_button_SX;            
int laststate_button_SX=LOW;   
int state_button_DX;            
int laststate_button_DX=LOW;   
int state_button_OK;            
int laststate_button_OK=LOW;   
int state_button_CANC;            
int laststate_button_CANC=LOW;   
int reading_button_SX ;
int reading_button_DX ;
int reading_button_OK ;
int reading_button_CANC ;
long lastDebounceTime_SX = 0;  
long lastDebounceTime_DX = 0;  
long lastDebounceTime_OK = 0;  
long lastDebounceTime_CANC = 0;  
long debounceDelay = 20;   


// VARIABILI VARIE
int timer_lcd=0;
int percentuale_apertura_parziale=50;
int sempre_aperto = 0;
byte low ;
byte high;

void setup() {
  int kk=0;
  
  low =  EEPROM.read(1002);
  high =  EEPROM.read(1001);
   
  pinMode(13, OUTPUT);
  
  // INPUTS
  pinMode(A0, INPUT);        
  pinMode(A1, INPUT);       
  pinMode(A2, INPUT);        
  pinMode(A3, INPUT);        
  
  // Instantiate the RTC
  Wire.begin();
  
  // 16X2 LCD 
  lcd.begin(16, 2);
  
  requestDateTime();
  weekDay=(byte)dow2(year, month,monthDay);
  
  // modbus
  node.begin(19200);   

// this portion of code mess up all things :
  
/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
for ( int i=1; i<128; i++ )
{
buffer_EEprom[i]=EEPROM.read(i);
}
/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!! */   
    
  
}

void loop() {
  
  static uint32_t i;
  uint8_t result;
  

  int reading_button_SX   = analogRead(A0);
  int reading_button_DX   = analogRead(A1);
  int reading_button_OK   = analogRead(A2);
  int reading_button_CANC = analogRead(A3);
   
  if ( reading_button_SX > 500 ) reading_button_SX=1000;
  else reading_button_SX=0;
   
  if ( reading_button_DX > 500 ) reading_button_DX=1000;
  else reading_button_DX=0;
   
  if ( reading_button_OK > 500 ) reading_button_OK=1000;
  else reading_button_OK=0;
  
  if ( reading_button_CANC > 500 ) reading_button_CANC=1000;
  else reading_button_CANC=0;
   
  weekDay=(byte)dow2(year, month,monthDay);
  
  
 
 
 switch(menu_visualizzato) 
 {
   
     case SPLASH:                          
      {  
        // read some registers        
        result = node.readHoldingRegisters(0x1020,16);
        // write some registers 
        node.setTransmitBuffer(0,213);
        result = node.writeMultipleRegisters(0xFFFC,1);        
        node.setTransmitBuffer(0,35161);
        result = node.writeMultipleRegisters(0xFFFD,1);
    
        
        lcd.setCursor(0,0);
        lcd.print(" HELLO ");
        
        lcd.setCursor(0,1);
        lcd.print(low);
        lcd.setCursor(8,1);
        lcd.print(high);
        
   
        if(timer_lcd==5) menu_visualizzato = MENU_OROLOGIO;
        break;
      } 
 
 }   
  
  laststate_button_SX = reading_button_SX;
  laststate_button_DX = reading_button_DX;
  laststate_button_OK = reading_button_OK;
  laststate_button_CANC = reading_button_CANC;  
   
}

I have not problem to load other data from EEProm like parameters low and high at address 1001 and 1002.
I'm going out of memory ?
Sram is full ?

int buffer_EEprom[128];

The EEPROM is bytes so there isn't much point reading into an int.

for ( int i=1; i<128; i++ )
{
buffer_EEprom[i]=EEPROM.read(i);
}

Also this is corrupting memory because arrays start at zero.

I have tried also ( int i=0;i<128;i++) , same problem. :frowning:

Yes, i have tried with bytes at first and then tried to modify it in int, same results.
In arduino IDE i selected Arduino UNO and i'm using a atmega328-pu, maybe it is not correct ?

If i try to load only 9 values from the EEprom it works, but if i try to load more values i have the problem:

What problem?

Strange characters on LCD screen and faulty modbus messages.
The same behaviour when you have a buffer overflow.
I remove the for cycle with eeprom.read and all works fine.
Maybe the SRAM is full ?
My program it's very simple but maybe the libraries i use eat a lot of SRAM.

I notice that you have quite a lot of data being stored in global variables. Have you checked that you're not running out of sRam? Which model arduino are you using?

KenF:
I notice that you have quite a lot of data being stored in global variables. Have you checked that you're not running out of sRam? Which model arduino are you using?

It' an atmega328pu with arduino UNO bootloader.
Yes, i fear i have used too many sram.
Do you know how i can check it ?

MaxDamage75:
It' an atmega328pu with arduino UNO bootloader.
Yes, i fear i have used too many sram.
Do you know how i can check it ?

Add this function to your sketch

int get_free_memory()
{
 extern int __heap_start, *__brkval;
 int v;
 return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}

Then perhaps at the end of your setup function add

Serial.print(F("Free memory "));
Serial.println(get_free_memory(),DEC);

Then perhaps add that same two lines at other strategic points in your sketch (maybe just before the eeprom read)

KenF:
Add this function to your sketch

int get_free_memory()

{
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}




Then perhaps at the end of your setup function add


Serial.print(F("Free memory "));
Serial.println(get_free_memory(),DEC);




Then perhaps add that same two lines at other strategic points in your sketch (maybe just before the eeprom read)

Thank you !
I was using too much memory, i have declared all strings with F() function and now it works.