0Pricing
Arduino & IoT Academy · Lekcja

map() — mapowanie wartości na nowy zakres

Przeskalowanie surowych odczytów na użyteczne jednostki

map() — mapowanie wartości na nowy zakres to bezpłatna lekcja Arduino & IoT Academy na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Arduino & IoT Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Arduino & IoT Academy zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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.

Często zadawane pytania

Czy lekcja „map() — mapowanie wartości na nowy zakres” jest bezpłatna?

Tak — pełny tekst „map() — mapowanie wartości na nowy zakres” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Arduino & IoT Academy, przejdź na CoddyKit PRO. Kurs Arduino & IoT Academy zawiera 4 lekcji w sumie.

Co nauczysz się w „map() — mapowanie wartości na nowy zakres”?

Przeskalowanie surowych odczytów na użyteczne jednostki Ćwiczysz Arduino & IoT Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Arduino & IoT Academy?

Nie wymagamy żadnego doświadczenia. Arduino & IoT Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.

Ile czasu zajmuje lekcja „map() — mapowanie wartości na nowy zakres”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Arduino & IoT Academy?

Tak. Każda lekcja Arduino & IoT Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Sygnały cyfrowe i analogowe
  2. analogRead zwraca wartości 0–1023
  3. Odczytywanie pokrętła potencjometru
  4. map() — mapowanie wartości na nowy zakres
← Powrót do Arduino & IoT Academy