if/else:LED 跟随按钮
仅在按住按钮时点亮 LED
if/else:LED 跟随按钮 是 CoddyKit 上的免费 Arduino & IoT Academy 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 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 跟随按钮」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Arduino & IoT Academy 课程的其余内容,请升级到 CoddyKit PRO。 Arduino & IoT Academy 课程共包含 4 节课。
「if/else:LED 跟随按钮」这节课中我会学到什么?
仅在按住按钮时点亮 LED 你通过在浏览器中直接运行的动手代码来练习 Arduino & IoT Academy,全天候 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 跟随按钮
- 在面包板上连接按钮