0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

arduinoでmap関数

Posted at

概要

arduinoのmap関数が便利そうなので、テストしてみた。

コード

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

サンプルコード

void setup()
{
	Serial.begin(115200);
	Serial.println("ok");
	long ans;
	ans = map(300, 200, 500, 0, 100);
	Serial.print("map(300, 200, 500, 0, 100) = ");
	Serial.println(ans);
	ans = map(600, 200, 500, 0, 100);
	Serial.print("map(600, 200, 500, 0, 100) = ");
	Serial.println(ans);
	ans = map(100, 200, 500, 0, 100);
	Serial.print("map(100, 200, 500, 0, 100) = ");
	Serial.println(ans);
	ans = map(300, 200, 500, -400, 0);
	Serial.print("map(300, 200, 500, -400, 0) = ");
	Serial.println(ans);
	ans = map(600, 200, 500, 0, 100);
	ans = constrain(ans, 0, 100);
	Serial.print("map2(600, 200, 500, 0, 100) = ");
	Serial.println(ans);
	ans = map(100, 200, 500, 0, 100);
	ans = constrain(ans, 0, 100);
	Serial.print("map2(100, 200, 500, 0, 100) = ");
	Serial.println(ans);
	
}
void loop()
{
}

結果

map.JPG

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?