หน้าแรกHome › Arduino › บทที่ 04Lesson 04
LED หลายดวงMany LEDs
ควบคุม LED หลายดวง (ไฟจราจร)Control several LEDs (a traffic light)
บทนี้เราจะได้...In this lesson you will...
- ต่อ LED 3 ดวง เข้ากับ 3 ขาบนบอร์ดWire 3 LEDs to 3 pins on the board
- ใช้ตัวแปรตั้งชื่อขา ให้อ่านโค้ดง่ายUse variables to name the pins so code is easy to read
- ทำไฟจราจร: เขียว → เหลือง → แดงMake a traffic light: green → yellow → red
อุปกรณ์ที่ใช้What you need
LED ทั้ง 3 ดวงต้องกลับลง GND เหมือนกัน แทนที่จะลากสาย 3 เส้นไปที่บอร์ด เราต่อทุกดวงลงราง − ของบอร์ดทดลอง แล้วต่อรางนั้นไป GND เส้นเดียว เป็นเทคนิคที่ใช้บ่อยมากAll 3 LEDs must return to GND. Instead of 3 wires to the board, wire each one to the breadboard's − rail, then one wire from the rail to GND. You'll use this trick a lot.
โต๊ะทดลอง: ต่อวงจร แล้วรันโค้ดLab bench: wire it, then run the code
ลากจากขาหรือช่องหนึ่งไปอีกช่องเพื่อต่อสาย · ชี้ที่ช่องเพื่อดูว่าช่องไหนเชื่อมกัน · แตะที่สายเพื่อลบ · แตะที่ LED เพื่อกลับขาDrag from one pin or hole to another to add a wire · point at a hole to see what's connected · tap a wire to remove it · tap an LED to flip it
ภารกิจMissions
อ่านโค้ดทีละบรรทัดReading the code, line by line
ช่องโค้ดเริ่มจากโครงเปล่า ปิ๊งจะบอกทีละบรรทัดว่าต้องพิมพ์อะไร ตอนกดรัน บรรทัดที่บอร์ดกำลังทำจะเป็นสีเหลือง คำสั่งสำคัญของบทนี้มีดังนี้The code box starts as an empty sketch, and Ping tells you what to type line by line. While it runs, the line the board is doing turns yellow. The key commands in this lesson:
int red = 8;สร้างตัวแปรชื่อ red เก็บเลข 8 ต่อไปเขียน red แทนเลข 8 ได้เลยMake a variable called red that holds 8, so you can write red instead of 8pinMode(red, OUTPUT)ตั้งขาของไฟแดง (ขา 8) เป็นขาจ่ายไฟออกMake the red light's pin (8) an outputdigitalWrite(green, HIGH)เปิดไฟเขียวTurn the green light ondelay(3000)รอ 3 วินาที ก่อนทำบรรทัดถัดไปWait 3 seconds before the next lineลองแก้เองดูสิTry changing it
แก้โค้ดในช่องโค้ดของโต๊ะทดลองด้านบนได้เลย แล้วกด ▶ รันโค้ด ดูผล อยากเริ่มใหม่ทั้งหมด กด "ล้างโค้ด"Edit the code in the lab bench's code box above, then press ▶ Run code. Want to start over? Press "Clear code".
- ทำให้ไฟเขียวนานขึ้นเป็น 5 วินาทีMake the green light last 5 seconds
- ทำไฟวิ่ง: แดง → เหลือง → เขียว วนเร็ว ๆ ทีละ 0.2 วินาทีMake a running light: red → yellow → green, 0.2 s each
- ท้าทาย: เก็บขาไว้ใน array
int leds[] = {8, 9, 10};แล้วใช้forเปิดทีละดวงChallenge: keep the pins in an arrayint leds[] = {8, 9, 10};and light them one by one withfor
มีบอร์ด Arduino จริง? กดคัดลอกโค้ด ไปวางในโปรแกรม Arduino IDE (ฟรี) แล้วกดอัปโหลดลงบอร์ดได้เลยHave a real Arduino? Copy the code into the free Arduino IDE and upload it to your board.
ควิซท้ายบทQuick quiz
int red = 8; หมายความว่าอะไร?What does int red = 8; mean?
LED 3 ดวงต้องใช้ตัวต้านทานกี่ตัว?How many resistors do 3 LEDs need?
ถ้าอยากให้ไฟเหลืองติดนาน 2 วินาที ต้องแก้ตรงไหน?To keep yellow on for 2 seconds, what do you change?
ราง − ของบอร์ดทดลองมีไว้ทำอะไร?What is the breadboard's − rail for?
ใบงาน: Worksheet: LED หลายดวงMany LEDs
ออกแบบลำดับไฟจราจรของฉัน: สีไหนติดกี่วินาทีDesign my traffic light: which colour, for how many seconds
ทำครบทุกภารกิจแล้ว?All missions done?
กดปุ่มเพื่อให้จุดบนแผนที่บทเรียนสว่างขึ้นPress the button to light up this lesson on the map.