map() ค่าไปยังช่วงใหม่
ปรับสเกลค่าที่อ่านได้ดิบให้เป็นหน่วยที่ใช้งานได้
map() ค่าไปยังช่วงใหม่ เป็นบทเรียน Arduino & IoT Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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() ค่าไปยังช่วงใหม่” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Arduino & IoT Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Arduino & IoT Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “map() ค่าไปยังช่วงใหม่”
ปรับสเกลค่าที่อ่านได้ดิบให้เป็นหน่วยที่ใช้งานได้ คุณปฏิบัติ Arduino & IoT Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Arduino & IoT Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Arduino & IoT Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “map() ค่าไปยังช่วงใหม่” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Arduino & IoT Academy นี้ได้ไหม
ได้ บทเรียน Arduino & IoT Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- สัญญาณดิจิทัลเทียบกับแอนะล็อก
- analogRead คืนค่า 0–1023
- อ่านปุ่มหมุนโพเทนชิโอมิเตอร์
- map() ค่าไปยังช่วงใหม่