0Pricing
Arduino & IoT Academy · レッスン

map() で値を新しい範囲に変換する

生の読み取り値を役立つ単位に再スケーリングします

「map() で値を新しい範囲に変換する」はCoddyKit上の無料Arduino & IoT Academyレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはArduino & IoT Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Arduino & IoT Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Raw Values Are Awkward

A 0 to 1023 reading rarely matches what you need. Often you want a percentage or an angle, not a raw sensor count.

Meet the map Function

The map function rescales a number from one range into another. It does the proportional math so you do not have to.

Its Five Arguments

You give map five things: the value, plus the low and high of its current range and the low and high of the target range.

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

Scale to a Percentage

To turn a knob into 0 to 100 percent, map from the analog range to a percent range. The result reads like a volume dial.

int pct = map(analogRead(A0), 0, 1023, 0, 100);

Scale to a Servo Angle

Servos expect 0 to 180. Map a knob into that range and the reading becomes a usable angle for the motor.

int angle = map(analogRead(A0), 0, 1023, 0, 180);

It Returns Whole Numbers

The map function works in integers, so it returns whole numbers and drops the fraction. That is fine for most controls.

You Can Flip the Range

Set the target low above the high to invert the output. Now turning the knob right makes the result go down.

int v = map(analogRead(A0), 0, 1023, 100, 0);

It Does Not Clamp

The map function will go past your limits if the input does. Use constrain afterward to keep the result safely inside bounds.

int v = constrain(map(x,0,1023,0,100),0,100);

Negative Ranges Work Too

Targets can be negative, like -50 to 50 for a centered control. The map math handles that direction with no extra effort.

Map for Brightness

Pair map with PWM later to set brightness. A knob can scale neatly into the 0 to 255 range an LED dimmer expects.

int b = map(analogRead(A0), 0, 1023, 0, 255);

A Tool You Will Reuse

You will reach for map constantly: percentages, angles, ranges, and more. It turns raw sensor data into meaningful numbers.

Quick Check

What is the job of the map function?

Recap: Rescaling with map

You learned map rescales a value between ranges using five arguments. Pair it with constrain to clamp results into safe bounds.

よくある質問

「map() で値を新しい範囲に変換する」レッスンは無料ですか?

はい。「map() で値を新しい範囲に変換する」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Arduino & IoT Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Arduino & IoT Academyコースには全4レッスンが含まれています。

「map() で値を新しい範囲に変換する」で何を学びますか?

生の読み取り値を役立つ単位に再スケーリングします ブラウザで直接実行するハンズオンコードでArduino & IoT Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Arduino & IoT Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのArduino & IoT Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「map() で値を新しい範囲に変換する」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このArduino & IoT Academyレッスンでコードを書いて実行できますか?

はい。すべてのArduino & IoT Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. デジタル信号とアナログ信号
  2. analogRead は 0~1023 を返す
  3. ポテンショメーターのつまみを読み取る
  4. map() で値を新しい範囲に変換する
← Arduino & IoT Academyに戻る