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!
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.
Just to muddy the waters a little more
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.
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.
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
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.
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.