0Pricing
Arduino & IoT Academy · レッスン

読み取り失敗に対処する

NaNを検出し、クラッシュせずに再試行します。

「読み取り失敗に対処する」はCoddyKit上の無料Arduino & IoT Academyレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これは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. 🎉

よくある質問

「読み取り失敗に対処する」レッスンは無料ですか?

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

「読み取り失敗に対処する」で何を学びますか?

NaNを検出し、クラッシュせずに再試行します。 ブラウザで直接実行するハンズオンコードでArduino & IoT Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

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

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

「読み取り失敗に対処する」レッスンにはどのくらい時間がかかりますか?

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

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

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

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

  1. DHT11/DHT22を配線する
  2. DHTライブラリをインストールする
  3. 温度と湿度を読み取る
  4. 読み取り失敗に対処する
← Arduino & IoT Academyに戻る