Lesson 9: Sound and functions
このページには最新のブラウザ(SharedArrayBuffer対応)が必要です。Chrome、Edge、Firefox、Safariを最新バージョンに更新してください。
English
A buzzer
tone(8, 440);makes pin 8 switch on and off 440 times a second. A buzzer plays the note A.tone(8, 440, 500);plays it for 500 ms.noTone(8);stops it.- Press the 🔇 button on the board to hear it.

void setup() {
tone(8, 262, 300); // C
delay(400);
tone(8, 330, 300); // E
delay(400);
tone(8, 392, 300); // G
}
void loop() {
}
English
Your own function
- A function is a name for some lines. You write it once and use it many times.
void beep(int hz)makes a function calledbeepthat gets one number.
void beep(int hz) {
tone(8, hz, 200);
delay(300);
}
void setup() {
beep(262);
beep(262);
beep(392);
}
void loop() {
}
English
In the exam
- 0478/22/O/N/23 Q8: explain why a programmer would use procedures and parameters. The mark scheme's points, in your
beep:- A procedure puts many statements under one name:
beep. - It can be re-used, so the program is shorter and easier to maintain: the two beep lines are written once and used three times.
- A parameter passes a value into the procedure:
beep(392)passes 392 tohz. - So the same procedure works with different data: a different note each time.
- A procedure puts many statements under one name:
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
Play the note A (440 Hz) on the buzzer on pin 8 for 1 second, once.
実行ボタンをクリックして出力を確認してください。
難易度 3/5 · 200 コイン + 今日初回達成時の小ボーナス
Write a function void ding(int hz) that plays hz on pin 8 for 300 ms and then waits 400 ms. In setup(), call it twice: first 659 Hz, then 523 Hz (a doorbell).
実行ボタンをクリックして出力を確認してください。