0Pricing
Arduino & IoT Academy · บทเรียน

จดจำตัวนับหลังรีเซ็ต

รักษาสถานะไว้ตลอดการตัดและจ่ายไฟใหม่

จดจำตัวนับหลังรีเซ็ต เป็นบทเรียน Arduino & IoT Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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! 🧠

คำถามที่พบบ่อย

บทเรียน “จดจำตัวนับหลังรีเซ็ต” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “จดจำตัวนับหลังรีเซ็ต” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Arduino & IoT Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Arduino & IoT Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “จดจำตัวนับหลังรีเซ็ต”

รักษาสถานะไว้ตลอดการตัดและจ่ายไฟใหม่ คุณปฏิบัติ 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 ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. ทำไมปุ่มจึงเกิดการเด้ง
  2. ตัดการเด้งด้วย millis()
  3. เก็บค่าใน EEPROM
  4. จดจำตัวนับหลังรีเซ็ต
← กลับไปที่ Arduino & IoT Academy