Go Down

Topic: multiMap() function - integer linear interpolation (Read 11930 times) previous topic - next topic


Nov 01, 2014, 07:40 pm Last Edit: Nov 01, 2014, 09:16 pm by HazardsMind
The whole point to allowing multiple types was for decltype to choose the largest type and make it the returning result.

Originally I only allowed one type, but then you lose precision if the first type (valueToMap) is smaller then the rest. So allowing multiple types solves that, but as Paul pointed out, it can also cause problems if the data entered is foolish. A possible scenario for new programmers, i'm sure, but we can't prevent everything.
My GitHub:


but as Paul pointed out, it can also cause problems
I wasn't picking on your efforts. I've never had the need for templates, so I've not studied up on them. I don't know if you can create a template that takes different types, and still require that some of the function arguments must all be the same type. I guess it's time to get over the lack of knowledge of templates.
The art of getting good answers lies in asking good questions.


I wasn't picking on your efforts.
I wasn't saying you were, its just helpful criticism for me and others who read these posts.
My GitHub:


Nov 03, 2014, 09:41 am Last Edit: Nov 03, 2014, 09:44 am by pYro_65
Even disallowing Strings and strings, does it make sense to use different types for the from range (float fromMin, long fromMax), for instance? Does it make sense to use different types for the to range (long long toMin, byte toMax)?

C allows you to shoot yourself in the foot. C++'s templates allow you to blow your whole leg off.

This is a situation, I think, where a template is NOT a good idea.
Silly use of any code is a bad idea, and yes it does make sense if you understand the declaration.

You can read the post where I created this function (apart from the single map algo line that hazardsmind changed) and you will see why the different template parameters matter. http://forum.arduino.cc/index.php?topic=265085.msg1874470#msg1874470

And there is no reason why you cannot expand on type safety allowing only the types that you want. There is always a bigger picture :)
Forum Mod anyone?


How do I set up a library for multiMap
I have a error multiMap was not declared in this scope


Hello robtillaart,

I was wondering if you ever got round to reviewing the to-do list on Arduino Playground?

- Check if binary search is faster
- Implement a simple cache that holds the last used value (some projects would benefit)
I'll be grateful for any suggestions to optimise multimap to make it any faster if possible?



I've been trying to get multiMap to work, but it seems to be all over the place for me.
Code I'm using is below. I've simplified it down, and set the map data so input and output is the same, and it still doesn't output consistently. What am I doing wrong?

#include <LiquidCrystal.h>

const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

int outputreading = 0;
// note: the _in array should have increasing values
int multiMap(int val, int* _in, int* _out, uint8_t size)
  // take care the value is within range
  // val = constrain(val, _in[0], _in[size-1]);
  if (val <= _in[0]) return _out[0];
  if (val >= _in[size-1]) return _out[size-1];

  // search right interval
  uint8_t pos = 1;  // _in[0] allready tested
  while(val > _in[pos]) pos++;

  // this will handle all exact "points" in the _in array
  if (val == _in[pos]) return _out[pos];

  // interpolate in the right segment for the rest
  return (val - _in[pos-1]) * (_out[pos] - _out[pos-1]) / (_in[pos] - _in[pos-1]) + _out[pos-1];

void setup() {
  lcd.begin(16, 2);

void loop() {
lcd.setCursor(0, 0);
int out[] = {0,205,409,615,819,1021};
int in[] = {0,205,409,615,819,1021};
int val = analogRead(A0);
outputreading = multiMap(val, in, out, 6);


16 days, I finally figured out the issue.

32,767 is exceeded during calculations

Go Up