Lesson 9: Sound and functions
Cette page nécessite un navigateur récent (avec support SharedArrayBuffer). Veuillez mettre à jour Chrome, Edge, Firefox ou Safari vers la dernière version.
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() {
}
Difficulté 1/5 · 100 pièces + un petit bonus lors de la première réussite aujourd'hui
Play the note A (440 Hz) on the buzzer on pin 8 for 1 second, once.
Cliquez sur Exécuter pour voir le résultat ici.
Difficulté 3/5 · 200 pièces + un petit bonus lors de la première réussite aujourd'hui
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).
Cliquez sur Exécuter pour voir le résultat ici.