Maryland, USA
Offline
Jr. Member
Karma: 0
Posts: 79
|
 |
« on: November 16, 2012, 08:39:06 am » |
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: int table [100] [1] ; And then fill it like this: 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
|
|
|
|
|
Logged
|
There are three kind of people in the world: Those who can count, and those who can't
|
|
|
|
Offline
Full Member
Karma: 4
Posts: 187
|
 |
« Reply #1 on: November 16, 2012, 08:41:39 am » |
Put it in Flash memory of controller
|
|
|
|
|
Logged
|
From Idea To Invention
|
|
|
|
New Jersey
Offline
Edison Member
Karma: 24
Posts: 2345
|
 |
« Reply #2 on: November 16, 2012, 08:44:39 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 114
Posts: 2205
|
 |
« Reply #3 on: November 16, 2012, 08:45:33 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 4
Posts: 187
|
 |
« Reply #4 on: November 16, 2012, 08:50:36 am » |
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.
|
|
|
|
|
Logged
|
From Idea To Invention
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 137
Posts: 19017
I don't think you connected the grounds, Dave.
|
 |
« Reply #5 on: November 16, 2012, 09:00:21 am » |
int table [100] [1] ; table [0] [1] = y0 ; Sorry, no. int table [100] [2] ;
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 88
Posts: 9392
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #6 on: November 16, 2012, 10:05:04 am » |
(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?
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Boston area, metrowest
Offline
Brattain Member
Karma: 242
Posts: 16485
Available for Design & Build services
|
 |
« Reply #7 on: November 16, 2012, 02:23:11 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Maryland, USA
Offline
Jr. Member
Karma: 0
Posts: 79
|
 |
« Reply #8 on: November 16, 2012, 09:24:15 pm » |
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.
|
|
|
|
|
Logged
|
There are three kind of people in the world: Those who can count, and those who can't
|
|
|
|
Offline
Edison Member
Karma: 114
Posts: 2205
|
 |
« Reply #9 on: November 16, 2012, 09:34:42 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #10 on: November 17, 2012, 04:12:43 am » |
int table [100] [1] ;
As AWOL was suggesting, a table dimension of 1 isn't much use.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #11 on: November 17, 2012, 04:17:51 am » |
table [0] [0] = x0 ; table [0] [1] = y0 ; table [1] [0] = x1; table [1] [1] = y1; No. Assign the values like this: 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. int PROGMEM table [100] [2] = { { 1, 2 }, { 3, 4 }, { 5, 6 },
// and so on
};
http://www.arduino.cc/en/Reference/PROGMEM
|
|
|
|
|
Logged
|
|
|
|
|
Maryland, USA
Offline
Jr. Member
Karma: 0
Posts: 79
|
 |
« Reply #12 on: November 17, 2012, 03:20:53 pm » |
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
|
|
|
|
« Last Edit: November 17, 2012, 04:00:22 pm by Thot »
|
Logged
|
There are three kind of people in the world: Those who can count, and those who can't
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #13 on: November 17, 2012, 04:12:32 pm » |
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. int foo [2];
You get an array of two. However they are zero-relative. So you index into 0, 1. 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. 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). 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.
|
|
|
|
|
Logged
|
|
|
|
|
Maryland, USA
Offline
Jr. Member
Karma: 0
Posts: 79
|
 |
« Reply #14 on: November 21, 2012, 12:14:37 am » |
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: 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: 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: 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
|
|
|
|
|
Logged
|
There are three kind of people in the world: Those who can count, and those who can't
|
|
|
|
|