Pages: [1] 2   Go Down
Author Topic: Serial print of arrays  (Read 1429 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello guys,

I am experiencing one really weird problem. I have variable Theta which is declared as a array of floats, and just for test, I want to print on serial port first 10 values. My code goes like this, trivial example:
Code:
void setup() {
     
float Theta[1001] = {-1.454425, 0.002870, -0.060573...

    Serial.begin(9600);   
     for (i = 0; i <= 10; i = i + 1) {
            Serial.println(Theta[i]);
           }
}
 

And, nothing is printed.. But when I change my code to print some arbitrary value, in e. Serial.println(Theta[3]), works fine.
What do you think could be problem? Has someone experienced such strange issues?

Add. info, array Theta is assigned to 1001 members, and is stored in Flash, using PROGMEM keyword.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48569
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
float Theta[1001]
That uses 4004 of the 2000 bytes of memory you have.
Logged

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

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.
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.

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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, :/
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48569
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
so,if I put any member in a loop, it is normally printed 10 times.. but this, :/
is not how to print a value in PROGMEM.

Back to the documentation for you...
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

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

Quote
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.
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.

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48569
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
OK, guys.. problem still unsolved.
Are you still creating a 1001 * 4 byte array?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes. Here is a code..
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;
    }
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48569
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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..
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48569
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Psst, reply #6
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.

Pages: [1] 2   Go Up
Jump to: