Go Down

Topic: Importing a large lookup table (Read 2378 times) previous topic - next topic

Hello,
   I need to setup a large (100 or so pairs of ints) lookup table to connect two set of numbers that are not mathematically related, i.e. One cannot be calculated from the other.
I am plannig to set up an array like this:

Code: [Select]
int table [100] [1] ;

And then fill it like this:

Code: [Select]
table [0] [0] = x0 ;
table [0] [1] = y0 ;
table [1] [0] = x1;
table [1] [1] = y1;
........................


I could easilly do it within the sketch but that would make the sketch heavy to read.
Is there a way to set up the table in a separate text file:

xo,yo
x1,y1
x2,y2
........

and then read or import it into the array?

That would have the additional advantage to be able to tweak the table without modifying verifying and re-uploading the sketch

TIA
There are three kind of people in the world: Those who can count, and those who can't

Cybernetician

Put it in Flash memory of controller
From Idea To Invention

wildbill

If you have the hardware needed to read from an SD card, it would be relatively easy to read your data from a text file at run time. Alternatively you could store it in EEPROM, but you'd have to write a program on the PC to send the data to put there, both for the initial load and when you wanted to change it. Or you could put the initialization code in another tab in the IDE to keep it segregated from your main sketch. With this last method, you would still have to reload the sketch every time you made a change. Another way would be to use the Arduino as a web client and go get the data from a web server at startup.

dhenry

Put the array itself in a .c file and declare it as extern in a .h file.

In your code, include the .h file and then include the .c file in the project.

Cybernetician

Quote
That would have the additional advantage to be able to tweak the table without modifying verifying and re-uploading the sketch


EEPROM Or SD card best options.
From Idea To Invention

AWOL

Code: [Select]
int table [100] [1] ;
Code: [Select]
table [0] [1] = y0 ;
Sorry, no.

Code: [Select]
int table [100] [2] ;
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

robtillaart

Quote
(100 or so pairs of ints) lookup table

questions pop up.
What do these values represent?
what is the range of values?
Can you post a part of the table?
Would it fit in a byte ?
Are there repeating patterns in the table?
Are they interpolatable?
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

CrossRoads

Are you using the tabs feature of the IDE?
Put the array and your other pin mappings and global variations in one tab, called a_presetup. Be sure to use progmem for the array unless the sketch needs to change the values.
put void setup in a 2nd called b_setup
put void look in a 3rd called c_loop
seperates things nicely.
When you save, the first tab will be your sketch file name, I use that for any notes I want to keep.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Thanks for all the answers !
I was (vaguely) thinking along the lines of dhenry and crossroads' solutions. I am going to research those
I was thinking maybe I can just #include  it or something.
Another thing i thought would be to create a library that does the data loading but that seems to me more trouble that it's worth, to keep the long list away from the program itself.

There are three kind of people in the world: Those who can count, and those who can't

dhenry

Quote
I was thinking maybe I can just #include  it or something.


You can. If you do that, you can only include it once, or you create local copies of global variables.

Nick Gammon

Code: [Select]

int table [100] [1] ;


As AWOL was suggesting, a table dimension of 1 isn't much use.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Nick Gammon

Code: [Select]
table [0] [0] = x0 ;
table [0] [1] = y0 ;
table [1] [0] = x1;
table [1] [1] = y1;


No. Assign the values like this:

Code: [Select]
int table [100] [2] = {
  { 1, 2 },
  { 3, 4 },

// and so on

  };


And then keep it in flash memory with the PROGMEM directive so it doesn't use any RAM.

eg.

Code: [Select]

int PROGMEM table [100] [2] = {
  { 1, 2 },
  { 3, 4 },
  { 5, 6 },

// and so on

  };


http://www.arduino.cc/en/Reference/PROGMEM
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

#12
Nov 17, 2012, 09:20 pm Last Edit: Nov 17, 2012, 10:00 pm by Thot Reason: 1
Thanks AWOL and Nick,
 I am still baffled by the fact that in the declaration "1" means 1 total and in the assignment "1" means the second item: 0 - 1.
I keep on making this mistake and the IDE  keeps on reminding me about that (I'm glad the IDE didn't get tired of that yet).

Thanks also for the PROGMEM  tip and for the link! I can do it so long as the program does not need to modify it, correct?
Though it may not be necessary as an int array of 100 x 2 entries should occupy 2 x 100 x 2 = 400 bytes).
As the second numbers are not bigger than a byte I could create two monodimensional arrays, one int and one byte for a total of 100 * (2 + 1) = 300 bytes (plus some overhead, I assume).

Finally, can I put the code in a function at the end of the sketch and call it from setup(), maybe by stating somehow it's  a global array or, being it a variable assignment must it go before setup() (that would make the sketch neater) ?

Thanks
There are three kind of people in the world: Those who can count, and those who can't

Nick Gammon


 I am still baffled by the fact that in the declaration "1" means 1 total and in the assignment "1" means the second item: 0 - 1.


You declare the number you want. eg.

Code: [Select]
int foo [2];


You get an array of two. However they are zero-relative. So you index into 0, 1.

Quote
Thanks also for the PROGMEM  tip and for the link! I can do it so long as the program does not need to modify it, correct?


Yes.

Quote
Finally, can I put the code in a function at the end of the sketch and call it from setup(), maybe by stating somehow it's  a global array or, being it a variable assignment must it go before setup() (that would make the sketch neater) ?


The assignments would take instructions. It seems much simpler to pre-assign the array (ie. declare the contents, don't assign them). Then it would need to be globally defined (or at least, static inside a function).

Quote
As the second numbers are not bigger than a byte I could create two monodimensional arrays, one int and one byte for a total of 100 * (2 + 1) = 300 bytes (plus some overhead, I assume).


You could do that, but if you keep them in program memory you have 32 Kb to play with, so you aren't running out yet.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

In search for the way to import an array of numeric data from a separate file into my sketch, I found a post in a c++ forum that showcased this code:

Code: [Select]
static const char* data[] = {  // <--- new line
#include "mydata.hh"
};   // <--- don't forget semi-colon


I put in "mydata.hh" the following text:
"tizio", "caio", "sempronio"

and created the following sketch:

Code: [Select]
static const char* data[] = {  // <--- new line
#include "E:\Downloads\Devices\Arduino\arduino-1.0.1\data\mydata.hh"
};   // <--- don't forget semi-colon

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

void loop () {
  Serial.print (data[0]);
  Serial.println();
 
}


It compiles flawlessly but the serial monitor shows nothing (should show "tizio")!

Then I tried this:

Code: [Select]
static const int data[] = {  // <--- new line changed char* to int
#include "E:\Downloads\Devices\Arduino\arduino-1.0.1\data\numbers.hh"
};   // <--- don't forget semi-colon

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

void loop () {
  Serial.print (data[0]);
  Serial.println();
 
}


with numbers.hh containing:
1,2,3,4

It also compiles flawlessly but the serial monitor shows nothing (in other attempt it showed numbers totally unrelated to the content of the file)!

I feel I am very close on learning how to import numeric data from an external file but I am unable to walk the final mile.

BTW, Arduino reference says that using pointers is very complicate and I probably don't need them anyways  :(

Any help is appreciated

TIA
There are three kind of people in the world: Those who can count, and those who can't

Go Up