{"id":239,"date":"2014-09-30T18:58:52","date_gmt":"2014-09-30T18:58:52","guid":{"rendered":"http:\/\/blogs.discovery.wisc.edu\/wearable14\/?p=239"},"modified":"2014-09-30T18:58:52","modified_gmt":"2014-09-30T18:58:52","slug":"the-map-function","status":"publish","type":"post","link":"https:\/\/blogs.discovery.wisc.edu\/wearable14\/2014\/09\/30\/the-map-function\/","title":{"rendered":"The Map Function"},"content":{"rendered":"<p>Here is a function that may be useful for many of the projects.<\/p>\n<p style=\"padding-left: 30px\">map(value, fromLow, fromHigh, toLow, toHigh)<br \/>\n<a href=\"http:\/\/arduino.cc\/en\/reference\/map\">http:\/\/arduino.cc\/en\/reference\/map<\/a><\/p>\n<p>Here is a diagram of how this function works<\/p>\n<p><a href=\"http:\/\/blogs.discovery.wisc.edu\/wearable14\/files\/2014\/09\/map1.png\"><img loading=\"lazy\" class=\"aligncenter size-medium wp-image-240\" src=\"http:\/\/blogs.discovery.wisc.edu\/wearable14\/files\/2014\/09\/map1-300x283.png\" alt=\"map1\" width=\"300\" height=\"283\" srcset=\"https:\/\/blogs.discovery.wisc.edu\/wearable14\/files\/2014\/09\/map1-300x283.png 300w, https:\/\/blogs.discovery.wisc.edu\/wearable14\/files\/2014\/09\/map1.png 575w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>The function takes a <strong>value<\/strong> that is in the range of <strong>fromHigh<\/strong> to <strong>fromLow<\/strong>.\u00a0 It then remaps the <strong>value<\/strong> to <strong>toHigh<\/strong> to <strong>toLow<\/strong>.<\/p>\n<p>One very nice applicatin of this technique is for remapping analog input to analog output.<\/p>\n<div class=\"sourceblocktext\">\n<div class=\"arduino\" style=\"padding-left: 30px\">\n<span class=\"kw1\">void<\/span> <span class=\"kw3\">loop<\/span><span class=\"br0\">(<\/span><span class=\"br0\">)<\/span><br \/>\n<span class=\"br0\">{<\/span><br \/>\n<span class=\"kw1\">int<\/span> val <span class=\"sy0\">=<\/span> <span class=\"kw1\">analogRead<\/span><span class=\"br0\">(<\/span><span class=\"nu0\">0<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span><br \/>\nval <span class=\"sy0\">=<\/span> <span class=\"kw1\">map<\/span><span class=\"br0\">(<\/span>val<span class=\"sy0\">,<\/span> <span class=\"nu0\">0<\/span><span class=\"sy0\">,<\/span> <span class=\"nu0\">1023<\/span><span class=\"sy0\">,<\/span> <span class=\"nu0\">0<\/span><span class=\"sy0\">,<\/span> <span class=\"nu0\">255<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw1\">analogWrite<\/span><span class=\"br0\">(<\/span><span class=\"nu0\">9<\/span><span class=\"sy0\">,<\/span> val<span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"br0\">}<\/span><\/div>\n<div class=\"arduino\" style=\"padding-left: 30px\"><\/div>\n<div class=\"arduino\">Another interesting aspect of this function is that it allows us to flip the lows and highs as shown in the diagram below.<\/div>\n<\/div>\n<p><a href=\"http:\/\/blogs.discovery.wisc.edu\/wearable14\/files\/2014\/09\/map2.png\"><img loading=\"lazy\" class=\"aligncenter size-medium wp-image-241\" src=\"http:\/\/blogs.discovery.wisc.edu\/wearable14\/files\/2014\/09\/map2-300x280.png\" alt=\"map2\" width=\"300\" height=\"280\" srcset=\"https:\/\/blogs.discovery.wisc.edu\/wearable14\/files\/2014\/09\/map2-300x280.png 300w, https:\/\/blogs.discovery.wisc.edu\/wearable14\/files\/2014\/09\/map2.png 579w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a>This may be really useful for items such as the RGB LED that work by driving a signal low.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is a function that may be useful for many of the projects. map(value, fromLow, fromHigh, toLow, toHigh) http:\/\/arduino.cc\/en\/reference\/map Here is a diagram of how this function works The function takes a value that is in the range of fromHigh to fromLow.\u00a0 It then remaps the value to toHigh to toLow. One very nice applicatin &hellip; <a href=\"https:\/\/blogs.discovery.wisc.edu\/wearable14\/2014\/09\/30\/the-map-function\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">The Map Function<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.discovery.wisc.edu\/wearable14\/wp-json\/wp\/v2\/posts\/239"}],"collection":[{"href":"https:\/\/blogs.discovery.wisc.edu\/wearable14\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.discovery.wisc.edu\/wearable14\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.discovery.wisc.edu\/wearable14\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.discovery.wisc.edu\/wearable14\/wp-json\/wp\/v2\/comments?post=239"}],"version-history":[{"count":1,"href":"https:\/\/blogs.discovery.wisc.edu\/wearable14\/wp-json\/wp\/v2\/posts\/239\/revisions"}],"predecessor-version":[{"id":242,"href":"https:\/\/blogs.discovery.wisc.edu\/wearable14\/wp-json\/wp\/v2\/posts\/239\/revisions\/242"}],"wp:attachment":[{"href":"https:\/\/blogs.discovery.wisc.edu\/wearable14\/wp-json\/wp\/v2\/media?parent=239"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.discovery.wisc.edu\/wearable14\/wp-json\/wp\/v2\/categories?post=239"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.discovery.wisc.edu\/wearable14\/wp-json\/wp\/v2\/tags?post=239"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}