if/else: ボタンに LED を連動させる
ボタンを押している間だけ LED を点灯させます
「if/else: ボタンに LED を連動させる」はCoddyKit上の無料Arduino & IoT Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはArduino & IoT Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Arduino & IoT Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Make a Decision
Reading a button is great, but the magic is reacting to it. An if statement lets your board choose what to do based on the reading.
if Runs Conditionally
The code inside if only runs when its test is true. So you can light an LED only when the button is actually pressed.
if (state == LOW) {
// button is pressed
}Test for Pressed
Remember the inversion: with INPUT_PULLUP, pressed is LOW. So your condition checks whether the stored state equals LOW.
int state = digitalRead(2);
if (state == LOW) {
digitalWrite(13, HIGH);
}Add an else Branch
What about when it isn't pressed? The else block runs whenever the if test is false, giving you the other half of the behavior.
if (state == LOW) {
digitalWrite(13, HIGH);
} else {
digitalWrite(13, LOW);
}LED Follows the Button
Together if and else make the LED mirror the button: on while held, off when released. That is your first real input-to-output link. 💡
Set Up Both Pins
The button pin is an input and the LED pin is an output. Declare both modes in setup() so each pin knows its job.
void setup() {
pinMode(2, INPUT_PULLUP);
pinMode(13, OUTPUT);
}One Equals vs Two
A classic trap: use == to compare, not a single =. One equals assigns a value, while two equals asks if they match.
Loop Keeps It Live
Put the whole thing in loop() so it checks the button thousands of times a second. The LED reacts the moment you press.
Read Then Decide
The pattern is always the same: read the input, then decide with if/else. Get comfortable with it; you'll reuse it everywhere.
Compare Right in the if
You can skip the variable and test the read directly. It reads cleanly and does exactly the same comparison inside the parentheses.
if (digitalRead(2) == LOW) {
digitalWrite(13, HIGH);
}From Sensing to Acting
You just closed the loop from input to output with pure logic. Swap the LED for a buzzer or motor and the same idea still drives it.
Quick Check
Pick the condition that lights the LED while the button is held.
Recap
You used if/else to make an LED follow a button: LOW lights it, HIGH turns it off. Remember == for comparison. Next you'll wire it all up. 🔌
よくある質問
「if/else: ボタンに LED を連動させる」レッスンは無料ですか?
はい。「if/else: ボタンに LED を連動させる」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Arduino & IoT Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Arduino & IoT Academyコースには全4レッスンが含まれています。
「if/else: ボタンに LED を連動させる」で何を学びますか?
ボタンを押している間だけ LED を点灯させます ブラウザで直接実行するハンズオンコードでArduino & IoT Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Arduino & IoT Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのArduino & IoT Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「if/else: ボタンに LED を連動させる」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このArduino & IoT Academyレッスンでコードを書いて実行できますか?
はい。すべてのArduino & IoT Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- INPUT と INPUT_PULLUP
- ボタンの状態を読み取る
- if/else: ボタンに LED を連動させる
- ブレッドボードにボタンを配線する