หน้าแรกHome › Arduino › บทที่ 07Lesson 07
BuzzerBuzzer
สร้างเสียงเตือนMake a warning beep
บทนี้เราจะได้...In this lesson you will...
- ต่อ buzzer ให้บอร์ดส่งเสียงได้Wire a buzzer so the board can make sound
- ใช้ tone() กับ noTone() เปิด–ปิดเสียงUse tone() and noTone() to start and stop sound
- รู้ว่าความถี่ (Hz) มาก = เสียงสูงKnow that a bigger frequency (Hz) = a higher sound
อุปกรณ์ที่ใช้What you need
tone(8, 1000) ทำให้ขา 8 สลับ HIGH/LOW 1000 ครั้งต่อวินาที แผ่นเพียโซใน buzzer สั่นตาม อากาศสั่นตาม หูเราจึงได้ยินเป็นเสียง หูคนได้ยินราว 20–20,000 Hztone(8, 1000) flips pin 8 HIGH/LOW 1000 times a second. The piezo disc in the buzzer shakes, the air shakes, and we hear it. People hear about 20–20,000 Hz.
โต๊ะทดลอง: ต่อวงจร แล้วรันโค้ด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:
tone(buzzer, 1000)ส่งเสียงที่ความถี่ 1000 Hz ออกขา 8 (เสียงดังค้างจนกว่าจะสั่งหยุด)Play a 1000 Hz sound on pin 8 (it keeps going until you stop it)noTone(buzzer)หยุดเสียงStop the soundtone(buzzer, 1500)ความถี่มากขึ้น = เสียงสูงขึ้นA bigger frequency = a higher sounddelay(200)ปล่อยเสียงหรือความเงียบไว้ 0.2 วินาทีKeep the sound (or the quiet) for 0.2 secondsลองแก้เองดูสิ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".
- ทำเสียงไซเรน: สลับ 600 Hz กับ 900 HzMake a siren: switch between 600 Hz and 900 Hz
- เล่นโน้ต โด เร มี: 262, 294, 330 HzPlay do re mi: 262, 294, 330 Hz
- ท้าทาย: เก็บโน้ตเพลงไว้ใน array แล้วเล่นด้วย for เช่นเพลงช้างช้างช้างChallenge: keep a tune's notes in an array and play them with for
มีบอร์ด Arduino จริง? กดคัดลอกโค้ด ไปวางในโปรแกรม Arduino IDE (ฟรี) แล้วกดอัปโหลดลงบอร์ดได้เลยHave a real Arduino? Copy the code into the free Arduino IDE and upload it to your board.
ควิซท้ายบทQuick quiz
คำสั่งไหนทำให้ buzzer เงียบ?Which command makes the buzzer quiet?
tone(8, 2000) เทียบกับ tone(8, 500) เสียงเป็นยังไง?How does tone(8, 2000) sound compared with tone(8, 500)?
Hz (เฮิรตซ์) คืออะไร?What is Hz (hertz)?
buzzer ใช้ทำอะไรได้บ้าง?What can a buzzer be used for?
ใบงาน: Worksheet: BuzzerBuzzer
เขียนเพลงสั้น ๆ ของฉัน: โน้ตไหน กี่ Hz นานเท่าไรWrite my short tune: which notes, how many Hz, how long
ทำครบทุกภารกิจแล้ว?All missions done?
กดปุ่มเพื่อให้จุดบนแผนที่บทเรียนสว่างขึ้นPress the button to light up this lesson on the map.