# Help with simple lookup table.

I have been scouring the forums and google and have not found a tutorial specifically about how to create and access a lookup table. I am only trying to take an interger value that is returned through a CAN BUS shield for Vehicle Speed and then reference a numerical value in a lookup table. I want to be able to refence the numerical value according to the vehicle speed. For example:
VSS Numerical Value
25 10
26 12
27 17
28 30
29 32
30 40
etc...

The Numerical Value has no relation to the Vehicle Speed value so I cannot use an equation.
Basically it will read the VSS value and assign it to "VSS":
Canbus.ecu_req(VEHICLE_SPEED,VSS);

then I want it to get the Numerical Value that relates to that speed value.

Thanks

A lookup table is not something magic. It is simply an array. The discrete known input values are listed. The output is determined by finding the element in the array that is above and below the known values in the array, and performing interpolation based on where the input value is relative to the upper and lower bounds.

There is a MultiMap function on the playground (Arduino Playground - MultiMap) that does the interpolation for you.

If Multimap does not do what you need, then you just want this I think, which returns a speed for the values you load.

int VSS [ ] = {0,0,0,0,0,0,0,0,0,0,25,0,26,0,0,0,0,27,0,0,0,0,0,0,0,0,0,0,0,0,28,0,29,0,0,0,0,0,0,0,0,0,30};

speed = VSS[10]; // would result in speed = 25.

or this, if I misinterpreted what you're after:

int VSS [ ] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,10,12,17,30,32,40, ...};

then
num_valu = VSS [25]; // would result in num_valu = 10 for example.

http://arduino.cc/en/Reference/Array

Wastes a few bytes of memory, but if you can't map out an equation for it ...

In theory any n-size lookup table can be put into a polynome of the order n+1 , but in practice this is often (far) worse than a lookup table.

CrossRoads solution is the easiest one: I elaborate on that.

VSS Numerical Value
25 10
26 12
27 17
28 30
29 32
30 40

I assume that VSS is 25 and up. Arrays in C typically begin at index 0, so what you need to make is a lookup table for (VSS-25)

``````int lut[] = { 10,12,17,30,32,40, ... };

....
int VSS = measure();
Serial.println(lut[VSS-25], DEC);
...
``````

by using VSS-25 the array is used more efficiently no 24 entries with 0's

Hope this helps,

That's a neat trick.

Do you gonna redo some code now?

Another lookup technique is if both VSS and thevalue where "jumpy"

int lut[][2] = { {25,10}, {26,12}, {27,17}, {28,30}, {29,32}, {30,40} };

Then you search the value in the first column - lut[idx,0] - and if found you use the value in the second column
e.g 27 is at idx=2 then you use: value = lut[idx,1];

Can also be done with two separate array's which have indexes linked - see the multiMap function on playGround.

No need, but certainly something to keep in mind 8)

I like the 2nd one, altho that requires a loop to look thru the array looking for a value.

The second technique is called a dictionary, it consist of {key,value} pairs, in Java and C# there exists classes for such lookup tables