Pages: [1]   Go Down
Author Topic: How to manage Arduino memory?  (Read 5090 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 13
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I had created array of structures. It contains sunrise, sunshine and noon times for each day of the year-  has  predefined data.
Looks like array is too long, as, if it contains more than 212 elements, code  stops working (i need about 368 elements) Could you point me to more advanced programming guidelines about Arduous memory types, layout,memory management, if such exists..
Existing reference material is very basic, for example typedef is not even mentioned , however is widely used by Arduino software.

see test code (syntax is right)
To make it more compact, I had deleted most of array elements

Code:

typedef struct {
      unsigned char      mNum;      
      unsigned char      mDay;      
      unsigned char      rHour;      
      unsigned char      rMin;      
      unsigned char      sHour;      
      unsigned char      sMin;      
        unsigned char   nHour;  
        unsigned char   nMin;  
} SUNTIME;

SUNTIME sSunTime[] = {
{ 1,  1,  9,  1, 15, 53, 12, 27},
{ 1,  2,  9,  1, 15, 55, 12, 28},
{ 1,  3,  9,  0, 15, 56, 12, 28},
// a lot of elements are deleted to shorten example
{ 7, 31,  5, 20, 21, 39, 13, 30},   // this is an element 212

 };
 /* If this and previous line has to be moved after next element of array - 213 element, code stops working.
 
{ 8,  1,  5, 22, 21, 37, 13, 30},
{ 8,  2,  5, 24, 21, 35, 13, 30},
// here a lot of elements are deleted to shorten example
{12, 31,  9,  1, 15, 52, 12, 26},
{ 0,  0,  0,  0,  0,  0,  0,  0}  // element 368, i guess
};

*/

void setup() {  
 Serial.begin(9600);
 delay(1000);
 Serial.println("Start ");
 delay(1000);
 PrintTime();
}

void loop() {
  delay(1000);
}


void PrintTime(void){
  int i;
  for (i = 0; i < sizeof(sSunTime)/sizeof(SUNTIME); i++){
    Serial.print(" i="); Serial.print(i, DEC);
    Serial.print("  M= "); Serial.print(sSunTime[i].mNum, DEC);
    Serial.print("  D= "); Serial.print(sSunTime[i].mDay, DEC);
    Serial.print(" rH= "); Serial.print(sSunTime[i].rHour,DEC);    
    Serial.print(" rM= "); Serial.print(sSunTime[i].rMin ,DEC);    
    Serial.print(" sH= "); Serial.print(sSunTime[i].sHour,DEC);    
    Serial.print(" SM= "); Serial.print(sSunTime[i].sMin,DEC);    
    Serial.print(" nH= "); Serial.print(sSunTime[i].nHour,DEC);  
    Serial.print(" nM= "); Serial.print(sSunTime[i].nMin,DEC);    
    Serial.println( ". ");  
  }
}


Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24302
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Put the tables into PROGMEM
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 13
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thks. could you point me to some guidelines?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 13
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thks again for hint.
See code that works.

Code:
#include <avr/pgmspace.h>
typedef struct {
      unsigned char      mNum;      
      unsigned char      mDay;      
      unsigned char      rHour;      
      unsigned char      rMin;      
      unsigned char      sHour;      
      unsigned char      sMin;      
        unsigned char   nHour;  
        unsigned char   nMin;  
} SUNTIME;

SUNTIME sSunTime[] PROGMEM = {
{ 1,  1,  9,  1, 15, 53, 12, 27},
{ 1,  2,  9,  1, 15, 55, 12, 28},
{ 1,  3,  9,  0, 15, 56, 12, 28},
// a lot of elements are deleted to shorten example

{12, 31,  9,  1, 15, 52, 12, 26},
{ 0,  0,  0,  0,  0,  0,  0,  0}  // element 368
};



void setup() {  
 Serial.begin(9600);
 delay(1000);
 Serial.println("Start ");
 delay(1000);
 PrintTime();
}

void loop() {
  delay(1000);
}


void PrintTime(void){
  int i;
  for (i = 0; i < sizeof(sSunTime)/sizeof(SUNTIME); i++){
     Serial.print(" i="); Serial.print(i, DEC);
    Serial.print("  M= "); Serial.print(pgm_read_byte(&(sSunTime[i].mNum)),  DEC);
    Serial.print("  D= "); Serial.print(pgm_read_byte(&(sSunTime[i].mDay)),  DEC);
    Serial.print(" rH= "); Serial.print(pgm_read_byte(&(sSunTime[i].rHour)), DEC);    
    Serial.print(" rM= "); Serial.print(pgm_read_byte(&(sSunTime[i].rMin)),  DEC);    
    Serial.print(" sH= "); Serial.print(pgm_read_byte(&(sSunTime[i].sHour)), DEC);    
    Serial.print(" SM= "); Serial.print(pgm_read_byte(&(sSunTime[i].sMin)),  DEC);    
    Serial.print(" nH= "); Serial.print(pgm_read_byte(&(sSunTime[i].nHour)), DEC);  
    Serial.print(" nM= "); Serial.print(pgm_read_byte(&(sSunTime[i].nMin)),  DEC);    
    Serial.println( ". ");  
  }
}
« Last Edit: January 22, 2011, 02:38:49 pm by 1Pats » Logged

Pages: [1]   Go Up
Jump to: