Serial print of arrays

You need to post you actual code - if the array really is stored in flash memory, your example doesn't show it being accessed correctly.

Okay guys, thanks for fast response.. here is actual code:

#include <avr/pgmspace.h>

void setup() {  
    
float Theta[1001] PROGMEM = {-1.454425, 0.002870, -0.060573, -0.017364, 0.046953, 0.050278, 0.090067, 0.118486, 0.095992, 0.074007...};

    Serial.begin(9600);
    int i;
    Serial.println(Theta[100],4);   // This is just to check if it can catch the array
    for (i = 0; i <= 10; i = i + 1) {
       Serial.println(Theta[i]);
      }
}

void loop() {
    ;
    }

so,if I put any member in a loop, it is normally printed 10 times.. but this, :confused:

so,if I put any member in a loop, it is normally printed 10 times.. but this, :confused:

is not how to print a value in PROGMEM.

Back to the documentation for you...

Yeah, I see now that floating point type is not even supported in flash memory..
Thanks, I'll try it on normal way just as my pin 13 stops blinking unreasonably.

I see now that floating point type is not even supported in flash memory.

You could always cast from a similar sized type that is supported.

OK, guys.. problem still unsolved.

So, I stoped assingning my arrays to the flash memory, and removed my print loop from setup to loop section of program. And still unable to print array values on the serial port. But when I try to get them without loop, just one by one, everything's fine.

Any ideas, why this happens?

OK, guys.. problem still unsolved.

Are you still creating a 1001 * 4 byte array?

Yes. Here is a code..

void setup() {
    Serial.begin(9600);
  }
 
void loop() {   
        
    float Theta[1001]  = {-1.454425, 0.002870, -0.060573, -0.017364, 0.046953, 0.050278, 0.090067, 0.118486...  };  // and so 1001 times
    int i;
    
    for (i = 0; i <= 10; i = i + 1) {
       Serial.println(Theta[i],6);
    }

    // This loop loops forever and does nothing
    while(true) { 
      continue; 
    } 
}

So, you are aware that the array uses 4004 of the 2000 bytes of SRAM (unless you are using a Mega), aren't you?

Well, I'm using ATMega328, which is declared to have 32k of memory.. and after compiling I have a message: "Binary sketch size: 7902 bytes (of a 30720 byte maximum)". And when I try to print some arbitrary theta,Theta[500] in.e., on serial, Arduino does it nicely. All this made me think it is not a memory issue..

All this made me think it is not a memory issue..

And I think just the opposite. The Arduino has 3 kinds of memory - Flash, where the code goes, SRAM, where the data goes, and EEPROM.

The compiler reports how much Flash space the code is using. It does not tell you that you have used more than twice the available memory for your data.

Okay, so, any ideas how to solve this? Relocate to program memory? PROGMEM seems not to work with float type...

Psst, reply #6

I believe Mikal Hart's Flash library supports storing float arrays in Flash memory:

http://arduiniana.org/libraries/flash/

So,if I got you right, I should save my Theta as a some compatible type in Flash, in.e. signed long, and after reading convert it to float. But how to save my values in range from 0 to 1 from truncating and rounding? Maybe to multiply them with in.e. 100000 before saving? Ok, this could work.. thanks man

So,if I got you right, I should save my Theta as a some compatible type in Flash, in.e. signed long,

Not sure what you mean here.
What's wrong with PROGMEM float Theta [1001...whatever] = {3.45,6.27};?

well, libraty pgmspace.h does not support float type. And I could not find any function that reads it properly.. So i thought, before saving, adapt my data to intiger type, and the return it float, after reading.

So, read it back as a type you can read, and cast the result to float.

OK, just to inform you guys, I finally made it. Seems like there is support for storing float type data in Flash. All you have to do is store it:

PROGMEM float Theta[]={1,2,3,4...};

and read it out:

float a;
a =(float) pgm_read_float(&Theta[i]); // i can be some constant or counter

Thank you all, cheers.