2D lookup table

Is it possible to create a 2D lookup table? I searched and could not find an answer to this question. I would (ideally) like a 16x16 table that would have engine RPM on one axis, engine load on the other and the table would be full of spark advance words empirically derrived.

I understand electronics but I am very new to programming so please bear with me here!

Sure. If the data is a 16 bit int, your 16x16 array is 512 bytes (16162).

int table[16][16];

-j

I can't find any reference to 'table' at all. How is this implemented?

table is the name of a variable. That code snippet was an example variable declaration for a variable named table, which is a 2D array of ints.

You're dealing with the C programming language here (C++, actually, but that's a superset of C).

Sounds like you may need to look at some of the arduino tutorial sketches, and/or maybe google for a C tutorial.

-j

I found an article about arrays and multidimensional arrays. Unfortunately the more I read the more confused I got. I think I'm in way over my head here.

Thanks for your help though.

P.S. wouldn't my 16x16 table be:

int table [15][15]

because it starts at zero?

P.S. wouldn't my 16x16 table be:

int table [15][15]

because it starts at zero?

Nope.

You declare arrays with their actual length (in this case 16), but index them starting at zero. So, with the 1D array:

int list[n]

The first entry is indexed as list[0] and the last as list[n-1]. It can be a little bit confusing at first, but it will seem very natural to you soon. :wink:

Just to muddy the waters a little more :wink:
Tables are indexed from 0 (as you know) so unless your rpms go from 0 to 15 you won't be able to use the table directly. You will need to map the rpm onto one of the 0-15 elements of the array (and likewise the other axis) before you can get the value that you want.

rpm value ----(map to 0-15)-----> array index -----(array access)----> desired value

int table[16][16];

int decode_rpm(int rpm)
{
   /* Code to turn a real rpm (integer) value into an index between 0 and 15 */
}


int decode_load(float load)
{
   /* Code to turn a real load (floating point value) value into an index between 0 and 15 */
}

int rpm_load_to_spark(int rpm, float load) 
{
    int rpm_index = decode_rpm(rpm);
    int load_index = decode_load(load);

    return table[rpm_index][load_index];
}

NB I know nothing about rpms vs load or even what a spark advance word is so take the above with a very large grain of salt. I chose a float for the load to drive the point about mapping home because you can't access an array with a float.

Charlieb

Like I said before, I'm in waaaaaay over my head here. I had a hard time getting my head around C before because I don't need to use it often enough for me to remember it well. If I read the code line by line with the book in front of me it sorta works but when I drop it and come back to it a month later I have to learn it all over again. I thought Wiring would be an easier path to follow but I don't think I'll have an easy time with it either because now C++ is somehow involved in it too- making it even more complicated.

What I'm looking for is a simple language like the Picaxe uses but with a fast microcontroller like the AVR.

Arrays in C are more or less like arrays in BASIC - except C starts counting at zero instead of one (just like your computer does :slight_smile: ).

-j

So the RPM will be integers but in what ranges? The loading will be also be integers right? In what ranges? Will the spark gaps be a decimal fraction? In what ranges?

RPM will be 0 to 8,000 RPM
Loading can be 0 to 1023 counts to match the ADC's output from a 0-5VDC pressure sensor 0-29.9" vacuum.
Spark advance will be integers 0 to 30 Degrees

So assuming linearity that gives you:

int table[16][16];

int decode_rpm(int rpm)
{
   /* initialize increment to be the difference in rpm between each array element and the next */
   int incremement = 8000/15;
   int index = 0;

   /* find which array element corresponds the the rpm value */
   while(index * increment < rpm)
        index++;

   return index;
}


int decode_load(int load)
{
   /* you can also just do the division explicitly */
   int divisor = 1023 / 15;
   int index = load / divisor;

   return index;
}

int rpm_load_to_spark(int rpm, float load) 
{
    int rpm_index = decode_rpm(rpm);
    int load_index = decode_load(load);

    return table[rpm_index][load_index];
}

I'm pretty sure this needs debugging but it has the right shape at least. Maybe it'll give you an idea of the direction you want to go.

Charlie

Thank you! I don't understand it just yet but I'll work on it.

How did this develop?

The reason i'm asking, is that i need a similar code.

Strongly aspired from this site: http://www.sportdevices.com/ignition/ignition.htm
I was hoping to adapt this to an Arduino, instead of using pic 12/16.

And i have tried to download the C source code, but it wont compile in the Arduino compiler?
Or in processing for that matter?

Anyone here who have the skills, and the desire, to convert the code to Arduino?

Processing is Java-based, so I wouldn't expect C to compile.
The C code has a "main" (as you would expect), whereas Wiring doesn't (or it does, but you don't see it).
You need to expend some effort translating.

Processing is Java-based, so I wouldn't expect C to compile.
The C code has a "main" (as you would expect), whereas Wiring doesn't (or it does, but you don't see it).
You need to expend some effort translating.

You're absolutely right about, me needing to spend more time and effort, to understand what i'm dealing with.

I don't know the real difference between Java and C based programming yet?
I mean many of the commands are the same, as far as i can see.

That being said.
I did a lot of programming, in basic, in the mid 80'es.
And practically haven't touched it since, so i'm sort of starting from scratch again.

I have done a little coding in Arduino the last few weeks,(and it works) but i have a long way to go yet. (i miss the goto(linenumber) function sometimes)

Anyway, what i was hoping for here, was that someone with better coding skills than me, could see the use of translating this, or similar project into Arduino code.

Preferably here on the forum, so that I, and/or others could learn along the way.

Translating the C isn't much of a problem - implementing the PIC assembler instructions, and retiming from a (roughly) 1 MIPS processor to a (roughly) 16 MIPS processor is trickier.
Post the code you've got, and we can help you with it.