简体   繁体   中英

Printing array contents to Serial, Giving unexpected values

I'm trying to send array contents over Serial. This is the code:

    #include <PCM.h>
    const unsigned char sample[] PROGMEM = {
    126, 127, 128, 128, 128, 128, 128, 127, 128, 128, 128, 129, 129, 128, 127, 128, 128, 127, 126, 127, 128, 129, 128, 127, 126, 127, 128, 128, 126, 126, 127, 127, 127, 127, 127, 127, 126, 127, 129, 130, 129, 128, 126, 126, 126, 126, 127, 129, 130, 129, 127, 127, 127, 127, 128, 128, 128, 128, 127, 127, 127, 127, 127, 127, 128, 130, 131, 129, 127, 126, 126, 126, 127, 127, 128, 128, 128, 128, 127, 128, 128, 127, 127};

void setup()
{
Serial.begin(115200);

delay(3000);
for (int i=0; i<sizeof(sample); i++)
{
delay(100);
Serial.println(sample[i]);
}
}

void loop()
{
}

When I start monitoring serial port, it gives non-intended output, not the original values inside the array. This is the Output of the first code "with Println":

0
0
6
0
0
0
1
0
0
6
6
6
6
6
6
6
0
0
135
0
0
6
0
0
1
3
0
6
171
0
0
0
0
0
0
0
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
2
6
1
4
45
0
0
6
0
6
0
248
254

This is the output with "Write": i could not paste the output of "write", i captured it as jpg

But, when I do this, I get the values I want, but I'll lose the FOR loop,

 #include <PCM.h>

 const unsigned char sample[] PROGMEM = {
 126, 127, 128, 128, 128, 128, 128, 127, 128, 128, 128, 129, 129, 128, 127, 128, 128, 127, 126, 127, 128, 129, 128, 127, 126, 127, 128};
 void setup()
 {
 Serial.begin(115200);
 delay(3000);
 //for (int i=0; i<sizeof(sample); i++)
// {
 delay(100);
 Serial.println(sample[0]);
 Serial.println(sample[1]);
 Serial.println(sample[2]);
 Serial.println(sample[3]);
 Serial.println(sample[4]);
// }
 }
 void loop()
 {
 }

What is the problem? I could not figure it out. Any help will be appreciated.

NOTE: Both codes should give same result for the first 5 values in the array, but it did not, the second code gives "126 127 128 128 128" and it is good, but the first code wont, and it has an only difference of having the variable "i" instead of listing all contents of the array one by one.

Thank you in advance,

I changed the type of memory from Program memory "PROGMEM" to normal or dynamic memory and every thing goes fine in this code, i could iterate with "for" loop easily with no problem.

Developers should find out how to iterate with "for or while" or any loop instruction in Program memory "PROGMEM", not just the dynamic memory (or show us how to if exist ^_^)

Thanks,

Sizeof returns the amount of bytes that are reserved on the system for your variable. Because it's an array the sizeof doesn't know what type of variable is being used. To get a variable array size and you want to loop through array you should check the sizeof the array but also the size of the variable in side of the array.

int array[] = {1,2,3,4}
size_t arraySize = sizeOf(array) / sizeOf(array[0])

this will return 4 as aspected. you explicitly tell the function it's dealing with a int because it's defined in the first index of the array. please look at this topic for more information 1 :

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM