Beeps and Silence
Part of the Outputs section of Coddy's Arduino journey. Lesson 11 of 24.
To stop a tone, call noTone(pin). A beep is a tone, a wait, then silence:
tone(8, 1000);
delay(100);
noTone(8);tone() can also take a third number, a duration in milliseconds. The tone then stops on its own:
tone(8, 1000, 100); // beep for 100 ms
delay(200); // the beep, then 100 ms of quietCareful: tone() does not wait for the beep to end. The next line runs at once, while the buzzer is still sounding. So a delay() is still needed to space beeps out: without it, two beeps in a row land on top of each other and you hear one.
Challenge
EasyMake the buzzer on pin 8 beep in pairs, like a phone ringing:
- a 1000 Hz beep of 100 ms
- 100 ms of silence
- a second 100 ms beep
- one second of silence, then again
Press Run, then Check.
Try it yourself
const int buzzerPin = 8;
void setup() {
}
void loop() {
// beep, short silence, beep, then a second of silence
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Outputs
3Sound
Play a Tone on a BuzzerBeeps and SilencePitch From a KnobNotes in an ArrayA Melody From Two ArraysPractice on your own: Arduino playground