Using Flash Memory

Goodmorning,
i am doing a sketch to control a servo which will adjust the air Fuel Ratio on a Porsche 911 SC from 1987, a piece of story!

I want to create a flash memory matrix in which store the value of the servo Output.
The matrix will have 2 axis TPS and RPM, and the value i will need is a 2d interpolation and i found a sketch on this forum that could be usefull to try.

this is the code i am using, and for the library flash.h i downloaded it from the library manager.

#include <Flash.h>

const int rpmSteps = 6; // Number of entries for rpm
const long rpmFactor = 1000; // Difference between entries for rpm
const int rpmMin = 2000; // Value of the first entry for rpm
const int rpmMax = rpmMin + (rpmSteps - 1) * rpmFactor; // Value of the last entry for rpm
const int tpsSteps = 7; // Number of entries for tps
const long tpsFactor = 10; // Difference between entries for tps
const int tpsMin = 0; // Value of the first entry for tps
const int tpsMax = tpsMin + (tpsSteps - 1) * tpsFactor; // Value of the last entry for tps

FLASH_TABLE(int, font_table, rpmSteps /* width of table */,
{100, 10, 0, 0, 0, 0}, // 100 %
{100, 60, 40, 30, 20, 10}, // 90 %
{100, 80, 70, 50, 30, 30}, // 80 %
{100, 90, 80, 80, 70, 60}, // 70 %
{100, 100, 90, 80, 80, 70}, // 60 %
{100, 100, 100, 90, 80, 80}, // 50 %
{100, 100, 100, 100, 100, 100}); // 40 %

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

void loop() {
Serial.println (magicnumber(6700, 17));
Serial.println (magicnumber(3100, 33));
delay (4000);
}

int magicnumber (int rpm, int tps) {
int rpmIL, rpmIH;
int tpsIL, tpsIH;
long rpmOffset, tpsOffset;

// Find right table indices to work on
if (rpm < rpmMin) {
rpmIL = rpmIH = 0;
rpmOffset = 0;
}
else if (rpm > rpmMax) {
rpmIL = rpmIH = rpmSteps - 1;
rpmOffset = 0;
}
else {
rpmIL = (rpm - rpmMin) / rpmFactor;
rpmIH = rpmIL + 1;
rpmOffset = (rpm - rpmMin) % rpmFactor;
}

if (tps < tpsMin) {
tpsIL = tpsIH = 0;
tpsOffset = 0;
}
else if (tps > tpsMax) {
tpsIL = tpsIH = tpsSteps - 1;
tpsOffset = 0;
}
else {
tpsIL = (tps - tpsMin) / tpsFactor;
tpsIH = tpsIL + 1;
tpsOffset = (tps - tpsMin) % tpsFactor;
}

// Interpolate the values
long fLL = font_table[tpsIL][rpmIL];
delayMicroseconds (0);
long fLH = font_table[tpsIL][rpmIH];
delayMicroseconds (0);
long fHL = font_table[tpsIH][rpmIL];
delayMicroseconds (0);
long fHH = font_table[tpsIH][rpmIH];

return (((fLL * (rpmFactor - rpmOffset)

  • fLH * rpmOffset) * (tpsFactor - tpsOffset))
  • ((fHL * (rpmFactor - rpmOffset)
  • fHH * rpmOffset) * tpsOffset))
    / (rpmFactor * tpsFactor);
    }

but i get an error:

In file included from C:\Users\utente4\Documents\Arduino\sketch_nov06a\sketch_nov06a.ino:1:0:
C:\Users\utente4\Documents\Arduino\libraries\Flash\src/Flash.h: In instantiation of ‘class _FLASH_ARRAY’:
C:\Users\utente4\Documents\Arduino\libraries\Flash\src/Flash.h:184:23: required from ‘void _FLASH_TABLE::print(Print&) const [with T = int]’
C:\Users\utente4\Documents\Arduino\sketch_nov06a\sketch_nov06a.ino:27:1: required from here
C:\Users\utente4\Documents\Arduino\libraries\Flash\src/Flash.h:116:21: internal compiler error: in type_hash_canon, at tree.c:7158

  • typedef T PROGMEM _DataType;*
  • ^~~~~~~~~*
    Please submit a full bug report,
    with preprocessed source if appropriate.
    See https://gcc.gnu.org/bugs/ for instructions.
    More than one library found for “Flash.h”
    Used: C:\Users\utente4\Documents\Arduino\libraries\Flash
    exit status 1
    Errore during compilation for board Arduino/Genuino Uno.

