0

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,

Sharista
  • 11
  • 4
  • I'm not sure what's going on here, but does the second example still "work" if you put a delay before each `println`? That's more like the first example. Similarly, what happens if you just have the first `println` in the loop: do you correctly get a load of 126's? – Rupert Swarbrick Dec 05 '15 at 08:59
  • 1
    Use write instead of println. Printeln adds LF character after each byte. – Helen Downs Dec 05 '15 at 11:44
  • thanks for the replay, Yes, i get the load of 126s u mentioned, thats my problem, When i use a variable "i" to loop the array, i get the non-array values (as in the first code) , but when i call the items by their respective index , i get them correctly as in the array... but i'll lose the for loop because i can't iterate them one by one ! – Sharista Dec 05 '15 at 12:06
  • Write will also give unintended values, i mean not the same values in the array... i'm pretty sure both gives the same values but may be in different format that i could not recover back to values stored in the array. – Sharista Dec 05 '15 at 12:33
  • What's the non-intended output like in the first code? – Marvin Wang Dec 06 '15 at 01:58
  • I added the output of the first code, with println and with write... – Sharista Dec 07 '15 at 05:06
  • Hi everyone, No replay until now? – Sharista Dec 09 '15 at 11:42

2 Answers2

1

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,

Sharista
  • 11
  • 4
0

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:

jeffrey
  • 26
  • 3