目录
一.效果展示
二.基本原理
三.电路图
四.代码
一.效果展示
arduino播放《我爱你中国》
二.基本原理
利用arduino uno单片机实现对蜂鸣器振动频率的调节,基于PWM控制系统通过代码实现控制。
三.电路图
四.代码
//main.uno
#define Buzzer 2int PotBuffer = 0;
int delaytime=0;
void sound(int PotBuffer,int delaytime)
{float i=0.0;while(true){i+=float(PotBuffer);digitalWrite(Buzzer,HIGH); //设置输出高电平delayMicroseconds(PotBuffer); //延时PotBuffer值 usdigitalWrite(Buzzer,LOW); //设置输出低电平delayMicroseconds(200);if((i/100)>float(delaytime)){break;} }}
void delayplay(int ind_len,int PotBuffer,int delaytime)
{for(int ind=0;ind<ind_len;ind++){sound(PotBuffer,delaytime);delay(100);}
}
void setup()
{pinMode(Buzzer,OUTPUT); //蜂鸣器引脚设定为输出
}void loop()
{//sound(88,100); //do//sound(84,100); //re//sound(80,100); //mi//sound(78,100); //fa//sound(76,100); //so//sound(72,100); //la//sound(68,100);int dt=1;delayplay(1,104,500*dt);delayplay(1,100,500*dt);delayplay(1,88,2000*dt);delayplay(1,84,500*dt);delayplay(1,88,1500*dt);delayplay(1,118,400*dt);delayplay(1,94,800*dt);delayplay(1,104,5000*dt);delayplay(1,88,500*dt);delayplay(1,84,500*dt);delayplay(1,80,2000*dt);delayplay(1,72,500*dt);delayplay(1,76,1500*dt);delayplay(1,80,300*dt);delayplay(1,84,800*dt);delayplay(1,88,2000*dt);while(true){}}