arduino实用数学函数之map()

map(value, fromLow, fromHigh, toLow, toHigh)
Description

Re-maps a number from one range to another. That is, a value of
fromLow would get mapped to toLow, a value of fromHigh to toHigh,
values in-between to values in-between, etc.

把一个数从一个范围变换到另一个范围。

Does not constrain values to within the range, because out-of-range
values are sometimes intended and useful. The constrain() function
may be used either before or after this function, if limits to the
ranges are desired.

不会把值强制限制在范围之内,因为超范围的值经常也是有用的。如果需要的范围做一限制。可以在这个函数之前或之后使用constrain()
函数。

Note that the "lower bounds" of either range may be larger or
smaller than the "upper bounds" so the map() function may be used
to reverse a range of numbers, for example

注意,两个范围中的“下界”要比“上界”大或下,这样map()可以用来反转一个范围,例如

y = map(x, 1, 50, 50, 1);

The function also handles negative numbers well, so that this
example

函数也可以处理负数,例如

y = map(x, 1, 50, 50, -100);

is also valid and works well.

也有效和正确

The map() 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.

map()函数使用整型,所以不会产生分数,分数将会被截去,并不是全面的或平均值(?)

Parameters 参数

value: the number to map

给map的值

fromLow: the lower bound of the value‘s current range

值现在的下界

fromHigh: the upper bound of the value‘s current range

值现在的上界

toLow: the lower bound of the value‘s target range

值目标范围的下界

toHigh: the upper bound of the value‘s target range

值目标范围的上界

Returns 返回值

The mapped value.

映射的值

Example

void setup() {}

void loop()
{
int val = analogRead(0); //读取0口的值
val = map(val, 0, 1023, 0, 255);//从0-1023映射到0-255
analogWrite(9, val);//把映射后的值写给9口
}

Appendix 附录

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;
}

时间: 2024-08-06 15:36:54

arduino实用数学函数之map()的相关文章

JQuery实践--实用工具函数

实用工具函数,$命名空间的一系列函数,但不操作包装集.它要么操作除DOM元素以外的Javascript对象,要么执行一些非对象相关的操作. JQuery的浏览器检测标志可在任何就绪处理程序执行之前使用这些标志.$.browser :msie,mozilla,safari,opera,version(引擎的版本)$.boxModel: 方框模型,true/false. 决定了元素的内容大小$.styleFloat: float样式的名称,值为字符串,供属性名称使用   element.style[

Python-lambda函数,map函数,filter函数

lambda函数主要理解: lambda 参数:操作(参数). lambda语句中,冒号前是参数,可以有多个,用逗号隔开,冒号右边的返回值.lambda语句构建的其实是一个函数对象 map函数: map(function_to_apply, list_of_inputs).map函数可以把list_of_inputs内的对象依次输入到function_to_apply中进行操作. filter函数: filter(function_to_apply, list_of_inputs).Filter

python笔记-lambda函数、sorted函数、map函数

1.lambda函数:又称匿名函数,示例如下: def f(x): return x**2 print f(4)  #16 g = lambda x:x**2 print g(4)  #16 2.map函数 print map(lambda x:x**2,range(10)) #[0, 1, 4, 9, 16, 25, 36, 49, 64, 81] 3.sorted函数 dict = {9:2,4:3,6:9,'a':'test','e':'fff','*':'$'} print sorted

Java语言程序设计(基础篇) 第四章 数学函数、字符和字符串

第四章 数学函数.字符和字符串 4.2 常用数学函数 方法分三类:三角函数方法(trigonometric method).指数函数方法(exponent method)和服务方法(service method) 4.4 String类型 String类型不是基本类型,而是引用类型(reference type).

Java--分支语句、循环、数组、控制台输入语句、常用数学函数

**-----本章节-----** 1.分支语句 2.循环 3.数组 4.控制台输入语句 5.部分常用的数学函数 ============================================================== 一分支语句 1.概念 (1)分支语句又称条件语句条件语句使部分程序可根据某些表达式的值被有选择地执行. (2)Java编程语言支持双路 if和多路 switch 分支语句. ===========================================

GPU编程中的常用数学函数

在GPU编程中,函数一般分为以下几种类型:数学函数.几何函数.纹理映射函数.偏导数函数.调试函数等.熟练利用好GPU自带函数,可以在一定程度上提高并行编程速度与效率. 关于数学数学函数(Mathematical Functions) 数学函数用于执行数学上常用计算,比如:三角函数.幂函数.向量和矩阵函数,这些函数一般都被重载,用来支持标量数据和不同长度的向量作为输入参数.列表如下: 标准函数库中的数学函数 未完待续......

?数学函数——在函数式编程背后的动力

函数式思维的动力来自数学.数学函数有很多特色--函数式语言试图模拟真实世界. 所以一开始,我们以一个加1函数开始: Add1(x) = x+1 这意思是什么?好吧,看起来十分直白.它意味着有一个操作以一个数字开始,然后给它加1. 我们引入一些术语: 可以被函数作为输入的值的集合叫做domain.这样,它可能是实数集合,为了简单,我们仅限于整数. 可以被函数作为输出的值的集合叫做range(更科学的应该是叫作codomain的image).还是仅限于整数. 函数被称作映射domain到range.

140926●日期时间操作、数学函数操作、表单验证

日期时间操作:var d=new Date();var d=new Date(1999,3,5); //时间是:1999-4-5 d.getFullYear();年d.getMonth();月(正常-1)d.getDate();天d.getDay();星期几d.getHours();d.getMinutes();d.getSeconds(); 数学函数操作:Math.ceil();Math.floor();Math.round();Math.random();Math.sqrt(); 表单验证:

Swift高阶函数:Map,Filter,Reduce

闭包介绍 Swift一大特性便是使用简洁的头等函数/闭包语法代替了复杂的blocks语法.希望我们在Swift中不再需要像fuckingblocksyntax中所描述的语法.(译者注:头等函数-即可将函数当作参数传递给其他的函数,或从其他的函数里返回出值,并且可以将他们设定为变量,或者将他们存储在数据结构中) 闭包是自包含的blocks,它能在代码中传递和使用. 本文我们将重点介绍匿名定义的闭包(如:定义成内联的且不具名)也称匿名闭包.我们能够将其作为参数传递给其他函数/方法或者将其作为返回值.