Buzzer Alarm
Language: | English • 中文 |
---|
ContentsObjectiveThis tutorial will show you how to use Microduino-buzzer with three examples. Equipment
DescriptionGenerally, buzzer can be divided into active buzzer and passive buzzer.
Example 1: Let the buzzer goes offBuild module circuit
Debugging
#define buzzer_pin 6 //Define buzzer pin
#define buzzer_fre 600 //Define output frequency
void setup()
{
pinMode(buzzer_pin,OUTPUT);
}
void loop()
{
tone(buzzer_pin,buzzer_fre); //Drive the buzzer
} Select Microduino-CoreUSB as the board card as well as corresponding port(COMXX). Click the right arrow(—>)and download program. When you see upload finish notice, it means the program has been written into CoreUSB. Then, you'll hear buzzer goes off.
Example 2: Buzzer makes alarm
#define buzzer_pin 6 //Define buzzer pin
void setup()
{
pinMode(buzzer_pin,OUTPUT);
}
void loop()
{
for(int i=200;i<=800;i++) //Increase frequency from 200HZ to 800HZ in a circular manner.
{
tone(buzzer_pin,i); //Output frequency in Number four port.
delay(5); //The frequency lasts 5ms.
}
delay(2000); //The highest frequency lasts 2s.
for(int i=800;i>=200;i--)
{
tone(buzzer_pin,i);
delay(10); //The frequency lasts 10ms.
}
} “for(int i=200;i<=800;i++)”Description: The value of 'i' is 200 initially, which increases 1 each time the 'for' function executes and it will exit from 'for' loop until the value reaches 800. Users can change parameters and watch buzzer change.
Example 3: Play music
#define buzzer_pin 6 //Define buzzer pin
int song[] = {
262, 262, 294, 262, 349, 330,
262, 262, 294, 262, 392, 349,
262, 262, 523, 440, 349, 330, 294,
494, 494, 440, 349, 392, 349
};
int noteDurations[] = {
4, 4, 2, 2, 2, 1,
4, 4, 2, 2, 2, 1,
4, 4, 2, 2, 2, 2, 2,
4, 4, 2, 2, 2, 1
};
void setup() {
pinMode(buzzer_pin, OUTPUT);
}
void loop() {
song_play();
}
void song_play()
{
for (int thisNote = 0; thisNote < 25; thisNote++)
{
int noteDuration = 1000 / noteDurations[thisNote];
tone(buzzer_pin, song[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 1.20;
delay(pauseBetweenNotes);
noTone(buzzer_pin);
}
} ' song_play()' is music play function and the note (frequency) is saved in ' song[]'. ' noteDurations[]' represents rhythm.
|