入力したコマンドを読み取る
シリアル入力を使ってキーボードから操作を実行します。
「入力したコマンドを読み取る」はCoddyKit上の無料Arduino & IoT Academyレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これは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! 🎉
よくある質問
「入力したコマンドを読み取る」レッスンは無料ですか?
はい。「入力したコマンドを読み取る」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと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フィードバックを取得できます。ローカル設定は不要です。