Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Map function with arrays?

Status
Not open for further replies.

Daniel Wood

Member
Hi guys

I have a working circuit at the moment measuring a sensor and I am using the arduino map function to calibrate my results

float map(float x)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}


Everything works fine but there are some non-linear regions to my sensor which is throwing off the results, does anyone know of a version of this method of calibration that can use arrays as inputs so I can do a 10 to 20 point calibration?
 
Last edited:
Not going to get close using map. Map going to round off anyway so if you had a table its just going to be divided into parts with map.

You'll have to write your own map and call it something like Nmap because map is built in to arduino ide
The example you posted is part of it
For the mathematically inclined, here's the whole function

long map(long x, long in_min, long in_max, long out_min, long out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
 
Last edited:
Isn't that just a linear interpolation (equation of a straight line)?

I hate how Arduino tries to make things "simple" by making people lazy, or "reluctant to learn".. no need to work out simple linear functions anymore.

Graph the input of the sensors versus the output you wan't. Then we can figure out how to do the "mapping".
I guess you need a piecewise linear function, which you can turn into lookup table (array).. and maybe use interpolation for extra accuracy.
(google "piecewise linear function" and "linear interpolation")
 
Last edited:
Thanks guys!

Ian, I am half way through solving this thing.. I think all I have to do is wrap the function in a for statement and it should be good to go
(moved to android basic)

Code:
Sub MultiCalibrate


  Dim x As Float
  Dim y As Float
  Dim i As Int

  For i As Int = 0 To NO_OF_POINTS
 
  If x >= INN(i) AND x <= INN(i+1)
     y = (x-INN(i))*(OUT(i)-OUT(i+1))/(INN(i)-INN(i+1))+OUT(i)
  End If
 
  Return y
End Sub
 
(moved to android basic)
Cool!! having fun...

If you have an array of calpoints.. You only need to map to the four points that are relevant..

Use you analogue input to determine which four cal points to use.. The lower two become your zero and the upper two your span...

Once you determine the four points you can pass that to the map function....
 
Your IF can be simpler

For i As Int = 1 To NO_OF_POINTS
If x < INN(i)
.. You have found the right "i".
 
This is how we are rolling
 

Attachments

  • Lazy.jpg
    Lazy.jpg
    130.8 KB · Views: 222
Thanks T for telling me the correct terminology
Well it works like a charm now. I'll leave it here for reference in case anyone needs it in the future


Code:
'Both arrays must be equal in length
OUT = Array As Int(0,67,105,209,326,419,520,631,727,838,942,1040,1150,1253,1371,1462,1569,1682,1779,1881,1987,2078,2210,2315,2409,2513,2620,2730,2835,2944,3034,3150,6430,9518,15810,0)
INN = Array As Int(2,4 ,5  ,7  ,12 ,15 ,19 ,22 ,26 ,30 ,34 ,37  ,41  ,44  ,48  ,52  ,55  ,59  ,62  ,66  ,69  ,73  ,77  ,81  ,84  ,87  ,91  ,95  ,100 ,105 ,109 ,110 ,216 ,318 ,514,0)


Sub MultiCalibrate

  Dim i As Int
  Dim x As Long

  x = PARAM(2)

  For i = 0 To INN.Length-1
    If (x <= INN(i)) OR (i >= INN.Length-2) Then
        temp1 = (((x-INN(i))*(OUT(i)-OUT(i+1))/(INN(i)-INN(i+1))+OUT(i))/1000)
        Exit
    End If
  Next

End Sub

Ian: Haha the lazy sods! Tell the boys that I envy them!.. You might be interested in the App I'm making.. I finally got that android application working! :happy:
 

Attachments

  • Screenshot_2015-04-09-14-26-18[1].png
    Screenshot_2015-04-09-14-26-18[1].png
    316 KB · Views: 234
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top