{"id":7020,"date":"2021-12-23T08:46:50","date_gmt":"2021-12-23T08:46:50","guid":{"rendered":"https:\/\/www.qec.tw\/?p=7020"},"modified":"2021-12-28T09:26:09","modified_gmt":"2021-12-28T09:26:09","slug":"map","status":"publish","type":"post","link":"https:\/\/www.qec.tw\/zh\/86duino-reference\/map\/","title":{"rendered":"map()"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">[Math]<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"_description\">Description<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Re-maps a number from one range to another. That is, a value of&nbsp;<code>fromLow<\/code>&nbsp;would get mapped to&nbsp;<code>toLow<\/code>, a value of&nbsp;<code>fromHigh<\/code>&nbsp;to&nbsp;<code>toHigh<\/code>, values in-between to values in-between, etc.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Does not constrain values to within the range, because out-of-range values are sometimes intended and useful. The&nbsp;<code>constrain()<\/code>&nbsp;function may be used either before or after this function, if limits to the ranges are desired.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Note that the \u201clower bounds\u201d of either range may be larger or smaller than the \u201cupper bounds\u201d so the&nbsp;<code>map()<\/code>&nbsp;function may be used to reverse a range of numbers, for example<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>y = map(x, 1, 50, 50, 1);<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">The function also handles negative numbers well, so that this example<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>y = map(x, 1, 50, 50, -100);<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">is also valid and works well.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<code>map()<\/code>&nbsp;function uses integer math so will not generate fractions, when the math might indicate that it should do so. Fractional remainders are truncated, and are not rounded or averaged.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax<\/h2>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>map(value, fromLow, fromHigh, toLow, toHigh)<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Parameters<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>value<\/code>: the number to map<br><code>fromLow<\/code>: the lower bound of the value\u2019s current range<br><code>fromHigh<\/code>: the upper bound of the value\u2019s current range<br><code>toLow<\/code>: the lower bound of the value\u2019s target range<br><code>toHigh<\/code>: the upper bound of the value\u2019s target range<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Returns<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The mapped value.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example Code<\/h2>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>\/* Map an analog value to 8 bits (0 to 255) *\/\nvoid setup() {}\n \nvoid loop()\n{\n  int val = analogRead(0);\n  val = map(val, 0, 1023, 0, 255);\n  analogWrite(9, val);\n}<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Appendix<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">For the mathematically inclined, here\u2019s the whole function<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>long map(long x, long in_min, long in_max, long out_min, long out_max)\n{\n  return (x - in_min) * (out_max - out_min) \/ (in_max - in_min) + out_min;\n}<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">See also<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>[Language] <a href=\"https:\/\/www.qec.tw\/86duino-reference\/constrain\/\">constrain<\/a>()<\/li><\/ul>\n\n\n<hr>\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/www.qec.tw\/86duino\/86duino-language-reference\/\">Language Reference Home<\/a><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><em>The text of the 86Duino reference is a modification of&nbsp;<a href=\"http:\/\/arduino.cc\/en\/Reference\/HomePage\" target=\"_blank\" rel=\"noopener\">the Arduino reference<\/a>&nbsp;and is licensed under a&nbsp;<a href=\"http:\/\/creativecommons.org\/licenses\/by-sa\/3.0\/\" target=\"_blank\" rel=\"noopener\">Creative Commons Attribution-ShareAlike 3.0 License<\/a>. Code samples in the reference are released into the public domain.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>[Math] Description Re-maps a number from one range to another. That is, a value of&nbsp;fromLow&nbsp;would get mapped to&nbsp;toLow, a value of&nbsp;fromHigh&nbsp;to&nbsp;toHigh, values in-between to values [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":7022,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[195],"tags":[162],"class_list":["post-7020","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-86duino-reference","tag-86duino"],"_links":{"self":[{"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/posts\/7020","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/comments?post=7020"}],"version-history":[{"count":0,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/posts\/7020\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/media\/7022"}],"wp:attachment":[{"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/media?parent=7020"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/categories?post=7020"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/tags?post=7020"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}