リセット後もカウンターを保持する
電源の入れ直し後も状態を保持します。
「リセット後もカウンターを保持する」はCoddyKit上の無料Arduino & IoT Academyレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはArduino & IoT Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Arduino & IoT Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
A Counter That Survives
Let's build a press counter that remembers its total even after a power cut. We'll combine debouncing with EEPROM storage.
The Plan in One Line
On startup, load the saved count. On each clean press, add one and save it back. Simple, but powerful.
Pick a Storage Slot
Reserve one EEPROM address for the counter. We'll keep it small enough to fit in a single byte for now.
const int ADDR = 0;
byte count = 0;Load on Boot
Inside setup(), read the saved value first. Now your counter picks up exactly where it left off.
void setup() {
count = EEPROM.read(ADDR);
}Wipe the Junk First
A fresh chip's EEPROM often reads 255. Treat that as empty and reset your counter to zero on the first run.
if (count == 255) count = 0;Reuse Your Debounce
Detect presses with the millis() debounce from before. Clean input is essential, or your saved count drifts upward.
Increment on a Clean Press
When a debounced press fires, bump the counter by one. This is the single moment your total grows.
count++;
Serial.println(count);Save Without Wearing Out
Use EEPROM.update to write the new total. It only touches memory when the value actually changed, protecting the chip.
EEPROM.update(ADDR, count);Test the Magic
Press a few times, then unplug and replug the board. The Serial Monitor shows your count continuing, not restarting. 🎉
Mind the Byte Limit
A byte counter wraps back to 0 after 255. For bigger totals, store an int with EEPROM.put and EEPROM.get instead.
Add a Reset Option
A real product often needs a way to clear the saved count. A long button hold could write 0 back to EEPROM.
EEPROM.update(ADDR, 0);Quick Check
Let's check the key step that lets the counter survive a reset.
Recap: Memory Across Resets
You built a counter that persists by loading from EEPROM at boot and saving each clean press. Your device finally has a memory! 🧠
よくある質問
「リセット後もカウンターを保持する」レッスンは無料ですか?
はい。「リセット後もカウンターを保持する」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Arduino & IoT Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Arduino & IoT Academyコースには全4レッスンが含まれています。
「リセット後もカウンターを保持する」で何を学びますか?
電源の入れ直し後も状態を保持します。 ブラウザで直接実行するハンズオンコードでArduino & IoT Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Arduino & IoT Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのArduino & IoT Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「リセット後もカウンターを保持する」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このArduino & IoT Academyレッスンでコードを書いて実行できますか?
はい。すべてのArduino & IoT Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- ボタンが「チャタリング」する理由
- millis()でデバウンスする
- EEPROMに値を保存する
- リセット後もカウンターを保持する