ArduinoのSketch(スケッチ)で使用できる算術関数について解説します。
min
minは、2つの数値を比較して小さいほうの値を返します。
//Arduino Sketch Example: min(x, y)
//Date: 2015.1.18
//Edited and Modified by: easy labo
//Original Source: Arduino Reference (http://arduino.cc/en/Reference/HomePage)
//Syntax
min(x, y)
//Parameters
//x: the first number, any data type
//y: the second number, any data type
//Returns
//The smaller of the two numbers.
//Description
//Calculates the minimum of two numbers.
//Examples
sensVal = min(sensVal, 100); // assigns sensVal to the smaller of sensVal or 100
// ensuring that it never gets above 100.
//Note
/*
Perhaps counter-intuitively, max() is often used to constrain the lower end of a variable's range, while min() is used to constrain the upper end of the range.
*/
//Warning
/*
Because of the way the min() function is implemented, avoid using other functions inside the brackets, it may lead to incorrect results
*/
Creative Commons Attribution-ShareAlike 3.0 License (CC BY-SA 3.0)
“Arduino Reference:min(x, y)” by Arduino Team, used under CC BY-SA 3.0/ easy labo made some changes and comments to the original
max
maxは、2つの数値を比較して大きいほうの値を返します。
//Arduino Sketch Example: max(x, y)
//Date: 2015.1.18
//Edited and Modified by: easy labo
//Original Source: Arduino Reference (http://arduino.cc/en/Reference/HomePage)
//Syntax
max(x, y)
//Parameters
//x: the first number, any data type
//y: the second number, any data type
//Returns
//The larger of the two parameter values.
//Description
//Calculates the maximum of two numbers.
//Example
sensVal = max(senVal, 20); // assigns sensVal to the larger of sensVal or 20
// (effectively ensuring that it is at least 20)
//Note
/*
Perhaps counter-intuitively, max() is often used to constrain the lower end of a variable's range, while min() is used to constrain the upper end of the range.
*/
//Warning
/*
Because of the way the max() function is implemented, avoid using other functions inside the brackets, it may lead to incorrect results
*/
max(a--, 0); // avoid this - yields incorrect results
a--; // use this instead -
max(a, 0); // keep other math outside the function
Creative Commons Attribution-ShareAlike 3.0 License (CC BY-SA 3.0)
“Arduino Reference:max(x, y)” by Arduino Team, used under CC BY-SA 3.0/ easy labo made some changes and comments to the original
abs
absは、絶対値を計算して返します。
//Arduino Sketch Example: abs(x)
//Date: 2015.1.18
//Edited and Modified by: easy labo
//Original Source: Arduino Reference (http://arduino.cc/en/Reference/HomePage)
//Syntax
abs(x)
//Parameters
//x: the number
//Returns
//x: if x is greater than or equal to 0.
//-x: if x is less than 0.
//Description
//Computes the absolute value of a number.
//Warning
/*
Because of the way the abs() function is implemented, avoid using other functions inside the brackets, it may lead to incorrect results.
*/
abs(a++); // avoid this - yields incorrect results
a++; // use this instead -
abs(a); // keep other math outside the function
Creative Commons Attribution-ShareAlike 3.0 License (CC BY-SA 3.0)
“Arduino Reference:abs(x)” by Arduino Team, used under CC BY-SA 3.0/ easy labo made some changes and comments to the original
constrain
constrainは、数値を指定した範囲のなかに収めて返します。設定パラメータは次の通りです。
x: 計算対象の値
a: 範囲下限
b: 範囲上限
constrainは、xがa以上b以下のときはxがそのまま返ります。xがaより小さいときはa、bより大きいときはbが返ります。
//Arduino Sketch Example: constrain(x, a, b)
//Date: 2015.1.18
//Edited and Modified by: easy labo
//Original Source: Arduino Reference (http://arduino.cc/en/Reference/HomePage)
//Syntax
constrain(x, a, b)
//Parameters
//x: the number to constrain, all data types
//a: the lower end of the range, all data types
//b: the upper end of the range, all data types
//Returns
//x: if x is between a and b
//a: if x is less than a
//b: if x is greater than b
//Description
//Constrains a number to be within a range.
//Example
sensVal = constrain(sensVal, 10, 150);
// limits range of sensor values to between 10 and 150
Creative Commons Attribution-ShareAlike 3.0 License (CC BY-SA 3.0)
“Arduino Reference:constrain(x, a, b)” by Arduino Team, used under CC BY-SA 3.0/ easy labo made some changes and comments to the original
map
mapは、数値をある範囲から別の範囲に変換します。mapでは、次の計算で数値の変換が行われます。
//Arduino Sketch Example: map
//Date: 2015.1.18
//Edited and Modified by: easy labo
//Original Source: Arduino Reference (http://arduino.cc/en/Reference/HomePage)
long map(long value, long fromLow, long fromHigh, long toLow, long toHigh) {
return (value - fromLow) * (toHigh - toLow) / (fromHigh - fromLow) + toLow;
}
Creative Commons Attribution-ShareAlike 3.0 License (CC BY-SA 3.0)
“Arduino Reference:map(value, fromLow, fromHigh, toLow, toHigh)” by Arduino Team, used under CC BY-SA 3.0/ easy labo made some changes and comments to the original
fromLowと同じ値を与えると、toLowが返り、fromHighと同じ値ならtoHighとなります。その中間の値は、2つの範囲の大きさの比に基づいて計算されます。
mapの書式は、次の通りです。
//Arduino Sketch Example: Syntax of map
//Date: 2015.1.18
//Edited and Modified by: easy labo
//Original Source: Arduino Reference (http://arduino.cc/en/Reference/HomePage)
//Syntax
map(value, fromLow, fromHigh, toLow, toHigh)
//Parameters
//value: the number to 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.
Creative Commons Attribution-ShareAlike 3.0 License (CC BY-SA 3.0)
“Arduino Reference:map(value, fromLow, fromHigh, toLow, toHigh)” by Arduino Team, used under CC BY-SA 3.0/ easy labo made some changes and comments to the original
mapの設定パラメータは次の通りです。
value: 変換したい数値
fromLow: 現在の範囲の下限
fromHigh: 現在の範囲の上限
toLow: 変換後の範囲の下限
toHigh: 変換後の範囲の上限
変換後の戻り値は、long型です。また 、mapは範囲外の値も切り捨てません。ある範囲のなかに収めたい場合は、constrain関数と併用します。
//Arduino Sketch Example: map example 1
//Date: 2015.1.18
//Edited and Modified by: easy labo
//Original Source: Arduino Reference (http://arduino.cc/en/Reference/HomePage)
//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.
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
*/
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.
*/
Creative Commons Attribution-ShareAlike 3.0 License (CC BY-SA 3.0)
“Arduino Reference:map(value, fromLow, fromHigh, toLow, toHigh)” by Arduino Team, used under CC BY-SA 3.0/ easy labo made some changes and comments to the original
上は、次の様な例になっています。
(例)範囲の下限を上限より大きな値に設定し、値の反転に使用した例
y = map(x, 1, 50, 50, 1);
(例)範囲を指定するパラメータに負の数を使った例
y = map(x, 1, 50, 50, -100);
なお、map関数は整数だけを扱います。計算の結果、小数が生じる場合、小数部分は単純に切り捨てられます。
//Arduino Sketch Example: map example 2
//Date: 2015.1.18
//Edited and Modified by: easy labo
//Original Source: Arduino Reference (http://arduino.cc/en/Reference/HomePage)
//Example
//Map an analog value to 8 bits (0 to 255)
void setup() {}
void loop()
{
int val = analogRead(0);
val = map(val, 0, 1023, 0, 255);
analogWrite(9, val);
}
Creative Commons Attribution-ShareAlike 3.0 License (CC BY-SA 3.0)
“Arduino Reference:map(value, fromLow, fromHigh, toLow, toHigh)” by Arduino Team, used under CC BY-SA 3.0/ easy labo made some changes and comments to the original
上の例では、アナログ入力の10ビットの値を8ビットに丸めています。
pow
powは、べき乗の計算をします。パラメータは、次の通りです。
base: 底となる数値 (float)
exponent: 指数となる数値 (float)
//Arduino Sketch Example: pow
//Date: 2015.1.18
//Edited and Modified by: easy labo
//Original Source: Arduino Reference (http://arduino.cc/en/Reference/HomePage)
//Syntax
pow(base, exponent)
//Parameters
//base: the number (float)
//exponent: the power to which the base is raised (float)
//Returns
//The result of the exponentiation (double)
//Description
/*
Calculates the value of a number raised to a power. Pow() can be used to raise a number to a fractional power. This is useful for generating exponential mapping of values or curves.
*/
//Example
a = pow(10, 1.5); // return around 31
Creative Commons Attribution-ShareAlike 3.0 License (CC BY-SA 3.0)
“Arduino Reference:pow(base, exponent)” by Arduino Team, used under CC BY-SA 3.0/ easy labo made some changes and comments to the original
上の例では、10の1.5乗を計算しています。powは小数も扱えて指数関数的な値や曲線が必要なときに便利です。
sqrt
sqrtは、数値の平方根を返します。
//Arduino Sketch Example: sqrt
//Date: 2015.1.18
//Edited and Modified by: easy labo
//Original Source: Arduino Reference (http://arduino.cc/en/Reference/HomePage)
//Syntax
sqrt(x)
//Parameters
//x: the number, any data type
//Returns
//double, the number's square root.
//Description
//Calculates the square root of a number.
Creative Commons Attribution-ShareAlike 3.0 License (CC BY-SA 3.0)
“Arduino Reference:sqrt(x)” by Arduino Team, used under CC BY-SA 3.0/ easy labo made some changes and comments to the original
Sponsored Link