Reading array value

I have saved the data in my Mega's program memory as array like this:

'const int flowInTable[1023] PROGRAM = { 0,0,0,...1,1,2,2,3,3, etc.....50};'

when I try to recall the values in the loop like this:
int flowIn;
for(int i=0;i<1023;i++)
I see some random values printed like 32, 24532, 26347,48 etc
but when I replace i , with some value say "flowInTable[450]", I see the correct value printed as in the array.
I don't understand why the variable i is not being replaced for increasing value?
Can someone help me locate the problem please ?

Hi @ark1944
try putting a delay(100) after the line serial.print(flowIn);
to allow time to print before fetching another value from the array.

Please post complete programs, not snippets.

Read the forum guidelines to see how to properly post code and some hints on how to get the most from this forum.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

that won't change anything and is totally misleading. print() will pause if the outgoing buffer is full.

The issue is likely in the code OP did not show... (the print would actually get all the values in the array next to each-other without any ',' separator, so the statement is very suspect).

@ark1944 ➜ Don't post snippets (Snippets R Us!)

What’s that?

good point, probably PROGMEM and not reading with the right access methods.

when using hardwired constant, the optimizer probably gets rid of the huge array and just plugs the value in the print() statement, hence the right number.

How big is 'flowInTable', really? Or should be? The declaration of 1023 is suspiciously close to 2^10 = 1024. If the table was designed to have some access method that depends on it being a power of two, with 1024 values and an index ranging from 0-1023, it may fail somewhere.

The optimising compiler is very, very clever.
It recognises the difference between a constant and variable array index.

Or if you want to use the conventional array index:

    flowIn = pgm_read_word(&flowInTable[i]);

@ark1944 the installation and troubleshooting section is not for your projects, but for getting the basic Arduino system working.
This clearly is your project, therefore I have moved it to a more appropriate section.

nice, just yesterday optimised some code reading from progmem the same way

2 posts were split to a new topic: Variable not increasing2