Question about FLASH.ARRAY

I am having trouble understanding what is wrong with this sketch.

Both variables x and y produce the same duplicate values.

I would greatly appreciate help with this question.

This is just a simple mock-up from a much larger sketch; however, this is my only issue with that sketch that I just can’t figure out.

x should equal 1022
y should equal 1024

but both x and y equal 1022…

Why???

#include <Flash.h>

FLASH_ARRAY(int, analog_values, 1022, 1024, 836, 844, 926, 934, 558, 566, 692, 700);

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

int x = analog_values[0];
int y = analog_values[1];

Serial.println(x);
Serial.println(y);
}

void loop()
{
}

Why???

Because you used too much punctuation.
Because you used a library that you did not provide a link to.
Because you posted your code incorrectly.
Because you have unrealistic expectations.

Shall I go on?

If the result of that library being used is to store values in an array that is in PROGMEM, you need to use the proper functions or methods to access those values. The standard array operator () may, or may not, be overloaded. Without knowing anything about the library, we can’t tell you.

Sorry, here is the link to the library.

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

The problem appears to be a compiler bug. The other possibility is const is not applied correctly to operator[] but that seems extremely unlikely because print works correctly.

I believe Mikal Hart typically uses a much newer version of avr-gcc toolset which, presumably, will have fewer bugs. I suggest replacing the toolset with the one available from Atmel.

I don’t understand what is going on. I downloaded the library, and compiled and linked your sketch, with the same results. Added some more code:

#include <Flash.h>

FLASH_ARRAY(int, analog_values, 1022, 1024, 836, 844, 926, 934, 558, 566, 692, 700);

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

  int x = analog_values[0];
  Serial.print("x = ");
  Serial.println(x);
  
  int y = analog_values[1];
  Serial.print("y = ");
  Serial.println(y);
  
  int stuff[10];
  for(int b=0; b<10; b++)
  {
    Serial.print("analog_values[");
    Serial.print(b);
    Serial.print("] = ");
    Serial.println(analog_values[b]);
    
    stuff[b] = analog_values[b];
  }
  
  for(int b=0; b<10; b++)
  {
    Serial.print("stuff[");
    Serial.print(b);
    Serial.print("] = ");
    Serial.println(stuff[b]);
  }
}

void loop()
{
}

which then produced:

x = 1022
y = 1024
analog_values[0] = 1022
analog_values[1] = 1024
analog_values[2] = 836
analog_values[3] = 844
analog_values[4] = 926
analog_values[5] = 934
analog_values[6] = 558
analog_values[7] = 566
analog_values[8] = 692
analog_values[9] = 700
stuff[0] = 1022
stuff[1] = 1024
stuff[2] = 836
stuff[3] = 844
stuff[4] = 926
stuff[5] = 934
stuff[6] = 558
stuff[7] = 566
stuff[8] = 692
stuff[9] = 700

The difference is that the original version has to actually store the result (x and y) for later use # but your version uses the result immediately. I think the gcc folks call that “register spill”; that’s probably where the bug lies.

# That’s not entirely accurate. If the compiler performs reorder optimizations the need to store the result can be eliminated. But I have seen no evidence that the Arduino version of gcc performs reorder optimizations.