ห้องทดลองเทคโนโลยีTech Lab ครูใบเฟิร์นKru Baifern

หน้าแรกHome › Arduino › บทที่ 04Lesson 04

04

LED หลายดวงMany LEDs

ควบคุม LED หลายดวง (ไฟจราจร)Control several LEDs (a traffic light)

ระดับเนื้อหาLevel
1

บทนี้เราจะได้...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
2

อุปกรณ์ที่ใช้What you need

Arduino UnoArduino Uno× 1
LED สีแดงRed LED× 1
LED สีเหลืองYellow LED× 1
LED สีเขียวGreen LED× 1
ตัวต้านทาน 220Ω220Ω resistor× 3
บอร์ดทดลองBreadboard× 1
สายจัมเปอร์Jumper wires× 7
มัธยม · รางไฟ GND ร่วมSecondary · A shared GND rail

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.

3

โต๊ะทดลอง: ต่อวงจร แล้วรันโค้ดLab bench: wire it, then run the code

สีสายWire

ลากจากขาหรือช่องหนึ่งไปอีกช่องเพื่อต่อสาย · ชี้ที่ช่องเพื่อดูว่าช่องไหนเชื่อมกัน · แตะที่สายเพื่อลบ · แตะที่ 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

โค้ด (แก้ได้เลย!)Code (edit it!)

ภารกิจMissions

    4

    อ่านโค้ดทีละบรรทัด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 8
    pinMode(red, OUTPUT)ตั้งขาของไฟแดง (ขา 8) เป็นขาจ่ายไฟออกMake the red light's pin (8) an output
    digitalWrite(green, HIGH)เปิดไฟเขียวTurn the green light on
    delay(3000)รอ 3 วินาที ก่อนทำบรรทัดถัดไปWait 3 seconds before the next line
    5

    ลองแก้เองดูสิ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 array int leds[] = {8, 9, 10}; and light them one by one with for

    มีบอร์ด Arduino จริง? กดคัดลอกโค้ด ไปวางในโปรแกรม Arduino IDE (ฟรี) แล้วกดอัปโหลดลงบอร์ดได้เลยHave a real Arduino? Copy the code into the free Arduino IDE and upload it to your board.

    6

    ควิซท้ายบท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?

    ทำครบทุกภารกิจแล้ว?All missions done?

    กดปุ่มเพื่อให้จุดบนแผนที่บทเรียนสว่างขึ้นPress the button to light up this lesson on the map.

    กลับแผนที่Back to map
    ← บทที่ 03Lesson 03 บทที่ 05Lesson 05 →