if/else: LED ทำตามปุ่ม
ให้ LED สว่างเฉพาะขณะที่กดปุ่มค้างไว้
if/else: LED ทำตามปุ่ม เป็นบทเรียน Arduino & IoT Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Arduino & IoT Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Arduino & IoT Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “if/else: LED ทำตามปุ่ม”
ให้ LED สว่างเฉพาะขณะที่กดปุ่มค้างไว้ คุณปฏิบัติ Arduino & IoT Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Arduino & IoT Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Arduino & IoT Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “if/else: LED ทำตามปุ่ม” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Arduino & IoT Academy นี้ได้ไหม
ได้ บทเรียน Arduino & IoT Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- INPUT เทียบกับ INPUT_PULLUP
- อ่านสถานะปุ่ม
- if/else: LED ทำตามปุ่ม
- ต่อปุ่มบนเบรดบอร์ด