อ่านคำสั่งที่คุณพิมพ์
ใช้ข้อมูลเข้าแบบอนุกรมเพื่อสั่งให้ทำงานจากแป้นพิมพ์
อ่านคำสั่งที่คุณพิมพ์ เป็นบทเรียน Arduino & IoT Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Arduino & IoT Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Arduino & IoT Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Talk Back to Your Board
Serial works both ways. You can type into the Monitor and your board will read it, letting you send commands from your keyboard. ⌨️
Is Anything Waiting?
First check if data has arrived with Serial.available(). It returns how many bytes are sitting in the buffer, ready to read.
if (Serial.available() > 0) {
}Why Check First
Reading when nothing is there gives junk. So you guard your read with available() and only act when real data exists.
Read One Character
Grab a single incoming character with Serial.read(). It pulls one byte from the buffer and hands it to you.
char c = Serial.read();React to a Command
Now branch on what you got. Compare the character and run different actions, like turning an LED on or off.
if (c == '1') digitalWrite(13, HIGH);
if (c == '0') digitalWrite(13, LOW);Mind the Quotes
Notice the single quotes around '1'. That is a character, not the number one, and matching it correctly matters.
Reading Whole Words
For full commands like on or off, read a whole line with readStringUntil(), stopping at the newline you send.
String cmd = Serial.readStringUntil('\n');Compare the Text
Then test the whole word. If the String equals on, do one thing; if it equals off, do another.
if (cmd == "on") digitalWrite(13, HIGH);Set the Line Ending
In the Monitor, pick a line ending like Newline. It tells readStringUntil where each typed command stops.
Confirm Back
Good feedback feels great. After acting on a command, println a confirmation so you know the board heard you.
Serial.println("LED is on");Now It Is Interactive
You just built a tiny command interface. Type, the board reads, acts, and replies. That is real two-way control. 🚀
Quick Check
Which function tells you whether typed data has arrived and is waiting to be read?
Recap
You made the board listen: checking available(), reading characters or full lines, acting on commands, and confirming back. Two-way Serial done! 🎉
คำถามที่พบบ่อย
บทเรียน “อ่านคำสั่งที่คุณพิมพ์” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “อ่านคำสั่งที่คุณพิมพ์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ 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 ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- Serial.begin และอัตราบอด
- Serial.print เทียบกับ println
- ดูค่าจากเซนเซอร์แบบสด
- อ่านคำสั่งที่คุณพิมพ์