i could not find out which can be the issue.
I downloaded the flash.h only one time. Can be that is not the correct flash.h which code is referring to?

thanks for Help.

Cheer!

ilcaso1990:
this is the code i am using, and for the library flash.h i downloaded it from the library manager.

can you find the link to which Flash library you actually downloaded? They usually come with examples, try one of them to see if it compiles. May be the library is obsolete.

I’m not sure why you need the library… you can just use PROGMEM directly, here is an example:

#include <avr/pgmspace.h>
const int tpsSteps = 7; // Number of entries for tps
const int rpmSteps = 6; // Number of entries for rpm

const int16_t TPSxRPM[tpsSteps][rpmSteps] PROGMEM =
{
  {100,  10,   0,   0,   0,   0},  // 100 %
  {100,  60,  40,  30,  20,  10},  // 90 %
  {100,  80,  70,  50,  30,  30},  // 80 %
  {100,  90,  80,  80,  70,  60},  // 70 %
  {100, 100,  90,  80,  80,  70},  // 60 %
  {100, 100, 100,  90,  80,  80},  // 50 %
  {100, 100, 100, 100, 100, 100}   // 40 %
};

void setup() {
  Serial.begin(115200);
  for (uint8_t tpsIdx = 0; tpsIdx < tpsSteps; tpsIdx++) {
    Serial.print(100 - 10 * tpsIdx);
    Serial.print(F("%"));
    Serial.print(F("\t"));
    for (uint8_t rpmIdx = 0; rpmIdx < rpmSteps; rpmIdx++) {
      Serial.print((int16_t ) pgm_read_word(&TPSxRPM[tpsIdx][rpmIdx])); // this is how you access the data
      Serial.print(F(" "));
    }
    Serial.println();
  }
}

void loop() {}

You’ll see in the console that I print the table, read from FLASH memory

100% 100 10 0 0 0 0 
90% 100 60 40 30 20 10 
80% 100 80 70 50 30 30 
70% 100 90 80 80 70 60 
60% 100 100 90 80 80 70 
50% 100 100 100 90 80 80 
40% 100 100 100 100 100 100

the command to read from the array is

pgm_read_word(&TPSxRPM[[color=blue]tps Index[/color]][[color=red]rpm Index[/color]]))

where tps Index and rpm Index are the x/y position in the array
pgm_read_word() reads 2 bytes from the address you give as a parameter. 2 bytes is matching the number of bytes for the data stored in the array (int16_t is a signed integer on 2 bytes).

As your values seems to be between 0 and 100, you could use only one byte instead of two and would use then int8_t for the type of the data in the array

const int8_t TPSxRPM[tpsSteps][rpmSteps] PROGMEM =
{
  {100,  10,   0,   0,   0,   0},  // 100 %
  {100,  60,  40,  30,  20,  10},  // 90 %
  {100,  80,  70,  50,  30,  30},  // 80 %
  {100,  90,  80,  80,  70,  60},  // 70 %
  {100, 100,  90,  80,  80,  70},  // 60 %
  {100, 100, 100,  90,  80,  80},  // 50 %
  {100, 100, 100, 100, 100, 100}   // 40 %
};

and the pgm_read_byte() function to get the data

      Serial.print((int8_t) pgm_read_byte(&TPSxRPM[tpsIdx][rpmIdx]));

as a side note, please read forum rules and Please correct your post above and add code tags around your code:

[code]

[color=blue]// your code is here[/color]

[/code]

.

It should look like this:

// your code is here

(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)