An ominous beep. And now another one. Wait, it’s counting down! Create a countdown timer that sounds like the bombs from action movies, but remember to use it at your own risk.
Parts list
What you’ll need
Step 1
Let’s build this
This is the positive leg of the buzzer. It will have a ‘+’ over top and will also be longer than the other leg.
Don’t forget to plug the board into your computer with the supplied USB cable.
Connections
Buzzer
The +LEG is the longer leg.
Step 2
Code some chaos
Don’t forget to select your port, like usual
Copy and paste the sample code
int buzzer = 11;
void setup() {
// put your setup code here, to run once:
pinMode(buzzer, OUTPUT);
for(int i = 1000; i > 10; i = i * 0.9){
digitalWrite(buzzer, HIGH);
delay(100);
digitalWrite(buzzer, LOW);
delay(i);
}
}
void loop() {
// put your main code here, to run repeatedly:
}
Upload the code and test it out
Step 3
Modify the madness
Change the length of the beeps
The longer a beeping sound goes for, the more annoying it is. Really. So why not make the beeps in our program longer? Right now, each beep only goes for a measly 100 milliseconds – a tenth of a second. Surely we can do better than that.
delay(100);
Line 9 in the code controls the time, in milliseconds, in-between when the buzzer is turned on and when it is turned off. If we change this value to something like 500, then the beeps will go for half a second. What about to 750? Each beep will now go for three-quarters of a second. Now that’s more like it.
delay(750);
Change how long the beeping goes for
Take a look at the code on line 7. While it does look a bit complicated, this is the line of code we need to change if we want to control how long the noise bomb beeping goes on for before it stops.
for(int i = 1000; i > 10; i = i * 0.9){
The only bit of code you need to pay attention to is int i = 1000;
which is at the very start of the line. This number controls the delay between beeps at the start of the program, which gradually decreases until there’s almost no delay at the end of the program. Like many of the other numbers we’ve been working with, this one is in milliseconds as well. Change it to 2000 and you will get a break of 2 seconds between beeps at the start of the program.