จัดการการอ่านค่าที่ล้มเหลว
ตรวจจับค่า NaN และลองใหม่โดยไม่ทำให้โปรแกรมหยุดทำงาน
จัดการการอ่านค่าที่ล้มเหลว เป็นบทเรียน Arduino & IoT Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Arduino & IoT Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Arduino & IoT Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Reads Can Fail
Even a good sensor sometimes returns nothing useful. Loose wires, fast polling, or noise can cause a failed read, so plan for it. ⚠️
Meet NaN
When a read fails, the library returns NaN, which means Not a Number. It is the sensor's way of saying I did not get a value.
Why NaN Is Tricky
A NaN never equals anything, not even itself. So you cannot just compare it with a normal number to catch the error.
Detect It with isnan
Use the built-in isnan() function to test a reading. It returns true when the value is the failed NaN result.
if (isnan(t)) {
}Check Before You Use
Always test a reading before you print or act on it. Skipping this check lets bad data slip into your logic unnoticed.
Check Both Values
Guard temperature and humidity together, since either can fail. If one is NaN, treat the whole reading as bad and skip it.
if (isnan(t) || isnan(h)) {
return;
}Print a Warning
When a read fails, print a clear message instead of a value. Future you will thank present you for the helpful note.
Serial.println("Failed to read DHT!");Retry, Do Not Crash
A single failure is normal, so do not stop the program. Simply retry on the next loop and the reading usually succeeds.
Keep the Last Good Value
For a steady display, store the last good reading and reuse it when a read fails. Your screen never shows an ugly blank.
Slow Down to Fix Failures
If failures pile up, you may be reading too fast or your wiring is loose. A longer delay and a firm connection often cure it.
Robust by Design
Handling errors gracefully is what separates a toy from a real device. Your sensor now survives the occasional hiccup calmly.
Quick Check
A DHT read fails. How should your sketch detect that in code?
Recap
You learned that failed reads return NaN, caught them with isnan, warned instead of crashing, and kept your device steady. 🎉
คำถามที่พบบ่อย
บทเรียน “จัดการการอ่านค่าที่ล้มเหลว” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “จัดการการอ่านค่าที่ล้มเหลว” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Arduino & IoT Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Arduino & IoT Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “จัดการการอ่านค่าที่ล้มเหลว”
ตรวจจับค่า NaN และลองใหม่โดยไม่ทำให้โปรแกรมหยุดทำงาน คุณปฏิบัติ Arduino & IoT Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Arduino & IoT Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Arduino & IoT Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “จัดการการอ่านค่าที่ล้มเหลว” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Arduino & IoT Academy นี้ได้ไหม
ได้ บทเรียน Arduino & IoT Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ต่อสาย DHT11/DHT22
- ติดตั้งไลบรารี DHT
- อ่านอุณหภูมิและความชื้น
- จัดการการอ่านค่าที่ล้มเหลว