Skip to content

Instantly share code, notes, and snippets.

@ilomon10
Last active May 12, 2021 07:24
Show Gist options
  • Save ilomon10/998c947a1fbdb1d08bbe65086c954194 to your computer and use it in GitHub Desktop.
Save ilomon10/998c947a1fbdb1d08bbe65086c954194 to your computer and use it in GitHub Desktop.
Algorithms for reading and interpolating measurements from specified table lookups.
// Ref: https://forum.arduino.cc/t/how-to-create-lookup-table/116161/19
// Title: How to create lookup table
int[] tableLookup = {
140, 300,
151, 280,
162, 260,
184, 240,
211, 220,
250, 200,
285, 180,
328, 160,
371, 140,
0, 0
};
float getValue(int[] lookup, int currentValue) {
float value = 0;
for (int i=0; i<lookup.length-2; i=i+2) {
if ((currentValue >= lookup[i]) && (currentValue <= lookup[i+2])) {
value = lookup[i+1] - ((lookup[i+1]-lookup[i+3]) * ((currentValue-lookup[i]) / (lookup[i+2]-lookup[i])));
break;
}
}
return value;
}
getValue(lookup, 162);
// 243.63636363636363
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment