Pages: [1]   Go Down
Author Topic: help understanding multiMap function  (Read 975 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 2
Posts: 106
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I need assistane understanding the multiMap() function found in the playground.

I also have 2 arrays, one is a series of known analogRead points and the second is a list of corresponding output results that I want with interpolation between.

I do not understand the structure and meaning of the multiMap() declaration statement in line 1. Can someone please break down what each of the 4 items are?
thanks
The post is at http://arduino.cc/playground/Main/MultiMap


Code:
The code:

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 map(val, _in[pos-1], _in[pos], _out[pos-1], _out[pos]);
}

Usage
Some snippets shows how multiMap() can be used:


  //My calibrated distance sensor - SHARP 2Y0A02 F 9Y
  // out[] holds the values wanted in cm
  int out[] = {150,140,130,120,110,100, 90, 80, 70, 60, 50, 40, 30, 20};
  // in[] holds the measured analogRead() values for defined distances
  int in[]  = { 90, 97,105,113,124,134,147,164,185,218,255,317,408,506};
  val = analogRead(A0);
  cm = multiMap(val, in, out, 14);



Logged

Offline Offline
Full Member
***
Karma: 2
Posts: 106
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

As an add to above, here is my code but I get a compilation error "undefined reference to multiMap(int, int*, int*, unsigned char)


Code:
int multiMap(int rawMM, int* _mils, int* _anlogin, uint8_t size);

 
 
  rawMM = analogRead(0); 
 
  int mils[] = { 1,25,50,75,100,125,150,175,200,225,250,300,350,400,475,550,600}; //output required
 
  int anlogin[] = { 338,381,420,456,492,518,544,571,592,614,640,670,696,719,743,765,779 }; //sampled anlogread
 
 

 
int mm = multiMap(rawMM, anlogin, mils, 17);
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 644
Posts: 50452
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I do not understand the structure and meaning of the multiMap() declaration statement in line 1. Can someone please break down what each of the 4 items are?
Code:
int multiMap(int val, int* _in, int* _out, uint8_t size)
The first argument is the value to map. (rawMM in your case)
The 2nd argument is the array of values to map from. (analogin in your case)
The 3rd argument is the array of values to map to. (mils in your case)
The 4th argument is the number of items in the arrays.

Quote
As an add to above, here is my code but I get a compilation error "undefined reference to multiMap(int, int*, int*, unsigned char)
"here is my code" is wrong. "here is a snippet of my code" would be correct. You need to post ALL of your code.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 224
Posts: 13917
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Code:
int multiMap(int rawMM, int* _mils, int* _anlogin, uint8_t size);

Your signature seems to have swapped analogRead and the mils, that can cause undesired results..

The call itself is correct:    int mm = multiMap(rawMM, anlogin, mils, 17);

Think of multiMap as Y = f(X);  Y is a function of X;  X is your rawMM;

Hope this helps...
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Full Member
***
Karma: 2
Posts: 106
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Helps a lot, thank you.
I have never come accross the uint_t size parameter before..

What does this mean? I have searched but am not clear on the meaning.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 644
Posts: 50452
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I have never come accross the uint_t size parameter before..
That name is missing something...

The u at the beginning means that the type is unsigned. The int part means that the value is integer type. The missing part is the size (typically, the name is uint8_t, uint16_t, uint32_t, or uint64_t). The _t on the end means that the variable occupies the specified number of bits regardless of the platform.

An int is different sizes based on whether it is on an Arduino, a PC, a Unix machine, or a mainframe computer. A uint16_t, on the other hand, is 16 bits, regardless of where the code runs.
Logged

Offline Offline
Full Member
***
Karma: 2
Posts: 106
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you very much. Appreciate your time.
Logged

Pages: [1]   Go Up
Jump to: