Arduino
有源蜂鸣器
/********************************************************
* function: you can hear the activate buzzer beeping.
* But it won't work if you can use a passive one here
**************************************************** */
int buzzerPin = 7;//the pin of the activate buzzer attach to pin7
void setup()
{
pinMode(buzzerPin, OUTPUT);
digitalWrite(buzzerPin, LOW);
}
void loop()
{
}
无源蜂鸣器
//function:buzzer beeping in a different frequence
/*****************************************************/
const int buzzerPin = 7;
int fre; //frequenze value
/****************************************************/
void setup()
{
pinMode(buzzerPin, OUTPUT);//set the buzzerPin as OUTPUT
}
/*****************************************************/
void loop()
{
for (int i=200; i<=800; i++) //frequence loop from 200 to 800
{
tone(7,i); //in pin7 generate a tone, its frequency is i
delay(5); //wait for 5 milliseconds
}
delay(4000); //wait for 4 seconds on highest frequence
for(int i=800; i>=200; i--) //frequence loop from 800 to 200
{
tone(7,i);
delay(10